You are here

function _sitemap_menus in Sitemap 8

Helper callback for the sitemap menus.

Return value

array Returns a keyed array with variables for rendering the menu's sitemap box.

1 call to _sitemap_menus()
template_preprocess_sitemap in ./sitemap.theme.inc
Preprocesses the variables for sitemap.html.twig.

File

./sitemap.module, line 158
Provides sitemap functionality.

Code

function _sitemap_menus($mid) {
  $sitemap_box = [];
  $attributes = new Attribute();
  $options = [];
  if (!empty($mid)) {
    $menu = Menu::load($mid);

    // Retrieve the expanded tree.
    $tree = \Drupal::service('sitemap.menu.link_tree')
      ->load($mid, new MenuTreeParameters());
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];
    $tree = \Drupal::service('sitemap.menu.link_tree')
      ->transform($tree, $manipulators);

    // Add an alter hook so that other modules can manipulate the
    // menu tree prior to rendering.
    $alter_mid = preg_replace('/[^a-z0-9_]+/', '_', $mid);
    \Drupal::moduleHandler()
      ->alter([
      'sitemap_menu_tree',
      'sitemap_menu_tree_' . $alter_mid,
    ], $tree, $menu);
    $menu_display = \Drupal::service('sitemap.menu.link_tree')
      ->buildForSitemap($tree);
    $menu_html = \Drupal::service('renderer')
      ->render($menu_display);
    if (!empty($menu_html)) {

      // If the menu does not have a special Sitemap display name provided,
      // supply the default menu name.
      if (!empty($menu
        ->getThirdPartySetting('sitemap', 'sitemap_display_name'))) {
        $title = $menu
          ->getThirdPartySetting('sitemap', 'sitemap_display_name');
      }
      else {
        $title = $menu
          ->label();
      }
      \Drupal::service('sitemap.helper')
        ->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
      $attributes
        ->addClass('sitemap-box-menu', 'sitemap-box-menu-' . $mid);
      $sitemap_box = [
        'title' => $title,
        'content' => $menu_html,
        'attributes' => $attributes,
        'options' => $options,
      ];
    }
  }
  return $sitemap_box;
}