You are here

public function EntityHierarchy::getEntityValues in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 modules/entity_hierarchy_workbench_access/src/Plugin/AccessControlHierarchy/EntityHierarchy.php \Drupal\entity_hierarchy_workbench_access\Plugin\AccessControlHierarchy\EntityHierarchy::getEntityValues()

File

modules/entity_hierarchy_workbench_access/src/Plugin/AccessControlHierarchy/EntityHierarchy.php, line 232

Class

EntityHierarchy
Defines a hierarchy based on an entity hierarchy field.

Namespace

Drupal\entity_hierarchy_workbench_access\Plugin\AccessControlHierarchy

Code

public function getEntityValues(EntityInterface $entity) {

  // We start with our own ID.
  $values = [
    $entity
      ->id(),
  ];
  $nodeKey = $this->keyFactory
    ->fromEntity($entity);

  // A top level parent can be part of the tree without having the field.
  if ($entity
    ->hasField($this->pluginDefinition['field_name'])) {
    foreach ($entity
      ->get($this->pluginDefinition['field_name']) as $item) {
      if ($item
        ->isEmpty()) {
        continue;
      }
      $property_name = $item
        ->mainPropertyName();
      $values[] = $item->{$property_name};
      if ($item instanceof EntityReferenceItem && $item->entity) {
        $nodeKey = $this->keyFactory
          ->fromEntity($item->entity);
      }
    }
  }
  $storage = $this->nestedSetStorageFactory
    ->get($this->pluginDefinition['field_name'], $this->pluginDefinition['entity']);
  $ancestors = $storage
    ->findAncestors($nodeKey);
  foreach ($ancestors as $ancestor) {
    $values[] = $ancestor
      ->getId();
  }
  return $values;
}