You are here

protected function TaxonomyMenu::buildMenuDefinition in Taxonomy menu 8.3

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.

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

Return value

array The menu link plugin definition.

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

File

src/Entity/TaxonomyMenu.php, line 252

Class

TaxonomyMenu
Defines the TaxonomyMenu entity.

Namespace

Drupal\taxonomy_menu\Entity

Code

protected function buildMenuDefinition(TermInterface $term, array $base_plugin_definition) {
  $term_id = $term
    ->id();
  $term_url = $term
    ->toUrl();
  $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
    ->entityTypeManager()
    ->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]);
  }

  // Note:
  // if menu_parent_id is NULL, it will not update the hierarchy properly.
  if (empty($menu_parent_id)) {
    $menu_parent_id = str_replace($this
      ->getMenu() . ':', '', $this
      ->getMenuParent());
  }

  // TODO: Consider implementing a forced weight based on taxonomy tree.
  // Generate link.
  $arguments = [
    'taxonomy_term' => $term_id,
  ];
  $link = $base_plugin_definition;
  $link += [
    'id' => $this
      ->buildMenuPluginId($term),
    'title' => $term
      ->label(),
    'description' => $term
      ->getDescription(),
    'menu_name' => $menu_id,
    'expanded' => $this->expanded,
    'metadata' => [
      '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',
  ];

  // Order by terms weight if configured for this taxonomy_menu.
  if ($this
    ->useTermWeightOrder()) {
    $link['weight'] = $term
      ->getWeight();
  }
  \Drupal::moduleHandler()
    ->alter('taxonomy_menu_link', $link, $term);
  return $link;
}