You are here

public function RenderCheeseburgerMenuBlock::findVocabularyChild in Cheeseburger Menu 8.4

Searches for vocabulary child.

1 call to RenderCheeseburgerMenuBlock::findVocabularyChild()
RenderCheeseburgerMenuBlock::getVocabularyTree in src/Controller/RenderCheeseburgerMenuBlock.php
Formats vocabulary.

File

src/Controller/RenderCheeseburgerMenuBlock.php, line 443
Controller used for rendering block.

Class

RenderCheeseburgerMenuBlock
Class RenderCheeseburgerMenuBlock.

Namespace

Drupal\cheeseburger_menu\Controller

Code

public function findVocabularyChild($term) {
  $icon = FALSE;
  if ($term
    ->hasField('field_icon')) {
    if (!empty($term
      ->get('field_icon')
      ->getValue())) {
      $icon = $term
        ->get('field_icon')->entity
        ->getFileUri();
      $icon = file_create_url($icon);
      $icon = file_url_transform_relative($icon);
    }
  }
  $langcode = $this
    ->languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $translation_languages = $term
    ->getTranslationLanguages();
  if (array_key_exists($langcode, $translation_languages)) {
    $translation = $term
      ->getTranslation($langcode);
  }
  else {
    $translation = $term;
  }
  $term_tree = [
    'id' => $translation
      ->get('tid')->value,
    'title' => $translation
      ->getName(),
    'url' => $translation
      ->url(),
    'entity_type_id' => $translation
      ->getEntityTypeId(),
    'icon' => $icon,
    'children' => [],
  ];
  $ancestors = $this
    ->entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadChildren($term
    ->get('tid')->value);
  foreach ($ancestors as $ancestor) {
    $term_tree['children'][] = $this
      ->findVocabularyChild($ancestor);
  }
  return $term_tree;
}