You are here

public function BusinessRulesItemSelection::getReferenceableEntities in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/EntityReferenceSelection/BusinessRulesItemSelection.php \Drupal\business_rules\Plugin\EntityReferenceSelection\BusinessRulesItemSelection::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/BusinessRulesItemSelection.php, line 27

Class

BusinessRulesItemSelection
Provides specific access control for the Business Rules item entity type.

Namespace

Drupal\business_rules\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
  $target_type = $this
    ->getConfiguration()['target_type'];
  $query = parent::buildEntityQuery($match, $match_operator);

  // Here is the magic.
  $handler_settings = $this->configuration['handler_settings'];
  if (isset($handler_settings['filter'])) {
    $filter_settings = $handler_settings['filter'];
    foreach ($filter_settings as $field_name => $value) {
      $query
        ->condition($field_name, $value, '=');
    }
  }
  if ($limit > 0) {
    $query
      ->range(0, $limit);
  }
  $result = $query
    ->execute();
  if (empty($result)) {
    return [];
  }
  $options = [];
  $entities = $this->entityManager
    ->getStorage($target_type)
    ->loadMultiple($result);
  foreach ($entities as $entity_id => $entity) {
    $bundle = $entity
      ->bundle();
    $options[$bundle][$entity_id] = Html::escape($this->entityManager
      ->getTranslationFromContext($entity)
      ->label());
  }
  return $options;
}