Commit 9b7ead2e authored by alexpott's avatar alexpott

Issue #2145633 by Jose Reyero, Gábor Hojtsy | YesCT: Use standard discovery...

Issue #2145633 by Jose Reyero, Gábor Hojtsy | YesCT: Use standard discovery facility in TypedConfigManager instead of one-off implementation.
parent 4c285fef
......@@ -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']
......
<?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;
}
}
......@@ -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();
}
/**
......
......@@ -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
......
......@@ -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".
*/
......@@ -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.
......
......@@ -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;
......
......@@ -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".
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment