/** * {@inheritdoc} */ public function getAllDefinedLanguages() { // Get list of all configured languages. $languages = []; // See Drupal\language\ConfigurableLanguageManager::getLanguages() for details $predefined = LanguageManager::getStandardLanguageList(); foreach ($predefined as $key => $value) { $languages[$key] = new TranslatableMarkup($value[0]); } $config_ids = $this->configFactory->listAll('language.entity.'); foreach ($this->configFactory->loadMultiple($config_ids) as $config) { $data = $config->get(); $languages[$data['id']] = new TranslatableMarkup($data['label']); } asort($languages); return $languages; }
/** * {@inheritdoc} */ protected function doLoadMultiple(array $ids = NULL) { $prefix = $this->getPrefix(); // Get the names of the configuration entities we are going to load. if ($ids === NULL) { $names = $this->configFactory->listAll($prefix); } else { $names = array(); foreach ($ids as $id) { // Add the prefix to the ID to serve as the configuration object name. $names[] = $prefix . $id; } } // Load all of the configuration entities. /** @var \Drupal\Core\Config\Config[] $configs */ $configs = []; $records = []; foreach ($this->configFactory->loadMultiple($names) as $config) { $id = $config->get($this->idKey); $records[$id] = $this->overrideFree ? $config->getOriginal(NULL, FALSE) : $config->get(); $configs[$id] = $config; } $entities = $this->mapFromStorageRecords($records, $configs); // Config entities wrap config objects, and therefore they need to inherit // the cacheability metadata of config objects (to ensure e.g. additional // cacheability metadata added by config overrides is not lost). foreach ($entities as $id => $entity) { // But rather than simply inheriting all cacheability metadata of config // objects, we need to make sure the self-referring cache tag that is // present on Config objects is not added to the Config entity. It must be // removed for 3 reasons: // 1. When renaming/duplicating a Config entity, the cache tag of the // original config object would remain present, which would be wrong. // 2. Some Config entities choose to not use the cache tag that the under- // lying Config object provides by default (For performance and // cacheability reasons it may not make sense to have a unique cache // tag for every Config entity. The DateFormat Config entity specifies // the 'rendered' cache tag for example, because A) date formats are // changed extremely rarely, so invalidating all render cache items is // fine, B) it means fewer cache tags per page.). // 3. Fewer cache tags is better for performance. $self_referring_cache_tag = ['config:' . $configs[$id]->getName()]; $config_cacheability = CacheableMetadata::createFromObject($configs[$id]); $config_cacheability->setCacheTags(array_diff($config_cacheability->getCacheTags(), $self_referring_cache_tag)); $entity->addCacheableDependency($config_cacheability); } return $entities; }
/** * {@inheritdoc} */ public function getConfigDependencyManager() { $dependency_manager = new ConfigDependencyManager(); // Read all configuration using the factory. This ensures that multiple // deletes during the same request benefit from the static cache. Using the // factory also ensures configuration entity dependency discovery has no // dependencies on the config entity classes. Assume data with UUID is a // config entity. Only configuration entities can be depended on so we can // ignore everything else. $data = array_map(function ($config) { $data = $config->get(); if (isset($data['uuid'])) { return $data; } return FALSE; }, $this->configFactory->loadMultiple($this->activeStorage->listAll())); $dependency_manager->setData(array_filter($data)); return $dependency_manager; }
/** * {@inheritdoc} */ protected function doLoadMultiple(array $ids = NULL) { $prefix = $this->getPrefix(); // Get the names of the configuration entities we are going to load. if ($ids === NULL) { $names = $this->configFactory->listAll($prefix); } else { $names = array(); foreach ($ids as $id) { // Add the prefix to the ID to serve as the configuration object name. $names[] = $prefix . $id; } } // Load all of the configuration entities. $records = array(); foreach ($this->configFactory->loadMultiple($names) as $config) { $records[$config->get($this->idKey)] = $this->overrideFree ? $config->getOriginal(NULL, FALSE) : $config->get(); } return $this->mapFromStorageRecords($records); }
/** * Loads the config records to examine for the query. * * @return array * Config records keyed by entity IDs. */ protected function loadRecords() { $prefix = $this->entityType->getConfigPrefix() . '.'; $prefix_length = strlen($prefix); // Search the conditions for restrictions on entity IDs. $ids = array(); if ($this->condition->getConjunction() == 'AND') { foreach ($this->condition->conditions() as $condition) { if (is_string($condition['field']) && $condition['field'] == $this->entityType->getKey('id')) { $operator = $condition['operator'] ?: (is_array($condition['value']) ? 'IN' : '='); if ($operator == '=') { $ids = array($condition['value']); } elseif ($operator == 'IN') { $ids = $condition['value']; } // We stop at the first restricting condition on ID. In the (weird) // case where there are additional restricting conditions, results // will be eliminated when the conditions are checked on the loaded // records. if ($ids) { break; } } } } // If there are conditions restricting config ID, we can narrow the list of // records to load and parse. if ($ids) { $names = array_map(function ($id) use($prefix) { return $prefix . $id; }, $ids); } else { $names = $this->configStorage->listAll($prefix); } // Load the corresponding records. $records = array(); foreach ($this->configFactory->loadMultiple($names) as $config) { $records[substr($config->getName(), $prefix_length)] = $config->get(); } return $records; }
/** * {@inheritdoc} */ public function updateLockedLanguageWeights() { $max_weight = 0; // Get maximum weight to update the system languages to keep them on bottom. foreach ($this->getLanguages(LanguageInterface::STATE_CONFIGURABLE) as $language) { if (!$language->isLocked()) { $max_weight = max($max_weight, $language->getWeight()); } } // Loop locked languages to maintain the existing order. $locked_languages = $this->getLanguages(LanguageInterface::STATE_LOCKED); $config_ids = array_map(function ($language) { return 'language.entity.' . $language->getId(); }, $locked_languages); foreach ($this->configFactory->loadMultiple($config_ids) as $config) { // Update system languages weight. $max_weight++; $config->set('weight', $max_weight); $config->save(); } }
/** * {@inheritdoc} */ public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) { // If a config override is set, cache using that language's ID. if ($override_language = $this->getConfigOverrideLanguage()) { $static_cache_id = $override_language->getId(); } else { $static_cache_id = $this->getCurrentLanguage()->getId(); } if (!isset($this->languages[$static_cache_id][$flags])) { // Initialize the language list with the default language and default // locked languages. These cannot be removed. This serves as a fallback // list if this method is invoked while the language module is installed // and the configuration entities for languages are not yet fully // imported. $default = $this->getDefaultLanguage(); $languages = array($default->getId() => $default); $languages += $this->getDefaultLockedLanguages($default->getWeight()); // Load configurable languages on top of the defaults. Ideally this could // use the entity API to load and instantiate ConfigurableLanguage // objects. However the entity API depends on the language system, so that // would result in infinite loops. We use the configuration system // directly and instantiate runtime Language objects. When language // entities are imported those cover the default and locked languages, so // site-specific configuration will prevail over the fallback values. // Having them in the array already ensures if this is invoked in the // middle of importing language configuration entities, the defaults are // always present. $config_ids = $this->configFactory->listAll('language.entity.'); foreach ($this->configFactory->loadMultiple($config_ids) as $config) { $data = $config->get(); $data['name'] = $data['label']; $languages[$data['id']] = new Language($data); } Language::sort($languages); // Filter the full list of languages based on the value of $flags. $this->languages[$static_cache_id][$flags] = $this->filterLanguages($languages, $flags); } return $this->languages[$static_cache_id][$flags]; }
/** * Creates new instance of BaseMapping class. * * @param ConfigFactoryInterface $config */ public function __construct(ConfigFactoryInterface $config) { $ids = $config->listAll($this->getConfigKey()); $this->config = $config->loadMultiple($ids); }
/** * Loads the config records to examine for the query. * * @return array * Config records keyed by entity IDs. */ protected function loadRecords() { $prefix = $this->entityType->getConfigPrefix() . '.'; $prefix_length = strlen($prefix); // Search the conditions for restrictions on configuration object names. $names = 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 == '=')) { // Special case ID lookups. if ($condition['field'] == $id_key) { $ids = (array) $condition['value']; $names = array_map(function ($id) use($prefix) { return $prefix . $id; }, $ids); } elseif (in_array($condition['field'], $lookup_keys)) { // If we don't find anything then there are no matches. No point in // listing anything. $names = array(); $keys = (array) $condition['value']; $keys = array_map(function ($value) use($condition) { return $condition['field'] . ':' . $value; }, $keys); foreach ($this->getConfigKeyStore()->getMultiple($keys) as $list) { $names = array_merge($names, $list); } } } elseif (!$id_condition && $condition['field'] == $id_key) { $id_condition = $condition; } // We stop at the first restricting condition on name. In the case where // there are additional restricting conditions, results will be // eliminated when the conditions are checked on the loaded records. if ($names !== FALSE) { // If the condition has been responsible for narrowing the list of // configuration to check there is no point in checking it further. unset($conditions[$condition_key]); break; } } } // If no restrictions on IDs were found, we need to parse all records. if ($names === FALSE) { $names = $this->configFactory->listAll($prefix); } // In case we have an ID condition, try to narrow down the list of config // objects to load. if ($id_condition && !empty($names)) { $value = $id_condition['value']; $filter = NULL; switch ($id_condition['operator']) { case '<>': $filter = function ($name) use($value, $prefix_length) { $id = substr($name, $prefix_length); return $id !== $value; }; break; case 'STARTS_WITH': $filter = function ($name) use($value, $prefix_length) { $id = substr($name, $prefix_length); return strpos($id, $value) === 0; }; break; case 'CONTAINS': $filter = function ($name) use($value, $prefix_length) { $id = substr($name, $prefix_length); return strpos($id, $value) !== FALSE; }; break; case 'ENDS_WITH': $filter = function ($name) use($value, $prefix_length) { $id = substr($name, $prefix_length); return strrpos($id, $value) === strlen($id) - strlen($value); }; break; } if ($filter) { $names = array_filter($names, $filter); } } // Load the corresponding records. $records = array(); foreach ($this->configFactory->loadMultiple($names) as $config) { $records[substr($config->getName(), $prefix_length)] = $config->get(); } return $records; }