class QueryFactory

Same name in this branch
  1. 10 core/modules/workspaces/src/EntityQuery/QueryFactory.php \Drupal\workspaces\EntityQuery\QueryFactory
  2. 10 core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\QueryFactory
  3. 10 core/lib/Drupal/Core/Entity/Query/Sql/pgsql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\pgsql\QueryFactory
  4. 10 core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php \Drupal\Core\Entity\Query\Null\QueryFactory
  5. 10 core/lib/Drupal/Core/Entity/KeyValueStore/Query/QueryFactory.php \Drupal\Core\Entity\KeyValueStore\Query\QueryFactory
Same name in other branches
  1. 9 core/modules/workspaces/src/EntityQuery/QueryFactory.php \Drupal\workspaces\EntityQuery\QueryFactory
  2. 9 core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php \Drupal\Core\Config\Entity\Query\QueryFactory
  3. 9 core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\QueryFactory
  4. 9 core/lib/Drupal/Core/Entity/Query/Sql/pgsql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\pgsql\QueryFactory
  5. 9 core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php \Drupal\Core\Entity\Query\Null\QueryFactory
  6. 9 core/lib/Drupal/Core/Entity/KeyValueStore/Query/QueryFactory.php \Drupal\Core\Entity\KeyValueStore\Query\QueryFactory
  7. 8.9.x core/modules/workspaces/src/EntityQuery/QueryFactory.php \Drupal\workspaces\EntityQuery\QueryFactory
  8. 8.9.x core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php \Drupal\Core\Config\Entity\Query\QueryFactory
  9. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\QueryFactory
  10. 8.9.x core/lib/Drupal/Core/Entity/Query/Sql/pgsql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\pgsql\QueryFactory
  11. 8.9.x core/lib/Drupal/Core/Entity/Query/QueryFactory.php \Drupal\Core\Entity\Query\QueryFactory
  12. 8.9.x core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php \Drupal\Core\Entity\Query\Null\QueryFactory
  13. 8.9.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/QueryFactory.php \Drupal\Core\Entity\KeyValueStore\Query\QueryFactory
  14. 11.x core/modules/workspaces/src/EntityQuery/QueryFactory.php \Drupal\workspaces\EntityQuery\QueryFactory
  15. 11.x core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php \Drupal\Core\Config\Entity\Query\QueryFactory
  16. 11.x core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\QueryFactory
  17. 11.x core/lib/Drupal/Core/Entity/Query/Sql/pgsql/QueryFactory.php \Drupal\Core\Entity\Query\Sql\pgsql\QueryFactory
  18. 11.x core/lib/Drupal/Core/Entity/Query/Null/QueryFactory.php \Drupal\Core\Entity\Query\Null\QueryFactory
  19. 11.x core/lib/Drupal/Core/Entity/KeyValueStore/Query/QueryFactory.php \Drupal\Core\Entity\KeyValueStore\Query\QueryFactory

Provides a factory for creating entity query objects for the config backend.

Hierarchy

Expanded class hierarchy of QueryFactory

2 files declare their use of QueryFactory
ConfigEntityQueryTest.php in core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php
QueryFactoryTest.php in core/tests/Drupal/Tests/Core/Config/Entity/Query/QueryFactoryTest.php
1 string reference to 'QueryFactory'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses QueryFactory
entity.query.config in core/core.services.yml
Drupal\Core\Config\Entity\Query\QueryFactory

File

core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php, line 21

Namespace

Drupal\Core\Config\Entity\Query
View source
class QueryFactory implements QueryFactoryInterface, EventSubscriberInterface {
    
    /**
     * The prefix for the key value collection for fast lookups.
     */
    const CONFIG_LOOKUP_PREFIX = 'config.entity.key_store.';
    
    /**
     * The config factory used by the config entity query.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * The namespace of this class, the parent class etc.
     *
     * @var array
     */
    protected $namespaces;
    
    /**
     * The key value factory.
     */
    protected KeyValueFactoryInterface $keyValueFactory;
    
    /**
     * The configuration manager.
     */
    protected ConfigManagerInterface $configManager;
    
    /**
     * Constructs a QueryFactory object.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config storage used by the config entity query.
     * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
     *   The key value factory.
     * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
     *   The configuration manager.
     */
    public function __construct(ConfigFactoryInterface $config_factory, KeyValueFactoryInterface $key_value, ConfigManagerInterface $config_manager) {
        $this->configFactory = $config_factory;
        $this->keyValueFactory = $key_value;
        $this->configManager = $config_manager;
        $this->namespaces = QueryBase::getNamespaces($this);
    }
    
    /**
     * {@inheritdoc}
     */
    public function get(EntityTypeInterface $entity_type, $conjunction) {
        return new Query($entity_type, $conjunction, $this->configFactory, $this->keyValueFactory, $this->namespaces);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAggregate(EntityTypeInterface $entity_type, $conjunction) {
        throw new QueryException('Aggregation over configuration entities is not supported');
    }
    
    /**
     * Gets the key value store used to store fast lookups.
     *
     * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
     *   The entity type.
     *
     * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
     *   The key value store used to store fast lookups.
     */
    protected function getConfigKeyStore(EntityTypeInterface $entity_type) {
        return $this->keyValueFactory
            ->get(static::CONFIG_LOOKUP_PREFIX . $entity_type->id());
    }
    
    /**
     * Updates or adds lookup data.
     *
     * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
     *   The entity type.
     * @param \Drupal\Core\Config\Config $config
     *   The configuration object that is being saved.
     */
    protected function updateConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
        $config_key_store = $this->getConfigKeyStore($entity_type);
        foreach ($entity_type->getLookupKeys() as $lookup_key) {
            foreach ($this->getKeys($config, $lookup_key, 'get', $entity_type) as $key) {
                $values = $config_key_store->get($key, []);
                if (!in_array($config->getName(), $values, TRUE)) {
                    $values[] = $config->getName();
                    $config_key_store->set($key, $values);
                }
            }
        }
    }
    
    /**
     * Deletes lookup data.
     *
     * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
     *   The entity type.
     * @param \Drupal\Core\Config\Config $config
     *   The configuration object that is being deleted.
     */
    protected function deleteConfigKeyStore(ConfigEntityTypeInterface $entity_type, Config $config) {
        $config_key_store = $this->getConfigKeyStore($entity_type);
        foreach ($entity_type->getLookupKeys() as $lookup_key) {
            foreach ($this->getKeys($config, $lookup_key, 'getOriginal', $entity_type) as $key) {
                $values = $config_key_store->get($key, []);
                $pos = array_search($config->getName(), $values, TRUE);
                if ($pos !== FALSE) {
                    unset($values[$pos]);
                }
                if (empty($values)) {
                    $config_key_store->delete($key);
                }
                else {
                    $config_key_store->set($key, $values);
                }
            }
        }
    }
    
    /**
     * Creates lookup keys for configuration data.
     *
     * @param \Drupal\Core\Config\Config $config
     *   The configuration object.
     * @param string $key
     *   The configuration key to look for.
     * @param string $get_method
     *   Which method on the config object to call to get the value. Either 'get'
     *   or 'getOriginal'.
     * @param \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type
     *   The configuration entity type.
     *
     * @return array
     *   An array of lookup keys concatenated to the configuration values.
     *
     * @throws \Drupal\Core\Config\Entity\Query\InvalidLookupKeyException
     *   The provided $key cannot end with a wildcard. This makes no sense since
     *   you cannot do fast lookups against this.
     */
    protected function getKeys(Config $config, $key, $get_method, ConfigEntityTypeInterface $entity_type) {
        if (str_ends_with($key, '*')) {
            throw new InvalidLookupKeyException(strtr('%entity_type lookup key %key ends with a wildcard this can not be used as a lookup', [
                '%entity_type' => $entity_type->id(),
                '%key' => $key,
            ]));
        }
        $parts = explode('.*', $key);
        // Remove leading dots.
        array_walk($parts, function (&$value) {
            $value = trim($value, '.');
        });
        $values = (array) $this->getValues($config, $parts[0], $get_method, $parts);
        $output = [];
        // Flatten the array to a single dimension and add the key to all the
        // values.
        array_walk_recursive($values, function ($current) use (&$output, $key) {
            if (is_scalar($current)) {
                $current = $key . ':' . $current;
            }
            $output[] = $current;
        });
        return $output;
    }
    
    /**
     * Finds all the values for a configuration key in a configuration object.
     *
     * @param \Drupal\Core\Config\Config $config
     *   The configuration object.
     * @param string $key
     *   The current key being checked.
     * @param string $get_method
     *   Which method on the config object to call to get the value.
     * @param array $parts
     *   All the parts of a configuration key we are checking.
     * @param int $start
     *   Which position of $parts we are processing. Defaults to 0.
     *
     * @return array|null
     *   The array of configuration values the match the provided key. NULL if
     *   the configuration object does not have a value that corresponds to the
     *   key.
     */
    protected function getValues(Config $config, $key, $get_method, array $parts, $start = 0) {
        $value = $config->{$get_method}($key);
        if (is_array($value)) {
            $new_value = [];
            $start++;
            if (!isset($parts[$start])) {
                // The configuration object does not have a value that corresponds to
                // the key.
                return NULL;
            }
            foreach (array_keys($value) as $key_bit) {
                $new_key = $key . '.' . $key_bit;
                if (!empty($parts[$start])) {
                    $new_key .= '.' . $parts[$start];
                }
                $new_value[] = $this->getValues($config, $new_key, $get_method, $parts, $start);
            }
            $value = $new_value;
        }
        return $value;
    }
    
    /**
     * Updates configuration entity in the key store.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     *   The configuration event.
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        $saved_config = $event->getConfig();
        $entity_type_id = $this->configManager
            ->getEntityTypeIdByName($saved_config->getName());
        if ($entity_type_id) {
            $entity_type = $this->configManager
                ->getEntityTypeManager()
                ->getDefinition($entity_type_id);
            $this->updateConfigKeyStore($entity_type, $saved_config);
        }
    }
    
    /**
     * Removes configuration entity from key store.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     *   The configuration event.
     */
    public function onConfigDelete(ConfigCrudEvent $event) {
        $saved_config = $event->getConfig();
        $entity_type_id = $this->configManager
            ->getEntityTypeIdByName($saved_config->getName());
        if ($entity_type_id) {
            $entity_type = $this->configManager
                ->getEntityTypeManager()
                ->getDefinition($entity_type_id);
            $this->deleteConfigKeyStore($entity_type, $saved_config);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            128,
        ];
        $events[ConfigEvents::DELETE][] = [
            'onConfigDelete',
            128,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
QueryFactory::$configFactory protected property The config factory used by the config entity query.
QueryFactory::$configManager protected property The configuration manager.
QueryFactory::$keyValueFactory protected property The key value factory.
QueryFactory::$namespaces protected property The namespace of this class, the parent class etc.
QueryFactory::CONFIG_LOOKUP_PREFIX constant The prefix for the key value collection for fast lookups.
QueryFactory::deleteConfigKeyStore protected function Deletes lookup data.
QueryFactory::get public function Overrides QueryFactoryInterface::get
QueryFactory::getAggregate public function Overrides QueryFactoryInterface::getAggregate
QueryFactory::getConfigKeyStore protected function Gets the key value store used to store fast lookups.
QueryFactory::getKeys protected function Creates lookup keys for configuration data.
QueryFactory::getSubscribedEvents public static function
QueryFactory::getValues protected function Finds all the values for a configuration key in a configuration object.
QueryFactory::onConfigDelete public function Removes configuration entity from key store.
QueryFactory::onConfigSave public function Updates configuration entity in the key store.
QueryFactory::updateConfigKeyStore protected function Updates or adds lookup data.
QueryFactory::__construct public function Constructs a QueryFactory object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.