You are here

public function Menu::view in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Sitemap/Menu.php \Drupal\sitemap\Plugin\Sitemap\Menu::view()

Builds a renderable array for a sitemap item.

Return value

array A renderable array for a themed field with its label and all its values.

Overrides SitemapInterface::view

File

src/Plugin/Sitemap/Menu.php, line 52

Class

Menu
Provides a sitemap for an individual menu.

Namespace

Drupal\sitemap\Plugin\Sitemap

Code

public function view() {
  $menuLinkTree = \Drupal::service('sitemap.menu.link_tree');
  $menu_id = $this->pluginDefinition['menu'];
  $menu = MenuEntity::load($menu_id);

  // Retrieve the expanded tree.
  $parameters = new MenuTreeParameters();
  if (!$this->settings['show_disabled']) {
    $parameters
      ->onlyEnabledLinks();
  }
  $tree = $menuLinkTree
    ->load($menu_id, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $menuLinkTree
    ->transform($tree, $manipulators);

  // Add an alter hook so that other modules can manipulate the
  // menu tree prior to rendering.
  // @TODO: Document
  $alter_mid = preg_replace('/[^a-z0-9_]+/', '_', $menu_id);
  \Drupal::moduleHandler()
    ->alter([
    'sitemap_menu_tree',
    'sitemap_menu_tree_' . $alter_mid,
  ], $tree, $menu);
  $menu_display = $menuLinkTree
    ->build($tree);
  return [
    '#theme' => 'sitemap_item',
    '#title' => $this->settings['title'],
    '#content' => $menu_display,
    '#sitemap' => $this,
  ];
}