class ConfigNamesMapper

Same name in other branches
  1. 9 core/modules/config_translation/src/ConfigNamesMapper.php \Drupal\config_translation\ConfigNamesMapper
  2. 8.9.x core/modules/config_translation/src/ConfigNamesMapper.php \Drupal\config_translation\ConfigNamesMapper
  3. 11.x core/modules/config_translation/src/ConfigNamesMapper.php \Drupal\config_translation\ConfigNamesMapper

Configuration mapper base implementation.

Hierarchy

Expanded class hierarchy of ConfigNamesMapper

1 file declares its use of ConfigNamesMapper
ConfigNamesMapperTest.php in core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php

File

core/modules/config_translation/src/ConfigNamesMapper.php, line 27

Namespace

Drupal\config_translation
View 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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigNamesMapper::$baseRoute protected property The base route object that the mapper is attached to.
ConfigNamesMapper::$configFactory protected property The configuration factory.
ConfigNamesMapper::$configMapperManager protected property The mapper plugin discovery service.
ConfigNamesMapper::$eventDispatcher protected property The event dispatcher.
ConfigNamesMapper::$langcode protected property The language code of the language this mapper, if any.
ConfigNamesMapper::$languageManager protected property The language manager.
ConfigNamesMapper::$localeConfigManager protected property The typed configuration manager.
ConfigNamesMapper::$routeCollection protected property The available routes.
ConfigNamesMapper::$routeProvider protected property The route provider.
ConfigNamesMapper::$typedConfigManager protected property The typed config manager.
ConfigNamesMapper::addConfigName public function Adds the given configuration name to the list of names. Overrides ConfigMapperInterface::addConfigName
ConfigNamesMapper::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
ConfigNamesMapper::getAddRoute public function Returns the route object for a translation add form route. Overrides ConfigMapperInterface::getAddRoute
ConfigNamesMapper::getAddRouteName public function Returns route name for the translation add form route. Overrides ConfigMapperInterface::getAddRouteName
ConfigNamesMapper::getAddRouteParameters public function Returns the route parameters for the translation add form route. Overrides ConfigMapperInterface::getAddRouteParameters
ConfigNamesMapper::getBasePath public function Returns a processed path for the base route the mapper is attached to. Overrides ConfigMapperInterface::getBasePath
ConfigNamesMapper::getBaseRoute public function Returns the base route object the mapper is attached to. Overrides ConfigMapperInterface::getBaseRoute
ConfigNamesMapper::getBaseRouteName public function Returns the name of the base route the mapper is attached to. Overrides ConfigMapperInterface::getBaseRouteName
ConfigNamesMapper::getBaseRouteParameters public function Returns the route parameters for the base route the mapper is attached to. Overrides ConfigMapperInterface::getBaseRouteParameters 1
ConfigNamesMapper::getConfigData public function Returns an array with all configuration data. Overrides ConfigMapperInterface::getConfigData
ConfigNamesMapper::getConfigNames public function Returns an array of configuration names for the mapper. Overrides ConfigMapperInterface::getConfigNames
ConfigNamesMapper::getContextualLinkGroup public function Returns the name of the contextual link group to add contextual links to. Overrides ConfigMapperInterface::getContextualLinkGroup 1
ConfigNamesMapper::getDeleteRoute public function Returns the route object for the translation deletion route. Overrides ConfigMapperInterface::getDeleteRoute
ConfigNamesMapper::getDeleteRouteName public function Returns route name for the translation deletion route. Overrides ConfigMapperInterface::getDeleteRouteName
ConfigNamesMapper::getDeleteRouteParameters public function Returns the route parameters for the translation deletion route. Overrides ConfigMapperInterface::getDeleteRouteParameters
ConfigNamesMapper::getEditRoute public function Returns the route object for a translation edit form route. Overrides ConfigMapperInterface::getEditRoute
ConfigNamesMapper::getEditRouteName public function Returns route name for the translation edit form route. Overrides ConfigMapperInterface::getEditRouteName
ConfigNamesMapper::getEditRouteParameters public function Returns the route parameters for the translation edit form route. Overrides ConfigMapperInterface::getEditRouteParameters
ConfigNamesMapper::getLangcode public function Returns the original language code of the configuration. Overrides ConfigMapperInterface::getLangcode
ConfigNamesMapper::getLangcodeFromConfig public function Returns the language code of a configuration object given its name. Overrides ConfigMapperInterface::getLangcodeFromConfig
ConfigNamesMapper::getOperations public function Provides an array of information to build a list of operation links. Overrides ConfigMapperInterface::getOperations 1
ConfigNamesMapper::getOverviewPath public function Returns a processed path for the translation overview route. Overrides ConfigMapperInterface::getOverviewPath
ConfigNamesMapper::getOverviewRoute public function Returns the route object for a translation overview route. Overrides ConfigMapperInterface::getOverviewRoute
ConfigNamesMapper::getOverviewRouteName public function Returns route name for the translation overview route. Overrides ConfigMapperInterface::getOverviewRouteName 1
ConfigNamesMapper::getOverviewRouteParameters public function Returns the route parameters for the translation overview route. Overrides ConfigMapperInterface::getOverviewRouteParameters
ConfigNamesMapper::getTitle public function Returns title of this translation page. Overrides ConfigMapperInterface::getTitle 1
ConfigNamesMapper::getTypeLabel public function Returns the label of the type of data the mapper encapsulates. Overrides ConfigMapperInterface::getTypeLabel 1
ConfigNamesMapper::getTypeName public function Returns the name of the type of data the mapper encapsulates. Overrides ConfigMapperInterface::getTypeName 1
ConfigNamesMapper::getWeight public function Returns the weight of the mapper. Overrides ConfigMapperInterface::getWeight
ConfigNamesMapper::hasSchema public function Checks that all pieces of this configuration mapper have a schema. Overrides ConfigMapperInterface::hasSchema
ConfigNamesMapper::hasTranslatable public function Checks if pieces of this configuration mapper have translatables. Overrides ConfigMapperInterface::hasTranslatable
ConfigNamesMapper::hasTranslation public function Checks whether there is already a translation for this mapper. Overrides ConfigMapperInterface::hasTranslation
ConfigNamesMapper::populateFromRouteMatch public function Populate the config mapper with route match data. Overrides ConfigMapperInterface::populateFromRouteMatch 1
ConfigNamesMapper::processRoute protected function Allows to process all config translation routes. 1
ConfigNamesMapper::setLangcode public function Sets the original language code. Overrides ConfigMapperInterface::setLangcode
ConfigNamesMapper::setRouteCollection public function Sets the route collection. Overrides ConfigMapperInterface::setRouteCollection
ConfigNamesMapper::__construct public function Constructs a ConfigNamesMapper. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.