Same filename in this branch
- 10 core/modules/workspaces/src/EntityQuery/Query.php
- 10 core/lib/Drupal/Core/Database/Query/Query.php
- 10 core/lib/Drupal/Core/Config/Entity/Query/Query.php
- 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/Query.php
- 10 core/lib/Drupal/Core/Entity/Query/Null/Query.php
- 10 core/lib/Drupal/Core/Entity/Query/Sql/Query.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Config/Entity/Query/Query.php
- 9 core/lib/Drupal/Core/Config/Entity/Query/Query.php
Namespace
Drupal\Core\Config\Entity\Query
File
core/lib/Drupal/Core/Config/Entity/Query/Query.php
View source
<?php
namespace Drupal\Core\Config\Entity\Query;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Query\QueryBase;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
class Query extends QueryBase implements QueryInterface {
protected $entityType;
protected $configFactory;
protected $keyValueFactory;
public function __construct(EntityTypeInterface $entity_type, $conjunction, ConfigFactoryInterface $config_factory, KeyValueFactoryInterface $key_value_factory, array $namespaces) {
parent::__construct($entity_type, $conjunction, $namespaces);
$this->configFactory = $config_factory;
$this->keyValueFactory = $key_value_factory;
}
public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
return parent::condition($property, $value, $operator, $langcode);
}
public function execute() {
$this
->alter();
$configs = $this
->loadRecords();
$result = $this->condition
->compile($configs);
foreach ($this->sort as $sort) {
$direction = $sort['direction'] == 'ASC' ? -1 : 1;
$field = $sort['field'];
uasort($result, function ($a, $b) use ($field, $direction) {
$properties = explode('.', $field);
foreach ($properties as $property) {
if (isset($a[$property]) || isset($b[$property])) {
$a = $a[$property] ?? NULL;
$b = $b[$property] ?? NULL;
}
}
return $a <= $b ? $direction : -$direction;
});
}
$this
->initializePager();
if ($this->range) {
$result = array_slice($result, $this->range['start'], $this->range['length'], TRUE);
}
if ($this->count) {
return count($result);
}
foreach ($result as $key => &$value) {
$value = (string) $key;
}
return $result;
}
protected function loadRecords() {
$prefix = $this->entityType
->getConfigPrefix() . '.';
$prefix_length = strlen($prefix);
$filter_by_names = [];
$has_added_restrictions = FALSE;
$id_condition = NULL;
$id_key = $this->entityType
->getKey('id');
if ($this->condition
->getConjunction() == 'AND') {
$lookup_keys = $this->entityType
->getLookupKeys();
$conditions = $this->condition
->conditions();
foreach ($conditions as $condition_key => $condition) {
$operator = $condition['operator'] ?: (is_array($condition['value']) ? 'IN' : '=');
if (is_string($condition['field']) && ($operator == 'IN' || $operator == '=')) {
if ($condition['field'] == $id_key) {
$has_added_restrictions = TRUE;
$ids = (array) $condition['value'];
$filter_by_names[] = array_map(static function ($id) use ($prefix) {
return $prefix . $id;
}, $ids);
}
elseif (in_array($condition['field'], $lookup_keys)) {
$has_added_restrictions = TRUE;
$keys = (array) $condition['value'];
$keys = array_map(static function ($value) use ($condition) {
return $condition['field'] . ':' . $value;
}, $keys);
foreach ($this
->getConfigKeyStore()
->getMultiple($keys) as $list) {
$filter_by_names[] = $list;
}
}
}
elseif (!$id_condition && $condition['field'] == $id_key) {
$id_condition = $condition;
}
if ($has_added_restrictions !== FALSE) {
unset($conditions[$condition_key]);
break;
}
}
}
if ($has_added_restrictions === FALSE) {
$filter_by_names = $this->configFactory
->listAll($prefix);
}
else {
$filter_by_names = array_merge(...$filter_by_names);
}
if ($id_condition && !empty($filter_by_names)) {
$value = $id_condition['value'];
$filter = NULL;
switch ($id_condition['operator']) {
case '<>':
$filter = static function ($name) use ($value, $prefix_length) {
$id = substr($name, $prefix_length);
return $id !== $value;
};
break;
case 'STARTS_WITH':
$filter = static function ($name) use ($value, $prefix_length) {
$id = substr($name, $prefix_length);
return str_starts_with($id, $value);
};
break;
case 'CONTAINS':
$filter = static function ($name) use ($value, $prefix_length) {
$id = substr($name, $prefix_length);
return str_contains($id, $value);
};
break;
case 'ENDS_WITH':
$filter = static function ($name) use ($value, $prefix_length) {
$id = substr($name, $prefix_length);
return str_ends_with($id, $value);
};
break;
}
if ($filter) {
$filter_by_names = array_filter($filter_by_names, $filter);
}
}
$records = [];
foreach ($this->configFactory
->loadMultiple($filter_by_names) as $config) {
$records[substr($config
->getName(), $prefix_length)] = $config
->get();
}
return $records;
}
protected function getConfigKeyStore() {
return $this->keyValueFactory
->get(QueryFactory::CONFIG_LOOKUP_PREFIX . $this->entityTypeId);
}
}
Classes
Name |
Description |
Query |
Defines the entity query for configuration entities. |