You are here

public function Taxonomy::getTree in Workbench Access 8

Gets the entire hierarchy tree.

This method will return a hierarcy tree from any supported source in a standard array structure. Using this method allows our code to abstract handling of access controls.

The array has the following components.

id - The lookup id of the entity or object (e.g. term tid). parents - A sorted array of ids for any parent items of this item. label - The human-readable label of the entity or object. description - A human-readable help description of this item. path - A fully-formed URL string for this item. depth - The depth in the hierarchy of this item. weight - The sort order (weight) of this item at its depth.

The first two items in this array (id, parents) are used to generate access control logic. The remaining items are used for building forms and user interfaces. Note that the last two items (depth, weight) are normally handled by the sorting done by the tree builder. They are provided in case your code needs to re-sort the tree.

Return value

array An array in the format defined above.

Overrides AccessControlHierarchyBase::getTree

File

src/Plugin/AccessControlHierarchy/Taxonomy.php, line 95

Class

Taxonomy
Defines a hierarchy based on a Vocabulary.

Namespace

Drupal\workbench_access\Plugin\AccessControlHierarchy

Code

public function getTree() {
  if (!isset($this->tree)) {
    $this->tree = [];

    /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
    $term_storage = $this->entityTypeManager
      ->getStorage('taxonomy_term');
    $tree = [];
    foreach ($this->configuration['vocabularies'] as $vocabulary_id) {
      if ($vocabulary = Vocabulary::load($vocabulary_id)) {
        $tree[$vocabulary_id][$vocabulary_id] = [
          'label' => $vocabulary
            ->label(),
          'depth' => 0,
          'parents' => [],
          'weight' => 0,
          'description' => $vocabulary
            ->label(),
          'path' => $vocabulary
            ->toUrl('overview-form')
            ->toString(),
        ];

        // @TODO: It is possible that this will return a filtered set, if
        // term_access is applied to the query.
        $data = $term_storage
          ->loadTree($vocabulary_id);
        $this->tree = $this
          ->buildTree($vocabulary_id, $data, $tree);
      }
    }
  }
  return $this->tree;
}