3 namespace Drupal\Core\Config\Entity\Query;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Entity\EntityTypeInterface;
7 use Drupal\Core\Entity\Query\QueryBase;
8 use Drupal\Core\Entity\Query\QueryInterface;
9 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
12 * Defines the entity query for configuration entities.
14 class Query extends QueryBase implements QueryInterface {
17 * Information about the entity type.
19 * @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
21 protected $entityType;
24 * The config factory used by the config entity query.
26 * @var \Drupal\Core\Config\ConfigFactoryInterface
28 protected $configFactory;
31 * The key value factory.
33 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
35 protected $keyValueFactory;
38 * Constructs a Query object.
40 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
41 * The entity type definition.
42 * @param string $conjunction
43 * - AND: all of the conditions on the query need to match.
44 * - OR: at least one of the conditions on the query need to match.
45 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
47 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
48 * The key value factory.
49 * @param array $namespaces
50 * List of potential namespaces of the classes belonging to this query.
52 public function __construct(EntityTypeInterface $entity_type, $conjunction, ConfigFactoryInterface $config_factory, KeyValueFactoryInterface $key_value_factory, array $namespaces) {
53 parent::__construct($entity_type, $conjunction, $namespaces);
54 $this->configFactory = $config_factory;
55 $this->keyValueFactory = $key_value_factory;
59 * Overrides \Drupal\Core\Entity\Query\QueryBase::condition().
61 * Additional to the syntax defined in the QueryInterface you can use
62 * placeholders (*) to match all keys of an subarray. Let's take the follow
63 * yaml file as example:
71 * Then you can filter out via $query->condition('level1.*.level3', 1).
73 public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
74 return parent::condition($property, $value, $operator, $langcode);
80 public function execute() {
81 // Load the relevant config records.
82 $configs = $this->loadRecords();
85 $result = $this->condition->compile($configs);
87 // Apply sort settings.
88 foreach ($this->sort as $sort) {
89 $direction = $sort['direction'] == 'ASC' ? -1 : 1;
90 $field = $sort['field'];
91 uasort($result, function($a, $b) use ($field, $direction) {
92 return ($a[$field] <= $b[$field]) ? $direction : -$direction;
96 // Let the pager do its work.
97 $this->initializePager();
100 $result = array_slice($result, $this->range['start'], $this->range['length'], TRUE);
103 return count($result);
106 // Create the expected structure of entity_id => entity_id. Config
107 // entities have string entity IDs.
108 foreach ($result as $key => &$value) {
109 $value = (string) $key;
115 * Loads the config records to examine for the query.
118 * Config records keyed by entity IDs.
120 protected function loadRecords() {
121 $prefix = $this->entityType->getConfigPrefix() . '.';
122 $prefix_length = strlen($prefix);
124 // Search the conditions for restrictions on configuration object names.
126 $id_condition = NULL;
127 $id_key = $this->entityType->getKey('id');
128 if ($this->condition->getConjunction() == 'AND') {
129 $lookup_keys = $this->entityType->getLookupKeys();
130 $conditions = $this->condition->conditions();
131 foreach ($conditions as $condition_key => $condition) {
132 $operator = $condition['operator'] ?: (is_array($condition['value']) ? 'IN' : '=');
133 if (is_string($condition['field']) && ($operator == 'IN' || $operator == '=')) {
134 // Special case ID lookups.
135 if ($condition['field'] == $id_key) {
136 $ids = (array) $condition['value'];
137 $names = array_map(function ($id) use ($prefix) {
138 return $prefix . $id;
141 elseif (in_array($condition['field'], $lookup_keys)) {
142 // If we don't find anything then there are no matches. No point in
145 $keys = (array) $condition['value'];
146 $keys = array_map(function ($value) use ($condition) {
147 return $condition['field'] . ':' . $value;
149 foreach ($this->getConfigKeyStore()->getMultiple($keys) as $list) {
150 $names = array_merge($names, $list);
154 // Save the first ID condition that is not an 'IN' or '=' for narrowing
156 elseif (!$id_condition && $condition['field'] == $id_key) {
157 $id_condition = $condition;
159 // We stop at the first restricting condition on name. In the case where
160 // there are additional restricting conditions, results will be
161 // eliminated when the conditions are checked on the loaded records.
162 if ($names !== FALSE) {
163 // If the condition has been responsible for narrowing the list of
164 // configuration to check there is no point in checking it further.
165 unset($conditions[$condition_key]);
170 // If no restrictions on IDs were found, we need to parse all records.
171 if ($names === FALSE) {
172 $names = $this->configFactory->listAll($prefix);
174 // In case we have an ID condition, try to narrow down the list of config
176 if ($id_condition && !empty($names)) {
177 $value = $id_condition['value'];
179 switch ($id_condition['operator']) {
181 $filter = function ($name) use ($value, $prefix_length) {
182 $id = substr($name, $prefix_length);
183 return $id !== $value;
187 $filter = function ($name) use ($value, $prefix_length) {
188 $id = substr($name, $prefix_length);
189 return strpos($id, $value) === 0;
193 $filter = function ($name) use ($value, $prefix_length) {
194 $id = substr($name, $prefix_length);
195 return strpos($id, $value) !== FALSE;
199 $filter = function ($name) use ($value, $prefix_length) {
200 $id = substr($name, $prefix_length);
201 return strrpos($id, $value) === strlen($id) - strlen($value);
206 $names = array_filter($names, $filter);
210 // Load the corresponding records.
212 foreach ($this->configFactory->loadMultiple($names) as $config) {
213 $records[substr($config->getName(), $prefix_length)] = $config->get();
219 * Gets the key value store used to store fast lookups.
221 * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
222 * The key value store used to store fast lookups.
224 protected function getConfigKeyStore() {
225 return $this->keyValueFactory->get(QueryFactory::CONFIG_LOOKUP_PREFIX . $this->entityTypeId);