You are here

function template_preprocess_sitemap in Sitemap 8

Preprocesses the variables for sitemap.html.twig.

See also

sitemap.html.twig

File

./sitemap.theme.inc, line 117
Sitemap theme functions.

Code

function template_preprocess_sitemap(array &$variables) {
  $config = \Drupal::config('sitemap.settings');
  $message = $config
    ->get('message.value');
  if (!empty($message)) {
    $variables['message'] = [
      '#markup' => $message,
    ];
  }
  if ($config
    ->get('show_titles')) {
    $variables['show_titles'] = TRUE;
  }
  $variables['sitemap_items'] = [];
  $sitemap_order = $config
    ->get('order');
  asort($sitemap_order);
  foreach ($sitemap_order as $content => $weight) {

    // Get type of content.
    $type = substr($content, 0, strpos($content, '_'));
    $id = substr($content, strpos($content, '_') + 1);
    if (empty($type)) {
      $type = $content;
      $id = NULL;
    }
    switch ($type) {
      case 'front':
        if ($config
          ->get('show_front')) {
          $variables['sitemap_items']['front'] = _sitemap_front_page();
        }
        break;
      case 'books':
        $books = $config
          ->get('show_books');
        if (!empty($books)) {
          $variables['sitemap_items']['books'] = _sitemap_books();
        }
        break;
      case 'menus':
        $menus = array_filter($config
          ->get('show_menus'));
        if (!empty($menus[$id])) {
          $variables['sitemap_items']['menu_' . $id] = _sitemap_menus($id);
        }
        break;
      case 'vocabularies':
        $vocabulary = \Drupal::entityManager()
          ->getStorage('taxonomy_vocabulary')
          ->load($id);
        $vocabularies = $config
          ->get('show_vocabularies');
        if ($vocabulary && !empty($vocabularies[$vocabulary
          ->id()])) {

          // Compile the vocabulary trees.
          $variables['sitemap_items']['vocabulary_' . $vocabulary
            ->id()] = \Drupal::service('sitemap.helper')
            ->getTerms($vocabulary);
        }
        break;
    }
  }

  // Invoke all custom modules and integrate themed HTML into the sitemap.
  $additional = \Drupal::service('module_handler')
    ->invokeAll('sitemap');
  foreach ($additional as $themed_sitemap_code) {
    $variables['additional'] = $themed_sitemap_code;
  }
}