You are here

sitemap.install in Sitemap 2.0.x

Same filename and directory in other branches
  1. 8.2 sitemap.install
  2. 8 sitemap.install

Installation functions for Sitemap module.

File

sitemap.install
View source
<?php

/**
 * @file
 * Installation functions for Sitemap module.
 */
use Drupal\sitemap\Plugin\Sitemap\Vocabulary;

/**
 * Change sitemap configuration page permission to administer sitemap.
 */
function sitemap_update_8101() {
  $roles = user_roles(FALSE, 'administer site configuration');
  if (!empty($roles)) {
    foreach ($roles as $rid => $role) {
      user_role_grant_permissions($rid, [
        'administer sitemap',
      ]);
    }
  }
}

/**
 * Update the CSS configuration setting to reflect the change in option wording
 * (from negation to affirmation).
 */
function sitemap_update_8102(&$sandbox) {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('sitemap.settings');
  if ($config
    ->get('css')) {
    $css = $config
      ->get('css') == TRUE ? FALSE : TRUE;
    $config
      ->set('css', $css);
  }
  $config
    ->save();
}

/**
 * Update configuration for 2.x.
 */
function sitemap_update_8200(&$sandbox) {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('sitemap.settings');

  // Update the config key for the CSS option.
  $config
    ->set('include_css', $config
    ->get('css'));
  $config
    ->clear('css');

  // Remove the config for the RSS position option.
  $show_rss = (bool) $config
    ->get('show_rss_links');
  $config
    ->clear('show_rss_links');
  $plugins = [];
  $show_titles = $config
    ->get('show_titles');
  $order = $config
    ->get('order');
  $config
    ->clear('show_titles');
  $config
    ->clear('order');

  // Update to the frontpage plugin.
  $plugins['frontpage'] = [
    'enabled' => (bool) $config
      ->get('show_front'),
    'weight' => isset($order['front']) ? $order['front'] : 0,
    'settings' => [
      'title' => $show_titles ? 'Front page' : '',
      'rss' => $config
        ->get('rss_front') && $show_rss ? '/' . $config
        ->get('rss_front') : '',
    ],
  ];
  $config
    ->clear('show_front');
  $config
    ->clear('rss_front');

  // Update to the book plugin.
  if ($books = $config
    ->get('show_books')) {
    foreach ($books as $bid => $enabled) {
      $plugins['book:' . $bid] = [
        'enabled' => (bool) $enabled,
        'weight' => isset($order['books_' . $bid]) ? $order['books_' . $bid] : 0,
        'settings' => [
          'title' => sitemap_book_title_update($bid, $show_titles),
          'show_expanded' => $config
            ->get('books_expanded'),
        ],
      ];
    }
  }
  $config
    ->clear('show_books');
  $config
    ->clear('books_expanded');

  // Update to the menu plugin.
  if ($menus = $config
    ->get('show_menus')) {
    foreach ($menus as $menu_id => $enabled) {
      $plugins['menu:' . $menu_id] = [
        'enabled' => (bool) $enabled,
        'weight' => isset($order['menus_' . $menu_id]) ? $order['menus_' . $menu_id] : 0,
        'settings' => [
          'title' => sitemap_menu_title_update($menu_id, $show_titles),
          'show_disabled' => $config
            ->get('show_menus_hidden'),
        ],
      ];
    }
  }
  $config
    ->clear('show_menus');
  $config
    ->clear('show_menus_hidden');

  // Update to the vocabulary plugin.
  if ($vocabs = $config
    ->get('show_vocabularies')) {
    if (\Drupal::service('module_handler')
      ->moduleExists('forum')) {
      $forumVid = \Drupal::config('forum.settings')
        ->get('vocabulary');
    }
    foreach ($vocabs as $vid => $enabled) {
      $plugins['vocabulary:' . $vid] = [
        'enabled' => (bool) $enabled,
        'weight' => isset($order['vocabularies_' . $vid]) ? $order['vocabularies_' . $vid] : 0,
        'settings' => [
          'title' => $show_titles ? sitemap_vocab_title_update($vid) : '',
          'show_description' => $config
            ->get('show_description'),
          'show_count' => $config
            ->get('show_count'),
          'term_depth' => sitemap_vocab_depth_update($config
            ->get('vocabulary_depth')),
          'term_count_threshold' => isset($formVid) && $forumVid == $vid ? sitemap_vocab_threshold_update($config
            ->get('forum_threshold')) : sitemap_vocab_threshold_update($config
            ->get('term_threshold')),
          'customize_link' => $config
            ->get('vocabulary_show_links'),
          'term_link' => Vocabulary::DEFAULT_TERM_LINK,
          'always_link' => $config
            ->get('vocabulary_show_links'),
          'enable_rss' => (bool) sitemap_vocab_depth_update($config
            ->get('rss_taxonomy')),
          'rss_link' => Vocabulary::DEFAULT_TERM_RSS_LINK,
          'rss_depth' => sitemap_vocab_depth_update($config
            ->get('rss_taxonomy')),
        ],
      ];
    }
  }
  $config
    ->clear('show_vocabularies');
  $config
    ->clear('show_description');
  $config
    ->clear('show_count');
  $config
    ->clear('vocabulary_depth');
  $config
    ->clear('vocabulary_show_links');
  $config
    ->clear('term_threshold');
  $config
    ->clear('forum_threshold');
  $config
    ->clear('rss_taxonomy');
  $config
    ->set('plugins', $plugins);
  $config
    ->save(TRUE);
}

/**
 * Helper function to get a book title for the 2.x settings update.
 *
 * @param int $bid
 * @param boolean $show_title
 *
 * @return string
 */
function sitemap_book_title_update($bid, $show_title) {
  if (!$show_title) {
    return '';
  }
  else {
    $book = \Drupal::service('entity_type.manager')
      ->getStorage('node')
      ->load($bid);
    if ($book) {
      return $book
        ->label();
    }
  }
}

/**
 * Helper function to get a menu title from 1.x menu settings.
 *
 * @param string $menu_id
 * @param bool $show_title
 *
 * @return string
 */
function sitemap_menu_title_update($menu_id, $show_title) {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('system.menu.' . $menu_id);
  $title = '';
  if ($show_title) {
    if ($thirdParty = $config
      ->get('third_party_settings')) {
      if (isset($thirdParty['sitemap']) && ($setting = $thirdParty['sitemap'])) {
        if (isset($setting['sitemap_display_name'])) {
          $title = $setting['sitemap_display_name'];
        }
      }
    }
    if (empty($title)) {
      $title = $config
        ->get('label');
    }
  }
  $config
    ->clear('third_party_settings.sitemap');
  $config
    ->save();
  return $title;
}

/**
 * Helper function to get a vocabulary title for the 2.x settings update.
 *
 * @param string $vid
 *
 * @return string
 */
function sitemap_vocab_title_update($vid) {
  $config_factory = \Drupal::configFactory();
  $config = $config_factory
    ->getEditable('taxonomy.vocabulary.' . $vid);
  return $config
    ->get('name');
}

/**
 * Helper function to update the term_depth setting.
 *
 * @param int $value
 *
 * @return int
 */
function sitemap_vocab_depth_update($value) {
  if ($value == -1 || $value > Vocabulary::DEPTH_MAX) {
    return Vocabulary::DEPTH_MAX;
  }
  return $value;
}

/**
 * Helper function to update the term_threshold and forum_threshold settings.
 *
 * @param int $value
 *
 * @return int
 */
function sitemap_vocab_threshold_update($value) {
  if ($value == -1) {
    return Vocabulary::THRESHOLD_DISABLED;
  }
  return $value;
}

Functions

Namesort descending Description
sitemap_book_title_update Helper function to get a book title for the 2.x settings update.
sitemap_menu_title_update Helper function to get a menu title from 1.x menu settings.
sitemap_update_8101 Change sitemap configuration page permission to administer sitemap.
sitemap_update_8102 Update the CSS configuration setting to reflect the change in option wording (from negation to affirmation).
sitemap_update_8200 Update configuration for 2.x.
sitemap_vocab_depth_update Helper function to update the term_depth setting.
sitemap_vocab_threshold_update Helper function to update the term_threshold and forum_threshold settings.
sitemap_vocab_title_update Helper function to get a vocabulary title for the 2.x settings update.