class ConfigNamesMapper in Drupal 9
Same name and namespace in other branches
- 8 core/modules/config_translation/src/ConfigNamesMapper.php \Drupal\config_translation\ConfigNamesMapper
Configuration mapper base implementation.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\config_translation\ConfigNamesMapper implements ConfigMapperInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ConfigNamesMapper
1 file declares its use of ConfigNamesMapper
- ConfigNamesMapperTest.php in core/
modules/ config_translation/ tests/ src/ Unit/ ConfigNamesMapperTest.php - Contains \Drupal\Tests\config_translation\Unit\ConfigNamesMapperTest.
File
- core/
modules/ config_translation/ src/ ConfigNamesMapper.php, line 27
Namespace
Drupal\config_translationView source
class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, ContainerFactoryPluginInterface {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManagerInterface
*/
protected $typedConfigManager;
/**
* The typed configuration manager.
*
* @var \Drupal\locale\LocaleConfigManager
*/
protected $localeConfigManager;
/**
* The mapper plugin discovery service.
*
* @var \Drupal\config_translation\ConfigMapperManagerInterface
*/
protected $configMapperManager;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The base route object that the mapper is attached to.
*
* @var \Symfony\Component\Routing\Route
*/
protected $baseRoute;
/**
* The available routes.
*
* @var \Symfony\Component\Routing\RouteCollection
*/
protected $routeCollection;
/**
* The language code of the language this mapper, if any.
*
* @var string|null
*/
protected $langcode = NULL;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The event dispatcher.
*
* @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a ConfigNamesMapper.
*
* @param $plugin_id
* The config mapper plugin ID.
* @param mixed $plugin_definition
* An array of plugin information with the following keys:
* - title: The title of the mapper, used for generating page titles.
* - base_route_name: The route name of the base route this mapper is
* attached to.
* - names: (optional) An array of configuration names.
* - weight: (optional) The weight of this mapper, used in mapper listings.
* Defaults to 20.
* - list_controller: (optional) Class name for list controller used to
* generate lists of this type of configuration.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
* The typed configuration manager.
* @param \Drupal\locale\LocaleConfigManager $locale_config_manager
* The locale configuration manager.
* @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
* The mapper plugin discovery service.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
* (optional) The event dispatcher.
*
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
* Throws an exception if the route specified by the 'base_route_name' in
* the plugin definition could not be found by the route provider.
*/
public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config, LocaleConfigManager $locale_config_manager, ConfigMapperManagerInterface $config_mapper_manager, RouteProviderInterface $route_provider, TranslationInterface $string_translation, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher = NULL) {
$this->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition;
$this->routeProvider = $route_provider;
$this->configFactory = $config_factory;
$this->typedConfigManager = $typed_config;
$this->localeConfigManager = $locale_config_manager;
$this->configMapperManager = $config_mapper_manager;
$this->stringTranslation = $string_translation;
$this->languageManager = $language_manager;
$this->eventDispatcher = $event_dispatcher ?: \Drupal::service('event_dispatcher');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
// Note that we ignore the plugin $configuration because mappers have
// nothing to configure in themselves.
return new static($plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('config.typed'), $container
->get('locale.config_manager'), $container
->get('plugin.manager.config_translation.mapper'), $container
->get('router.route_provider'), $container
->get('string_translation'), $container
->get('language_manager'), $container
->get('event_dispatcher'));
}
/**
* {@inheritdoc}
*/
public function setRouteCollection(RouteCollection $collection) {
$this->routeCollection = $collection;
}
/**
* {@inheritdoc}
*/
public function getTitle() {
// A title from a *.config_translation.yml. Should be translated for
// display in the current page language.
return $this
->t($this->pluginDefinition['title']);
}
/**
* {@inheritdoc}
*/
public function getBaseRouteName() {
return $this->pluginDefinition['base_route_name'];
}
/**
* {@inheritdoc}
*/
public function getBaseRouteParameters() {
return [];
}
/**
* {@inheritdoc}
*/
public function getBaseRoute() {
if ($this->routeCollection) {
return $this->routeCollection
->get($this
->getBaseRouteName());
}
else {
return $this->routeProvider
->getRouteByName($this
->getBaseRouteName());
}
}
/**
* Allows to process all config translation routes.
*
* @param \Symfony\Component\Routing\Route $route
* The route object to process.
*/
protected function processRoute(Route $route) {
}
/**
* {@inheritdoc}
*/
public function getBasePath() {
return Url::fromRoute($this
->getBaseRouteName(), $this
->getBaseRouteParameters())
->getInternalPath();
}
/**
* {@inheritdoc}
*/
public function getOverviewRouteName() {
return 'config_translation.item.overview.' . $this
->getBaseRouteName();
}
/**
* {@inheritdoc}
*/
public function getOverviewRouteParameters() {
return $this
->getBaseRouteParameters();
}
/**
* {@inheritdoc}
*/
public function getOverviewRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate', [
'_controller' => '\\Drupal\\config_translation\\Controller\\ConfigTranslationController::itemPage',
'plugin_id' => $this
->getPluginId(),
], [
'_config_translation_overview_access' => 'TRUE',
]);
$this
->processRoute($route);
return $route;
}
/**
* {@inheritdoc}
*/
public function getOverviewPath() {
return Url::fromRoute($this
->getOverviewRouteName(), $this
->getOverviewRouteParameters())
->getInternalPath();
}
/**
* {@inheritdoc}
*/
public function getAddRouteName() {
return 'config_translation.item.add.' . $this
->getBaseRouteName();
}
/**
* {@inheritdoc}
*/
public function getAddRouteParameters() {
// If sub-classes provide route parameters in getBaseRouteParameters(), they
// probably also want to provide those for the add, edit, and delete forms.
$parameters = $this
->getBaseRouteParameters();
$parameters['langcode'] = $this->langcode;
return $parameters;
}
/**
* {@inheritdoc}
*/
public function getAddRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/add', [
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationAddForm',
'plugin_id' => $this
->getPluginId(),
], [
'_config_translation_form_access' => 'TRUE',
]);
$this
->processRoute($route);
return $route;
}
/**
* {@inheritdoc}
*/
public function getEditRouteName() {
return 'config_translation.item.edit.' . $this
->getBaseRouteName();
}
/**
* {@inheritdoc}
*/
public function getEditRouteParameters() {
return $this
->getAddRouteParameters();
}
/**
* {@inheritdoc}
*/
public function getEditRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/edit', [
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationEditForm',
'plugin_id' => $this
->getPluginId(),
], [
'_config_translation_form_access' => 'TRUE',
]);
$this
->processRoute($route);
return $route;
}
/**
* {@inheritdoc}
*/
public function getDeleteRouteName() {
return 'config_translation.item.delete.' . $this
->getBaseRouteName();
}
/**
* {@inheritdoc}
*/
public function getDeleteRouteParameters() {
return $this
->getAddRouteParameters();
}
/**
* {@inheritdoc}
*/
public function getDeleteRoute() {
$route = new Route($this
->getBaseRoute()
->getPath() . '/translate/{langcode}/delete', [
'_form' => '\\Drupal\\config_translation\\Form\\ConfigTranslationDeleteForm',
'plugin_id' => $this
->getPluginId(),
], [
'_config_translation_form_access' => 'TRUE',
]);
$this
->processRoute($route);
return $route;
}
/**
* {@inheritdoc}
*/
public function getConfigNames() {
return $this->pluginDefinition['names'];
}
/**
* {@inheritdoc}
*/
public function addConfigName($name) {
$this->pluginDefinition['names'][] = $name;
}
/**
* {@inheritdoc}
*/
public function getWeight() {
return $this->pluginDefinition['weight'];
}
/**
* {@inheritdoc}
*/
public function populateFromRouteMatch(RouteMatchInterface $route_match) {
$this->langcode = $route_match
->getParameter('langcode');
$event = new ConfigMapperPopulateEvent($this, $route_match);
$this->eventDispatcher
->dispatch($event, ConfigTranslationEvents::POPULATE_MAPPER);
}
/**
* {@inheritdoc}
*/
public function getTypeLabel() {
return $this
->getTitle();
}
/**
* {@inheritdoc}
*/
public function getLangcode() {
$langcodes = array_map([
$this,
'getLangcodeFromConfig',
], $this
->getConfigNames());
if (count(array_unique($langcodes)) > 1) {
throw new ConfigMapperLanguageException('A config mapper can only contain configuration for a single language.');
}
return reset($langcodes);
}
/**
* {@inheritdoc}
*/
public function getLangcodeFromConfig($config_name) {
// Default to English if no language code was provided in the file.
// Although it is a best practice to include a language code, if the
// developer did not think about a multilingual use case, we fall back
// on assuming the file is English.
return $this->configFactory
->get($config_name)
->get('langcode') ?: 'en';
}
/**
* {@inheritdoc}
*/
public function setLangcode($langcode) {
$this->langcode = $langcode;
return $this;
}
/**
* {@inheritdoc}
*/
public function getConfigData() {
$config_data = [];
foreach ($this
->getConfigNames() as $name) {
$config_data[$name] = $this->configFactory
->getEditable($name)
->get();
}
return $config_data;
}
/**
* {@inheritdoc}
*/
public function hasSchema() {
foreach ($this
->getConfigNames() as $name) {
if (!$this->typedConfigManager
->hasConfigSchema($name)) {
return FALSE;
}
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function hasTranslatable() {
foreach ($this
->getConfigNames() as $name) {
if ($this->configMapperManager
->hasTranslatable($name)) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function hasTranslation(LanguageInterface $language) {
foreach ($this
->getConfigNames() as $name) {
if ($this->localeConfigManager
->hasTranslation($name, $language
->getId())) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getTypeName() {
return $this
->t('Settings');
}
/**
* {@inheritdoc}
*/
public function getOperations() {
return [
'translate' => [
'title' => $this
->t('Translate'),
'url' => Url::fromRoute($this
->getOverviewRouteName(), $this
->getOverviewRouteParameters()),
],
];
}
/**
* {@inheritdoc}
*/
public function getContextualLinkGroup() {
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigNamesMapper:: |
protected | property | The base route object that the mapper is attached to. | |
ConfigNamesMapper:: |
protected | property | The configuration factory. | |
ConfigNamesMapper:: |
protected | property | The mapper plugin discovery service. | |
ConfigNamesMapper:: |
protected | property | The event dispatcher. | |
ConfigNamesMapper:: |
protected | property | The language code of the language this mapper, if any. | |
ConfigNamesMapper:: |
protected | property | The language manager. | |
ConfigNamesMapper:: |
protected | property | The typed configuration manager. | |
ConfigNamesMapper:: |
protected | property | The available routes. | |
ConfigNamesMapper:: |
protected | property | The route provider. | |
ConfigNamesMapper:: |
protected | property | The typed config manager. | |
ConfigNamesMapper:: |
public | function |
Adds the given configuration name to the list of names. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the route object for a translation add form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns route name for the translation add form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route parameters for the translation add form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns a processed path for the base route the mapper is attached to. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the base route object the mapper is attached to. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the name of the base route the mapper is attached to. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route parameters for the base route the mapper is attached to. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns an array with all configuration data. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns an array of configuration names for the mapper. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the name of the contextual link group to add contextual links to. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the route object for the translation deletion route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns route name for the translation deletion route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route parameters for the translation deletion route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route object for a translation edit form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns route name for the translation edit form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route parameters for the translation edit form route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the original language code of the configuration. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the language code of a configuration object given its name. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Provides an array of information to build a list of operation links. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns a processed path for the translation overview route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns the route object for a translation overview route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns route name for the translation overview route. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the route parameters for the translation overview route. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Returns title of this translation page. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the label of the type of data the mapper encapsulates. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the name of the type of data the mapper encapsulates. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
public | function |
Returns the weight of the mapper. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Checks that all pieces of this configuration mapper have a schema. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Checks if pieces of this configuration mapper have translatables. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Checks whether there is already a translation for this mapper. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Populate the config mapper with route match data. Overrides ConfigMapperInterface:: |
1 |
ConfigNamesMapper:: |
protected | function | Allows to process all config translation routes. | 1 |
ConfigNamesMapper:: |
public | function |
Sets the original language code. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Sets the route collection. Overrides ConfigMapperInterface:: |
|
ConfigNamesMapper:: |
public | function |
Constructs a ConfigNamesMapper. Overrides PluginBase:: |
1 |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |