You are here

public static function CheeseburgerMenuItem::createFromTaxonomyVocabulary in Cheeseburger Menu 5.0.x

Creates cheeseburger menu item from taxonomy term.

Parameters

\Drupal\taxonomy\Entity\Term $taxonomy_term: Taxonomy term.

array $menu_item_settings: @see \Drupal\cheeseburger_menu\CheeseburgerMenuItem::applySettings()

int $max_depth: Max depth children.

int $current_depth: Current depth, not intended to be be used outside of function.

boolean|string $langcode: Langcode, in case equals FALSE, translation will not be used.

Return value

static Created cheeseburger menu item.

1 call to CheeseburgerMenuItem::createFromTaxonomyVocabulary()
CheeseburgerMenuService::buildMenuFromVocabulary in src/CheeseburgerMenuService.php
Returns cheeseburger menu from taxonomy vocabulary.

File

src/CheeseburgerMenuItem.php, line 158

Class

CheeseburgerMenuItem
Cheeseburger menu item class.

Namespace

Drupal\cheeseburger_menu

Code

public static function createFromTaxonomyVocabulary(Term $taxonomy_term, array $menu_item_settings = [], $max_depth = 0, $current_depth = 1, $langcode = FALSE) {
  $attribute = new Attribute([
    'class' => [
      'cheeseburger-menu__item',
      Html::cleanCssIdentifier($taxonomy_term
        ->bundle()) . '__item',
    ],
  ]);
  if ($langcode && $taxonomy_term
    ->hasTranslation($langcode)) {
    $taxonomy_term = $taxonomy_term
      ->getTranslation($langcode);
  }
  $menu_link_item_instance = new static($taxonomy_term
    ->label(), $taxonomy_term
    ->toUrl(), $attribute);
  foreach (\Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->getChildren($taxonomy_term) as $child_taxonomy_term) {
    if ($child_taxonomy_term
      ->access('view') && (empty($max_depth) || $current_depth < $max_depth)) {
      $menu_link_item_instance
        ->addChild(self::createFromTaxonomyVocabulary($child_taxonomy_term, $menu_item_settings, $max_depth, $current_depth + 1, $langcode));
    }
  }
  $menu_link_item_instance
    ->applySettings($menu_item_settings)
    ->setOriginalEntityTypeId($taxonomy_term
    ->getEntityTypeId())
    ->setOriginalEntityId($taxonomy_term
    ->id());
  if (in_array($taxonomy_term
    ->id(), $menu_item_settings['active_trail'])) {
    $menu_link_item_instance
      ->setIsInActiveTrail();
  }
  return $menu_link_item_instance;
}