You are here

protected function AncestryLabelTrait::generateEntityLabelWithAncestry in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Information/AncestryLabelTrait.php \Drupal\entity_hierarchy\Information\AncestryLabelTrait::generateEntityLabelWithAncestry()

Generate labels including ancestry.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: Entity to generate label for.

\PNX\NestedSet\NestedSetInterface|\Drupal\entity_hierarchy\Storage\NestedSetStorage $storage: Tree storage.

string $entity_type_id: Entity type ID.

array $tags: Cache tags.

Return value

string Label with ancestry if applicable.

2 calls to AncestryLabelTrait::generateEntityLabelWithAncestry()
EntityHierarchy::getReferenceableEntities in src/Plugin/EntityReferenceSelection/EntityHierarchy.php
Gets the list of referenceable entities.
EntityHierarchy::getTree in modules/entity_hierarchy_workbench_access/src/Plugin/AccessControlHierarchy/EntityHierarchy.php

File

src/Information/AncestryLabelTrait.php, line 41

Class

AncestryLabelTrait
Provides a trait for ancestry labels.

Namespace

Drupal\entity_hierarchy\Information

Code

protected function generateEntityLabelWithAncestry(ContentEntityInterface $entity, $storage, $entity_type_id, &$tags = []) {
  $key = $this->keyFactory
    ->fromEntity($entity);
  $ancestors = $storage
    ->findAncestors($key);

  // Remove ourself.
  array_pop($ancestors);
  $ancestor_entities = $this->entityTreeNodeMapper
    ->loadAndAccessCheckEntitysForTreeNodes($entity_type_id, $ancestors);
  $ancestors_labels = [];
  foreach ($ancestor_entities as $ancestor_node) {
    if (!$ancestor_entities
      ->contains($ancestor_node)) {

      // Doesn't exist or is access hidden.
      continue;
    }
    $ancestor_entity = $ancestor_entities
      ->offsetGet($ancestor_node);
    $ancestors_labels[] = $ancestor_entity
      ->label();
    foreach ($ancestor_entity
      ->getCacheTags() as $tag) {
      $tags[] = $tag;
    }
  }
  if (!$ancestors || !$ancestors_labels) {

    // No parents.
    return $entity
      ->label();
  }
  return sprintf('%s (%s)', $entity
    ->label(), implode(' ❭ ', $ancestors_labels));
}