3 namespace Drupal\config_translation;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
7 use Drupal\Core\Config\TypedConfigManagerInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Routing\RouteProviderInterface;
12 use Drupal\Core\StringTranslation\TranslationInterface;
14 use Drupal\locale\LocaleConfigManager;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\Routing\Route;
19 * Configuration mapper for configuration entities.
21 class ConfigEntityMapper extends ConfigNamesMapper {
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
31 * Configuration entity type name.
35 protected $entityType;
38 * Loaded entity instance to help produce the translation interface.
40 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
45 * The label for the entity type.
52 * Constructs a ConfigEntityMapper.
54 * @param string $plugin_id
55 * The config mapper plugin ID.
56 * @param mixed $plugin_definition
57 * An array of plugin information as documented in
58 * ConfigNamesMapper::__construct() with the following additional keys:
59 * - entity_type: The name of the entity type this mapper belongs to.
60 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
61 * The configuration factory.
62 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
63 * The typed configuration manager.
64 * @param \Drupal\locale\LocaleConfigManager $locale_config_manager
65 * The locale configuration manager.
66 * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
67 * The mapper plugin discovery service.
68 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
70 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
71 * The string translation manager.
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
74 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
75 * The language manager.
77 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, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager) {
78 parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager);
79 $this->setType($plugin_definition['entity_type']);
81 $this->entityManager = $entity_manager;
87 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
88 // Note that we ignore the plugin $configuration because mappers have
89 // nothing to configure in themselves.
93 $container->get('config.factory'),
94 $container->get('config.typed'),
95 $container->get('locale.config_manager'),
96 $container->get('plugin.manager.config_translation.mapper'),
97 $container->get('router.route_provider'),
98 $container->get('string_translation'),
99 $container->get('entity.manager'),
100 $container->get('language_manager')
107 public function populateFromRouteMatch(RouteMatchInterface $route_match) {
108 parent::populateFromRouteMatch($route_match);
109 $entity = $route_match->getParameter($this->entityType);
110 $this->setEntity($entity);
114 * Gets the entity instance for this mapper.
116 * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
117 * The configuration entity.
119 public function getEntity() {
120 return $this->entity;
124 * Sets the entity instance for this mapper.
126 * This method can only be invoked when the concrete entity is known, that is
127 * in a request for an entity translation path. After this method is called,
128 * the mapper is fully populated with the proper display title and
129 * configuration names to use to check permissions or display a translation
132 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
133 * The configuration entity to set.
136 * TRUE, if the entity was set successfully; FALSE otherwise.
138 public function setEntity(ConfigEntityInterface $entity) {
139 if (isset($this->entity)) {
143 $this->entity = $entity;
145 // Add the list of configuration IDs belonging to this entity. We add on a
146 // possibly existing list of names. This allows modules to alter the entity
147 // page with more names if form altering added more configuration to an
148 // entity. This is not a Drupal 8 best practice (ideally the configuration
149 // would have pluggable components), but this may happen as well.
150 /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */
151 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
152 $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $entity->id());
160 public function getTitle() {
161 return $this->entity->label() . ' ' . $this->pluginDefinition['title'];
167 public function getBaseRouteParameters() {
168 return [$this->entityType => $this->entity->id()];
172 * Set entity type for this mapper.
174 * This should be set in initialization. A mapper that knows its type but
175 * not yet its names is still useful for router item and tab generation. The
176 * concrete entity only turns out later with actual controller invocations,
177 * when the setEntity() method is invoked before the rest of the methods are
180 * @param string $entity_type
181 * The entity type to set.
184 * TRUE if the entity type was set correctly; FALSE otherwise.
186 public function setType($entity_type) {
187 if (isset($this->entityType)) {
190 $this->entityType = $entity_type;
195 * Gets the entity type from this mapper.
199 public function getType() {
200 return $this->entityType;
206 public function getTypeName() {
207 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
208 return $entity_type_info->getLabel();
214 public function getTypeLabel() {
215 $entityType = $this->entityManager->getDefinition($this->entityType);
216 return $entityType->getLabel();
222 public function getOperations() {
225 'title' => $this->t('List'),
226 'url' => Url::fromRoute('config_translation.entity_list', [
227 'mapper_id' => $this->getPluginId(),
236 public function getContextualLinkGroup() {
237 // @todo Contextual groups do not map to entity types in a predictable
238 // way. See https://www.drupal.org/node/2134841 to make them predictable.
239 switch ($this->entityType) {
242 return $this->entityType;
244 return 'entity.view.edit_form';
253 public function getOverviewRouteName() {
254 return 'entity.' . $this->entityType . '.config_translation_overview';
260 protected function processRoute(Route $route) {
261 // Add entity upcasting information.
262 $parameters = $route->getOption('parameters') ?: [];
264 $this->entityType => [
265 'type' => 'entity:' . $this->entityType,
268 $route->setOption('parameters', $parameters);