You are here

trait AncestryLabelTrait in Entity Reference Hierarchy 8.2

Same name and namespace in other branches
  1. 3.x src/Information/AncestryLabelTrait.php \Drupal\entity_hierarchy\Information\AncestryLabelTrait

Provides a trait for ancestry labels.

Hierarchy

2 files declare their use of AncestryLabelTrait
EntityHierarchy.php in src/Plugin/EntityReferenceSelection/EntityHierarchy.php
EntityHierarchy.php in modules/entity_hierarchy_workbench_access/src/Plugin/AccessControlHierarchy/EntityHierarchy.php

File

src/Information/AncestryLabelTrait.php, line 10

Namespace

Drupal\entity_hierarchy\Information
View source
trait AncestryLabelTrait {

  /**
   * Tree node mapper.
   *
   * @var \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface
   */
  protected $entityTreeNodeMapper;

  /**
   * Key factory.
   *
   * @var \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory
   */
  protected $keyFactory;

  /**
   * Generate labels including ancestry.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Entity to generate label for.
   * @param \PNX\NestedSet\NestedSetInterface|\Drupal\entity_hierarchy\Storage\NestedSetStorage $storage
   *   Tree storage.
   * @param string $entity_type_id
   *   Entity type ID.
   * @param array $tags
   *   Cache tags.
   *
   * @return string
   *   Label with ancestry if applicable.
   */
  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));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AncestryLabelTrait::$entityTreeNodeMapper protected property Tree node mapper.
AncestryLabelTrait::$keyFactory protected property Key factory.
AncestryLabelTrait::generateEntityLabelWithAncestry protected function Generate labels including ancestry.