You are here

protected function Query::getFromStorage in Apigee Edge 8

Loads entities from the entity storage for querying.

Return value

\Drupal\Core\Entity\EntityInterface[] Array of matching entities.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

2 calls to Query::getFromStorage()
AppQueryBase::getFromStorage in src/Entity/Query/AppQueryBase.php
Loads entities from the entity storage for querying.
Query::execute in src/Entity/Query/Query.php
Execute the query.
1 method overrides Query::getFromStorage()
AppQueryBase::getFromStorage in src/Entity/Query/AppQueryBase.php
Loads entities from the entity storage for querying.

File

src/Entity/Query/Query.php, line 151

Class

Query
Defines the entity query for Apigee Edge entities.

Namespace

Drupal\apigee_edge\Entity\Query

Code

protected function getFromStorage() : array {
  $storage = $this->entityTypeManager
    ->getStorage($this->entityTypeId);

  // The worst case: load all entities from Apigee Edge.
  $ids = NULL;
  $original_conditions =& $this->condition
    ->conditions();
  $filtered_conditions = [];
  foreach ($original_conditions as $key => $condition) {
    $filtered_conditions[$key] = $condition;
    $id = NULL;

    // Indicates whether we found a single entity id in this condition
    // or not.
    $id_found = FALSE;

    // \Drupal\Core\Entity\EntityStorageBase::buildPropertyQuery() always adds
    // conditions with IN this is the reason why the last part of this
    // condition is needed.
    if (in_array($condition['field'], $this
      ->getEntityIdProperties()) && (in_array($condition['operator'], [
      NULL,
      '=',
    ]) || $condition['operator'] === 'IN' && is_array($condition['value']) && count($condition['value']) === 1)) {
      if (is_array($condition['value'])) {
        $id = reset($condition['value']);
        $id_found = TRUE;
      }
      else {
        $id = $condition['value'];
        $id_found = TRUE;
      }
    }

    // We have to handle propertly when a developer probably unintentionally
    // passed an empty value (null, false, "", etc.) as a value of a condition
    // for a primary entity id. In this case we should return empty result
    // immediately because this condition can not be evaluated Apigee Edge
    // and we should not load all entities unnecessarily to get same result
    // after filtered the results in the PHP side.
    if ($id_found) {
      if (empty($id)) {
        return [];
      }
      else {
        $ids = [
          $id,
        ];
        unset($filtered_conditions[$key]);

        // If we found an id field in the query do not look for an another
        // because that would not make any sense to query one entity by
        // both id fields. (Where in theory both id field could refer to a
        // different entity.)
        break;
      }
    }
  }

  // Remove conditions that is going to be applied on Apigee Edge
  // (by calling the proper API with the proper parameters).
  // We do not want to apply the same filters on the result in execute()
  // again.
  $original_conditions = $filtered_conditions;
  return $storage
    ->loadMultiple($ids);
}