summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-08-06 10:50:16 +0100
committerAlex Pott2014-08-06 10:50:16 +0100
commit9b7ead2e45935bc1dadfe74490b8cbefa54f433a (patch)
tree77c11ae0ff7303142e96d19c961f14dfd3ef25c1
parent4c285fefdd2d83ff4e38e57e8390c471eabf88a9 (diff)
Issue #2145633 by Jose Reyero, Gábor Hojtsy | YesCT: Use standard discovery facility in TypedConfigManager instead of one-off implementation.8.0.0-alpha14
-rw-r--r--core/core.services.yml4
-rw-r--r--core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php50
-rw-r--r--core/lib/Drupal/Core/Config/TypedConfigManager.php47
-rw-r--r--core/modules/config/src/Tests/DefaultConfigTest.php3
-rw-r--r--core/modules/config_translation/config_translation.api.php19
-rw-r--r--core/modules/config_translation/config_translation.module4
-rw-r--r--core/modules/config_translation/src/Form/ConfigTranslationFormBase.php20
-rw-r--r--core/modules/system/system.api.php27
8 files changed, 93 insertions, 81 deletions
diff --git a/core/core.services.yml b/core/core.services.yml
index 59e8a6f..d8bac8f 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -132,7 +132,9 @@ services:
class: Drupal\Core\Config\InstallStorage
config.typed:
class: Drupal\Core\Config\TypedConfigManager
- arguments: ['@config.storage', '@config.storage.schema', '@cache.discovery']
+ arguments: ['@config.storage', '@config.storage.schema', '@cache.discovery', '@module_handler']
+ tags:
+ - { name: plugin_manager_cache_clear }
context.handler:
class: Drupal\Core\Plugin\Context\ContextHandler
arguments: ['@typed_data_manager']
diff --git a/core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php b/core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php
new file mode 100644
index 0000000..53330ec
--- /dev/null
+++ b/core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Config\Schema\ConfigSchemaDiscovery.
+ */
+
+namespace Drupal\Core\Config\Schema;
+
+use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
+use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
+use Drupal\Core\Config\StorageInterface;
+
+/**
+ * Allows YAML files to define config schema types.
+ */
+class ConfigSchemaDiscovery implements DiscoveryInterface {
+
+ use DiscoveryTrait;
+
+ /**
+ * A storage instance for reading configuration schema data.
+ *
+ * @var \Drupal\Core\Config\StorageInterface
+ */
+ protected $schemaStorage;
+
+ /**
+ * Constructs a ConfigSchemaDiscovery object.
+ *
+ * @param $schema_storage
+ * The storage object to use for reading schema data.
+ */
+ function __construct(StorageInterface $schema_storage) {
+ $this->schemaStorage = $schema_storage;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getDefinitions() {
+ $definitions = array();
+ foreach ($this->schemaStorage->readMultiple($this->schemaStorage->listAll()) as $schema) {
+ foreach ($schema as $type => $definition) {
+ $definitions[$type] = $definition;
+ }
+ }
+ return $definitions;
+ }
+}
diff --git a/core/lib/Drupal/Core/Config/TypedConfigManager.php b/core/lib/Drupal/Core/Config/TypedConfigManager.php
index ba3f353..c025a20 100644
--- a/core/lib/Drupal/Core/Config/TypedConfigManager.php
+++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php
@@ -9,6 +9,8 @@ namespace Drupal\Core\Config;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Config\Schema\ConfigSchemaDiscovery;
+use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\TypedData\TypedDataManager;
/**
@@ -17,13 +19,6 @@ use Drupal\Core\TypedData\TypedDataManager;
class TypedConfigManager extends TypedDataManager implements TypedConfigManagerInterface {
/**
- * The cache ID for the definitions.
- *
- * @var string
- */
- const CACHE_ID = 'typed_config_definitions';
-
- /**
* A storage instance for reading configuration data.
*
* @var \Drupal\Core\Config\StorageInterface
@@ -45,13 +40,6 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
protected $definitions;
/**
- * Cache backend for the definitions.
- *
- * @var \Drupal\Core\Cache\CacheBackendInterface
- */
- protected $cache;
-
- /**
* Creates a new typed configuration manager.
*
* @param \Drupal\Core\Config\StorageInterface $configStorage
@@ -61,10 +49,13 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to use for caching the definitions.
*/
- public function __construct(StorageInterface $configStorage, StorageInterface $schemaStorage, CacheBackendInterface $cache) {
+ public function __construct(StorageInterface $configStorage, StorageInterface $schemaStorage, CacheBackendInterface $cache, ModuleHandlerInterface $module_handler) {
$this->configStorage = $configStorage;
$this->schemaStorage = $schemaStorage;
- $this->cache = $cache;
+ $this->setCacheBackend($cache, 'typed_config_definitions');
+ $this->discovery = new ConfigSchemaDiscovery($schemaStorage);
+ $this->alterInfo('config_schema_info');
+ $this->moduleHandler = $module_handler;
}
/**
@@ -150,31 +141,9 @@ class TypedConfigManager extends TypedDataManager implements TypedConfigManagerI
/**
* {@inheritdoc}
*/
- public function getDefinitions() {
- if (!isset($this->definitions)) {
- if ($cache = $this->cache->get($this::CACHE_ID)) {
- $this->definitions = $cache->data;
- }
- else {
- $this->definitions = array();
- foreach ($this->schemaStorage->readMultiple($this->schemaStorage->listAll()) as $schema) {
- foreach ($schema as $type => $definition) {
- $this->definitions[$type] = $definition;
- }
- }
- $this->cache->set($this::CACHE_ID, $this->definitions);
- }
- }
- return $this->definitions;
- }
-
- /**
- * {@inheritdoc}
- */
public function clearCachedDefinitions() {
- $this->definitions = NULL;
$this->schemaStorage->reset();
- $this->cache->delete($this::CACHE_ID);
+ parent::clearCachedDefinitions();
}
/**
diff --git a/core/modules/config/src/Tests/DefaultConfigTest.php b/core/modules/config/src/Tests/DefaultConfigTest.php
index 3edbc7c..4880561 100644
--- a/core/modules/config/src/Tests/DefaultConfigTest.php
+++ b/core/modules/config/src/Tests/DefaultConfigTest.php
@@ -37,7 +37,8 @@ class DefaultConfigTest extends KernelTestBase {
$typed_config = new TypedConfigManager(
\Drupal::service('config.storage'),
new TestInstallStorage(InstallStorage::CONFIG_SCHEMA_DIRECTORY),
- \Drupal::service('cache.discovery')
+ \Drupal::service('cache.discovery'),
+ \Drupal::service('module_handler')
);
// Create a configuration storage with access to default configuration in
diff --git a/core/modules/config_translation/config_translation.api.php b/core/modules/config_translation/config_translation.api.php
index a7db185..27c89b1 100644
--- a/core/modules/config_translation/config_translation.api.php
+++ b/core/modules/config_translation/config_translation.api.php
@@ -89,24 +89,5 @@ function hook_config_translation_info_alter(&$info) {
}
/**
- * Alter config typed data definitions.
- *
- * Used to automatically generate translation forms, you can alter the typed
- * data types representing each configuration schema type to change default
- * labels or form element renderers.
- *
- * @param $definitions
- * Associative array of configuration type definitions keyed by schema type
- * names. The elements are themselves array with information about the type.
- */
-function hook_config_translation_type_info_alter(&$definitions) {
- // Enhance the text and date type definitions with classes to generate proper
- // form elements in ConfigTranslationFormBase. Other translatable types will
- // appear as a one line textfield.
- $definitions['text']['form_element_class'] = '\Drupal\config_translation\FormElement\Textarea';
- $definitions['date_format']['form_element_class'] = '\Drupal\config_translation\FormElement\DateFormat';
-}
-
-/**
* @} End of "addtogroup hooks".
*/
diff --git a/core/modules/config_translation/config_translation.module b/core/modules/config_translation/config_translation.module
index b128931..b0565b7 100644
--- a/core/modules/config_translation/config_translation.module
+++ b/core/modules/config_translation/config_translation.module
@@ -192,9 +192,9 @@ function config_translation_entity_operation(EntityInterface $entity) {
}
/**
- * Implements hook_config_translation_type_info_alter().
+ * Implements hook_config_schema_info_alter().
*/
-function config_translation_config_translation_type_info_alter(&$definitions) {
+function config_translation_config_schema_info_alter(&$definitions) {
// Enhance the text and date type definitions with classes to generate proper
// form elements in ConfigTranslationFormBase. Other translatable types will
// appear as a one line textfield.
diff --git a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
index 528158e..f3f769d 100644
--- a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
+++ b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
@@ -50,13 +50,6 @@ abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdI
protected $localeStorage;
/**
- * The module handler to invoke the alter hook.
- *
- * @var \Drupal\Core\Extension\ModuleHandlerInterface
- */
- protected $moduleHandler;
-
- /**
* The mapper for configuration translation.
*
* @var \Drupal\config_translation\ConfigMapperInterface
@@ -100,14 +93,11 @@ abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdI
* The configuration mapper manager.
* @param \Drupal\locale\StringStorageInterface $locale_storage
* The translation storage object.
- * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
- * The module handler to invoke the alter hook.
*/
- public function __construct(TypedConfigManagerInterface $typed_config_manager, ConfigMapperManagerInterface $config_mapper_manager, StringStorageInterface $locale_storage, ModuleHandlerInterface $module_handler, ConfigurableLanguageManagerInterface $language_manager) {
+ public function __construct(TypedConfigManagerInterface $typed_config_manager, ConfigMapperManagerInterface $config_mapper_manager, StringStorageInterface $locale_storage, ConfigurableLanguageManagerInterface $language_manager) {
$this->typedConfigManager = $typed_config_manager;
$this->configMapperManager = $config_mapper_manager;
$this->localeStorage = $locale_storage;
- $this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
}
@@ -119,7 +109,6 @@ abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdI
$container->get('config.typed'),
$container->get('plugin.manager.config_translation.mapper'),
$container->get('locale.storage'),
- $container->get('module_handler'),
$container->get('language_manager')
);
}
@@ -312,13 +301,6 @@ abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdI
else {
$definition = $element->getDataDefinition();
- // Invoke hook_config_translation_type_info_alter() implementations to
- // alter the configuration types.
- $definitions = array(
- $definition['type'] => &$definition,
- );
- $this->moduleHandler->alter('config_translation_type_info', $definitions);
-
// Create form element only for translatable items.
if (!isset($definition['translatable']) || !isset($definition['type'])) {
continue;
diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php
index 1477154..b50698f 100644
--- a/core/modules/system/system.api.php
+++ b/core/modules/system/system.api.php
@@ -2838,6 +2838,33 @@ function hook_config_import_steps_alter(&$sync_steps, \Drupal\Core\Config\Config
}
/**
+ * Alter config typed data definitions.
+ *
+ * For example you can alter the typed data types representing each
+ * configuration schema type to change default labels or form element renderers
+ * used for configuration translation.
+ *
+ * It is strongly advised not to use this hook to add new data types or to
+ * change the structure of existing ones. Keep in mind that there are tools
+ * that may use the configuration schema for static analysis of configuration
+ * files, like the string extractor for the localization system. Such systems
+ * won't work with dynamically defined configuration schemas.
+ *
+ * For adding new data types use configuration schema YAML files instead.
+ *
+ * @param $definitions
+ * Associative array of configuration type definitions keyed by schema type
+ * names. The elements are themselves array with information about the type.
+ */
+function hook_config_schema_info_alter(&$definitions) {
+ // Enhance the text and date type definitions with classes to generate proper
+ // form elements in ConfigTranslationFormBase. Other translatable types will
+ // appear as a one line textfield.
+ $definitions['text']['form_element_class'] = '\Drupal\config_translation\FormElement\Textarea';
+ $definitions['date_format']['form_element_class'] = '\Drupal\config_translation\FormElement\DateFormat';
+}
+
+/**
* @} End of "addtogroup hooks".
*/