You are here

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

Same name and namespace in other branches
  1. 8.2 src/Plugin/EntityReferenceSelection/EntityHierarchy.php \Drupal\entity_hierarchy\Plugin\EntityReferenceSelection\EntityHierarchy::getReferenceableEntities()

Gets the list of referenceable entities.

Parameters

string|null $match: (optional) Text to match the label against. Defaults to NULL.

string $match_operator: (optional) Operator to be used for string matching. Defaults to "CONTAINS".

int $limit: (optional) Limit the query to a given number of items. Defaults to 0, which indicates no limiting.

Return value

array A nested array of entities, the first level is keyed by the entity bundle, which contains an array of entity labels (escaped), keyed by the entity ID.

Overrides DefaultSelection::getReferenceableEntities

File

src/Plugin/EntityReferenceSelection/EntityHierarchy.php, line 53

Class

EntityHierarchy
Defines a class for entity reference selection that includes lineage.

Namespace

Drupal\entity_hierarchy\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  $target_type = $this->configuration['target_type'];
  $query = $this
    ->buildEntityQuery($match, $match_operator);
  if ($limit > 0) {
    $query
      ->range(0, $limit);
  }
  $result = $query
    ->execute();
  if (empty($result)) {
    return [];
  }
  $options = [];
  $entities = $this->entityTypeManager
    ->getStorage($target_type)
    ->loadMultiple($result);

  // We assume target and definition are one and the same, as there is no
  // point in a hierarchy if you're referencing something else, you can't
  // go more than one level deep.

  /** @var \PNX\NestedSet\NestedSetInterface $storage */
  $storage = $this->nestedSetStorageFactory
    ->get($this->pluginDefinition['field_name'], $target_type);
  foreach ($entities as $entity_id => $entity) {
    $bundle = $entity
      ->bundle();
    $label = $this
      ->generateEntityLabelWithAncestry($entity, $storage, $target_type);
    $options[$bundle][$entity_id] = Html::escape($label);
  }
  return $options;
}