class ConfigEntityMapper

Same name and namespace in other branches
  1. 9 core/modules/config_translation/src/ConfigEntityMapper.php \Drupal\config_translation\ConfigEntityMapper
  2. 8.9.x core/modules/config_translation/src/ConfigEntityMapper.php \Drupal\config_translation\ConfigEntityMapper
  3. 10 core/modules/config_translation/src/ConfigEntityMapper.php \Drupal\config_translation\ConfigEntityMapper

Configuration mapper for configuration entities.

Hierarchy

Expanded class hierarchy of ConfigEntityMapper

2 files declare their use of ConfigEntityMapper
ConfigEntityMapperTest.php in core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php
NodeTypeMapper.php in core/modules/node/src/ConfigTranslation/NodeTypeMapper.php

File

core/modules/config_translation/src/ConfigEntityMapper.php, line 22

Namespace

Drupal\config_translation
View source
class ConfigEntityMapper extends ConfigNamesMapper implements ConfigEntityMapperInterface {
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Configuration entity type name.
     *
     * @var string
     */
    protected $entityType;
    
    /**
     * Loaded entity instance to help produce the translation interface.
     *
     * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
     */
    protected $entity;
    
    /**
     * The label for the entity type.
     *
     * @var string
     */
    protected $typeLabel;
    
    /**
     * Constructs a ConfigEntityMapper.
     *
     * @param string $plugin_id
     *   The config mapper plugin ID.
     * @param mixed $plugin_definition
     *   An array of plugin information as documented in
     *   ConfigNamesMapper::__construct() with the following additional keys:
     *   - entity_type: The name of the entity type this mapper belongs to.
     * @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 $translation_manager
     *   The string translation manager.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
     *   The language manager.
     * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
     *   The event dispatcher.
     */
    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 $translation_manager, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, ?EventDispatcherInterface $event_dispatcher = NULL) {
        parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager, $event_dispatcher);
        $this->setType($plugin_definition['entity_type']);
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@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('entity_type.manager'), $container->get('language_manager'), $container->get('event_dispatcher'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function populateFromRouteMatch(RouteMatchInterface $route_match) {
        $entity = $route_match->getParameter($this->entityType);
        $this->setEntity($entity);
        parent::populateFromRouteMatch($route_match);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getEntity() {
        return $this->entity;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setEntity(ConfigEntityInterface $entity) {
        if (isset($this->entity)) {
            return FALSE;
        }
        $this->entity = $entity;
        // Add the list of configuration IDs belonging to this entity. We add on a
        // possibly existing list of names. This allows modules to alter the entity
        // page with more names if form altering added more configuration to an
        // entity. This is not a Drupal 8 best practice (ideally the configuration
        // would have pluggable components), but this may happen as well.
        
        /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */
        $entity_type_info = $this->entityTypeManager
            ->getDefinition($this->entityType);
        $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $entity->id());
        return TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTitle() {
        return $this->entity
            ->label() . ' ' . $this->pluginDefinition['title'];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getBaseRouteParameters() {
        return [
            $this->entityType => $this->entity
                ->id(),
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function setType(string $entity_type_id) : bool {
        if (isset($this->entityType)) {
            return FALSE;
        }
        $this->entityType = $entity_type_id;
        return TRUE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getType() : string {
        return $this->entityType;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTypeName() {
        $entity_type_info = $this->entityTypeManager
            ->getDefinition($this->entityType);
        return $entity_type_info->getLabel();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getTypeLabel() {
        $entityType = $this->entityTypeManager
            ->getDefinition($this->entityType);
        return $entityType->getLabel();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getOperations() {
        return [
            'list' => [
                'title' => $this->t('List'),
                'url' => Url::fromRoute('config_translation.entity_list', [
                    'mapper_id' => $this->getPluginId(),
                ]),
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextualLinkGroup() {
        // @todo Contextual groups do not map to entity types in a predictable
        //   way. See https://www.drupal.org/node/2134841 to make them predictable.
        switch ($this->entityType) {
            case 'menu':
            case 'block':
                return $this->entityType;
            case 'view':
                return 'entity.view.edit_form';
            default:
                return NULL;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getOverviewRouteName() {
        return 'entity.' . $this->entityType . '.config_translation_overview';
    }
    
    /**
     * {@inheritdoc}
     */
    protected function processRoute(Route $route) {
        // Add entity upcasting information.
        $parameters = $route->getOption('parameters') ?: [];
        $parameters += [
            $this->entityType => [
                'type' => 'entity:' . $this->entityType,
            ],
        ];
        $route->setOption('parameters', $parameters);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConfigEntityMapper::$entity protected property Loaded entity instance to help produce the translation interface. 1
ConfigEntityMapper::$entityType protected property Configuration entity type name.
ConfigEntityMapper::$entityTypeManager protected property The entity type manager.
ConfigEntityMapper::$typeLabel protected property The label for the entity type.
ConfigEntityMapper::create public static function Creates an instance of the plugin. Overrides ConfigNamesMapper::create
ConfigEntityMapper::getBaseRouteParameters public function Returns the route parameters for the base route the mapper is attached to. Overrides ConfigNamesMapper::getBaseRouteParameters 1
ConfigEntityMapper::getContextualLinkGroup public function Returns the name of the contextual link group to add contextual links to. Overrides ConfigNamesMapper::getContextualLinkGroup
ConfigEntityMapper::getEntity public function Gets the entity instance for this mapper. Overrides ConfigEntityMapperInterface::getEntity
ConfigEntityMapper::getOperations public function Provides an array of information to build a list of operation links. Overrides ConfigNamesMapper::getOperations
ConfigEntityMapper::getOverviewRouteName public function Returns route name for the translation overview route. Overrides ConfigNamesMapper::getOverviewRouteName 1
ConfigEntityMapper::getTitle public function Returns title of this translation page. Overrides ConfigNamesMapper::getTitle
ConfigEntityMapper::getType public function Gets the entity type ID from this mapper. Overrides ConfigEntityMapperInterface::getType
ConfigEntityMapper::getTypeLabel public function Returns the label of the type of data the mapper encapsulates. Overrides ConfigNamesMapper::getTypeLabel 1
ConfigEntityMapper::getTypeName public function Returns the name of the type of data the mapper encapsulates. Overrides ConfigNamesMapper::getTypeName
ConfigEntityMapper::populateFromRouteMatch public function Populate the config mapper with route match data. Overrides ConfigNamesMapper::populateFromRouteMatch
ConfigEntityMapper::processRoute protected function Allows to process all config translation routes. Overrides ConfigNamesMapper::processRoute
ConfigEntityMapper::setEntity public function Sets the entity instance for this mapper. Overrides ConfigEntityMapperInterface::setEntity 2
ConfigEntityMapper::setType public function Set entity type for this mapper. Overrides ConfigEntityMapperInterface::setType
ConfigEntityMapper::__construct public function Constructs a ConfigEntityMapper. Overrides ConfigNamesMapper::__construct
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::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::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::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::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::getOverviewRouteParameters public function Returns the route parameters for the translation overview route. Overrides ConfigMapperInterface::getOverviewRouteParameters
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::setLangcode public function Sets the original language code. Overrides ConfigMapperInterface::setLangcode
ConfigNamesMapper::setRouteCollection public function Sets the route collection. Overrides ConfigMapperInterface::setRouteCollection

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