class Query

Same name in this branch

Defines the entity query for configuration entities.

Hierarchy

Expanded class hierarchy of Query

4 string references to 'Query'
QueryTest::getInfo in drupal/core/modules/views/lib/Drupal/views/Tests/Plugin/QueryTest.php
views.data_types.schema.yml in drupal/core/modules/views/config/schema/views.data_types.schema.yml
drupal/core/modules/views/config/schema/views.data_types.schema.yml
ViewUI::renderPreview in drupal/core/modules/views_ui/lib/Drupal/views_ui/ViewUI.php
_drupal_decode_exception in drupal/core/includes/errors.inc
Decodes an exception and retrieves the correct caller.

File

drupal/core/lib/Drupal/Core/Config/Entity/Query/Query.php, line 19
Contains \Drupal\Core\Config\Entity\Query\Query.

Namespace

Drupal\Core\Config\Entity\Query
View source
class Query extends QueryBase implements QueryInterface {

  /**
   * Stores the entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManager
   */
  protected $entityManager;

  /**
   * The config storage used by the config entity query.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $configStorage;

  /**
   * Constructs a Query object.
   *
   * @param string $entity_type
   *   The entity type.
   * @param string $conjunction
   *   - AND: all of the conditions on the query need to match.
   *   - OR: at least one of the conditions on the query need to match.
   * @param \Drupal\Core\Entity\EntityManager $entity_manager
   *   The entity manager that stores all meta information.
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The actual config storage which is used to list all config items.
   */
  function __construct($entity_type, $conjunction, EntityManager $entity_manager, StorageInterface $config_storage) {
    parent::__construct($entity_type, $conjunction);
    $this->entityManager = $entity_manager;
    $this->configStorage = $config_storage;
  }

  /**
   * Implements \Drupal\Core\Entity\Query\QueryInterface::conditionGroupFactory().
   */
  public function conditionGroupFactory($conjunction = 'AND') {
    return new Condition($conjunction);
  }

  /**
   * Overrides \Drupal\Core\Entity\Query\QueryBase::condition().
   *
   * Additional to the syntax defined in the QueryInterface you can use
   * placeholders (*) to match all keys of an subarray. Let's take the follow
   * yaml file as example:
   * @code
   *  level1:
   *    level2a:
   *      level3: 1
   *    level2b:
   *      level3: 2
   * @endcode
   * Then you can filter out via $query->condition('level1.*.level3', 1).
   */
  public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
    return parent::condition($property, $value, $operator, $langcode);
  }

  /**
   * Implements \Drupal\Core\Entity\Query\QueryInterface::execute().
   */
  public function execute() {

    // Load all config files.
    $entity_info = $this->entityManager
      ->getDefinition($this
      ->getEntityType());
    $prefix = $entity_info['config_prefix'];
    $prefix_length = strlen($prefix) + 1;
    $names = $this->configStorage
      ->listAll($prefix);
    $configs = array();
    foreach ($names as $name) {
      $configs[substr($name, $prefix_length)] = config($name)
        ->get();
    }
    $result = $this->condition
      ->compile($configs);

    // Apply sort settings.
    foreach ($this->sort as $sort) {
      $direction = $sort['direction'] == 'ASC' ? -1 : 1;
      $field = $sort['field'];
      uasort($result, function ($a, $b) use ($field, $direction) {
        return $a[$field] <= $b[$field] ? $direction : -$direction;
      });
    }

    // Let the pager do its work.
    $this
      ->initializePager();
    if ($this->range) {
      $result = array_slice($result, $this->range['start'], $this->range['length'], TRUE);
    }
    if ($this->count) {
      return count($result);
    }

    // Create the expected structure of entity_id => entity_id. Config
    // entities have string entity IDs.
    foreach ($result as $key => &$value) {
      $value = (string) $key;
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Query::$configStorage protected property The config storage used by the config entity query.
Query::$entityManager protected property Stores the entity manager.
Query::condition public function Overrides \Drupal\Core\Entity\Query\QueryBase::condition(). Overrides QueryBase::condition
Query::conditionGroupFactory public function Implements \Drupal\Core\Entity\Query\QueryInterface::conditionGroupFactory(). Overrides QueryInterface::conditionGroupFactory
Query::execute public function Implements \Drupal\Core\Entity\Query\QueryInterface::execute(). Overrides QueryInterface::execute
Query::__construct function Constructs a Query object. Overrides QueryBase::__construct
QueryBase::$accessCheck protected property Whether access check is requested or not. Defaults to TRUE.
QueryBase::$age protected property Flag indicating whether to query the current revision or all revisions.
QueryBase::$aggregate protected property The list of aggregate expressions.
QueryBase::$alterMetaData protected property The query metadata for alter purposes.
QueryBase::$alterTags protected property The query tags.
QueryBase::$condition protected property Conditions. 1
QueryBase::$conditionAggregate protected property Aggregate Conditions
QueryBase::$count protected property TRUE if this is a count query, FALSE if it isn't.
QueryBase::$entityType protected property The entity type this query runs against.
QueryBase::$groupBy protected property The list of columns to group on.
QueryBase::$pager protected property The query pager data.
QueryBase::$range protected property The query range.
QueryBase::$sort protected property The list of sorts.
QueryBase::$sortAggregate protected property The list of sorts over the aggregate results.
QueryBase::accessCheck public function Implements \Drupal\Core\Entity\Query\QueryInterface::accessCheck().
QueryBase::addMetaData public function Implements \Drupal\Core\Database\Query\AlterableInterface::addMetaData().
QueryBase::addTag public function Implements \Drupal\Core\Database\Query\AlterableInterface::addTag().
QueryBase::age public function Implements \Drupal\Core\Entity\Query\QueryInterface::age().
QueryBase::aggregate public function Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::aggregate()
QueryBase::andConditionGroup public function Implements \Drupal\Core\Entity\Query\QueryInterface::andConditionGroup().
QueryBase::conditionAggregate public function Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::conditionAggregate().
QueryBase::count public function Implements \Drupal\Core\Entity\Query\QueryInterface::count().
QueryBase::exists public function Implements \Drupal\Core\Entity\Query\QueryInterface::exists().
QueryBase::getAggregationAlias protected function Generates an alias for a field and it's aggregated function.
QueryBase::getEntityType public function Implements \Drupal\Core\Entity\Query\QueryInterface::getEntityType().
QueryBase::getMetaData public function Implements \Drupal\Core\Database\Query\AlterableInterface::getMetaData().
QueryBase::groupBy public function Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::execute().
QueryBase::hasAllTags public function Implements \Drupal\Core\Database\Query\AlterableInterface::hasAllTags().
QueryBase::hasAnyTag public function Implements \Drupal\Core\Database\Query\AlterableInterface::hasAnyTag().
QueryBase::hasTag public function Implements \Drupal\Core\Database\Query\AlterableInterface::hasTag().
QueryBase::initializePager protected function Gets the total number of results and initialize a pager for the query.
QueryBase::notExists public function Implements \Drupal\Core\Entity\Query\QueryInterface::notExists().
QueryBase::orConditionGroup public function Implements \Drupal\Core\Entity\Query\QueryInterface::orConditionGroup().
QueryBase::pager public function Implements \Drupal\Core\Entity\Query\QueryInterface::pager().
QueryBase::range public function Implements \Drupal\Core\Entity\Query\QueryInterface::range().
QueryBase::sort public function Implements \Drupal\Core\Entity\Query\QueryInterface::sort().
QueryBase::sortAggregate public function Implements \Drupal\Core\Entity\Query\QueryAggregateInterface::sortAggregate().
QueryBase::tableSort public function Implements \Drupal\Core\Entity\Query\QueryInterface::tableSort().
QueryBase::__clone function Makes sure that the Condition object is cloned as well. 1