You are here

protected function TaxonomyMenu::buildMenuDefinition in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 modules/taxonomy_menu/src/Entity/TaxonomyMenu.php \Drupal\taxonomy_menu\Entity\TaxonomyMenu::buildMenuDefinition()

Generate a menu link plugin definition for a taxonomy term.

Parameters

\Drupal\taxonomy\TermInterface $term: The taxonomy term for which to build a menu link render array.

$base_plugin_definition: The base plugin definition to merge the link with.

Return value

array

1 call to TaxonomyMenu::buildMenuDefinition()
TaxonomyMenu::getLinks in modules/taxonomy_menu/src/Entity/TaxonomyMenu.php
Get menu link plugin definitions

File

modules/taxonomy_menu/src/Entity/TaxonomyMenu.php, line 181
Contains Drupal\taxonomy_menu\Entity\TaxonomyMenu.

Class

TaxonomyMenu
Defines the TaxonomyMenu entity.

Namespace

Drupal\taxonomy_menu\Entity

Code

protected function buildMenuDefinition(TermInterface $term, $base_plugin_definition) {
  $term_id = $term
    ->id();
  $term_url = $term
    ->urlInfo();
  $taxonomy_menu_id = $this
    ->id();
  $menu_id = $this
    ->getMenu();

  // Determine parent link.
  // TODO: Evaluate use case of multiple parents (should we make many menu items?)
  $menu_parent_id = NULL;

  /** @var $termStorage \Drupal\taxonomy\TermStorageInterface */
  $termStorage = $this
    ->entityManager()
    ->getStorage('taxonomy_term');
  $parents = $termStorage
    ->loadParents($term_id);
  $parents = array_values($parents);
  if (is_array($parents) && count($parents) && !is_null($parents[0]) && $parents[0] != '0') {
    $menu_parent_id = $this
      ->buildMenuPluginId($parents[0]);
  }

  // TODO: Consider implementing a forced weight based on taxonomy tree.
  // Generate link.
  $arguments = [
    'taxonomy_term' => $term_id,
  ];
  $link = $base_plugin_definition;
  $link += array(
    'id' => $this
      ->buildMenuPluginId($term),
    'title' => $term
      ->label(),
    'description' => $term
      ->getDescription(),
    'menu_name' => $menu_id,
    'metadata' => array(
      'taxonomy_menu_id' => $taxonomy_menu_id,
      'taxonomy_term_id' => $term_id,
    ),
    'route_name' => $term_url
      ->getRouteName(),
    'route_parameters' => $term_url
      ->getRouteParameters(),
    'load arguments' => $arguments,
    'parent' => $menu_parent_id,
    'provider' => 'taxonomy_menu',
    'class' => 'Drupal\\taxonomy_menu\\Plugin\\Menu\\TaxonomyMenuMenuLink',
  );
  return $link;
}