From 9b7ead2e45935bc1dadfe74490b8cbefa54f433a Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Wed, 6 Aug 2014 10:50:16 +0100 Subject: [PATCH] =?UTF-8?q?Issue=20#2145633=20by=20Jose=20Reyero,=20G?= =?UTF-8?q?=C3=A1bor=20Hojtsy=20|=20YesCT:=20Use=20standard=20discovery=20?= =?UTF-8?q?facility=20in=20TypedConfigManager=20instead=20of=20one-off=20i?= =?UTF-8?q?mplementation.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/core.services.yml | 4 +- .../Config/Schema/ConfigSchemaDiscovery.php | 50 +++++++++++++++++++ .../Drupal/Core/Config/TypedConfigManager.php | 47 +++-------------- .../config/src/Tests/DefaultConfigTest.php | 3 +- .../config_translation.api.php | 19 ------- .../config_translation.module | 4 +- .../src/Form/ConfigTranslationFormBase.php | 20 +------- core/modules/system/system.api.php | 27 ++++++++++ 8 files changed, 93 insertions(+), 81 deletions(-) create mode 100644 core/lib/Drupal/Core/Config/Schema/ConfigSchemaDiscovery.php diff --git a/core/core.services.yml b/core/core.services.yml index 59e8a6f19890..d8bac8f4be90 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 000000000000..53330ec9f4e1 --- /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 ba3f3530df29..c025a2079a6e 100644 --- a/core/lib/Drupal/Core/Config/TypedConfigManager.php +++ b/core/lib/Drupal/Core/Config/TypedConfigManager.php @@ -9,6 +9,8 @@ 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; /** @@ -16,13 +18,6 @@ */ 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. * @@ -44,13 +39,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. * @@ -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; } /** @@ -147,34 +138,12 @@ public function getDefinition($base_plugin_id, $exception_on_invalid = TRUE) { ); } - /** - * {@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 3edbc7c73071..48805612e7fa 100644 --- a/core/modules/config/src/Tests/DefaultConfigTest.php +++ b/core/modules/config/src/Tests/DefaultConfigTest.php @@ -37,7 +37,8 @@ public function testDefaultConfig() { $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 a7db1857e02e..27c89b199fc9 100644 --- a/core/modules/config_translation/config_translation.api.php +++ b/core/modules/config_translation/config_translation.api.php @@ -88,25 +88,6 @@ function hook_config_translation_info_alter(&$info) { $info['system.site_information_settings']['names'][] = 'example.site.setting'; } -/** - * 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 b1289312bdb0..b0565b7a4e65 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 528158e9b221..f3f769d57a49 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php @@ -49,13 +49,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. * @@ -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 @@ public static function create(ContainerInterface $container) { $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 @@ protected function buildConfigForm(Element $schema, $config_data, $base_config_d 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 14771543c082..b50698f3b3d6 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -2837,6 +2837,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". */ -- GitLab