You are here

public function TaxonomyTermTree::loadList in Organigrams 8

Same name and namespace in other branches
  1. 8.2 src/TaxonomyTermTree.php \Drupal\organigrams\TaxonomyTermTree::loadList()

Loads the tree of a vocabulary and puts it in an item list.

Parameters

string $vocabulary: Contains the vocabulary machine name.

Return value

array Renderable array containing an item list.

File

src/TaxonomyTermTree.php, line 115

Class

TaxonomyTermTree
Loads taxonomy terms in a tree.

Namespace

Drupal\organigrams

Code

public function loadList($vocabulary) {

  // Get all taxonomy terms.
  $terms = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadTree($vocabulary, 0, NULL, TRUE);

  // Get all taxonomy term fields.
  $fields = $this->entityFieldManager
    ->getFieldDefinitions('taxonomy_term', $vocabulary);

  // Put all fields starting with 'field_o_' in an array.
  $organigram_fields = [];
  foreach ($fields as $field_name => $field) {
    if (substr($field_name, 0, 8) == 'field_o_') {
      $organigram_fields[$field_name] = $field;
    }
  }

  // Build a hierarchical taxonomy term array.
  $items = [];
  foreach ($terms as $tree_object) {
    $this
      ->buildListTree($items, $tree_object, $vocabulary, $organigram_fields);
  }

  // Return an item list.
  return [
    '#theme' => 'item_list',
    '#type' => 'ul',
    '#wrapper_attributes' => [
      'class' => [
        'organigram',
        'organigram-' . $vocabulary,
      ],
    ],
    '#items' => $items,
  ];
}