function EntityFetchByField::doExecute

Executes the action with the given context.

Parameters

string $type: The entity type id.

string $field_name: Name of the field by which the entity is to be selected.

mixed $field_value: The field value of the entity to be fetched.

int $limit: Limit on the maximum number of fetched entities.

File

src/Plugin/RulesAction/EntityFetchByField.php, line 108

Class

EntityFetchByField
Provides a 'Fetch entities by field' action.

Namespace

Drupal\rules\Plugin\RulesAction

Code

protected function doExecute($type, $field_name, $field_value, $limit = NULL) {
  $storage = $this->entityTypeManager
    ->getStorage($type);
  // When retrieving entities, if $limit is not set there is no need to use
  // the query object directly.
  if (is_null($limit)) {
    $entities = $storage->loadByProperties([
      $field_name => $field_value,
    ]);
  }
  else {
    $query = $storage->getQuery();
    $entity_ids = $query->accessCheck(TRUE)
      ->condition($field_name, $field_value, '=')
      ->range(0, $limit)
      ->execute();
    $entities = $storage->loadMultiple($entity_ids);
  }
  // Set provided value.
  // @todo Ensure that the provided context has the correct entity type.
  $this->setProvidedValue('entity_fetched', $entities);
}