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\EventDispatcher\EventDispatcherInterface;
17 use Symfony\Component\Routing\Route;
20 * Configuration mapper for configuration entities.
22 class ConfigEntityMapper extends ConfigNamesMapper {
27 * @var \Drupal\Core\Entity\EntityManagerInterface
29 protected $entityManager;
32 * Configuration entity type name.
36 protected $entityType;
39 * Loaded entity instance to help produce the translation interface.
41 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
46 * The label for the entity type.
53 * Constructs a ConfigEntityMapper.
55 * @param string $plugin_id
56 * The config mapper plugin ID.
57 * @param mixed $plugin_definition
58 * An array of plugin information as documented in
59 * ConfigNamesMapper::__construct() with the following additional keys:
60 * - entity_type: The name of the entity type this mapper belongs to.
61 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
62 * The configuration factory.
63 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
64 * The typed configuration manager.
65 * @param \Drupal\locale\LocaleConfigManager $locale_config_manager
66 * The locale configuration manager.
67 * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
68 * The mapper plugin discovery service.
69 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
71 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
72 * The string translation manager.
73 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
75 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
76 * The language manager.
77 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
78 * The event dispatcher.
80 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, EventDispatcherInterface $event_dispatcher = NULL) {
81 parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager, $event_dispatcher);
82 $this->setType($plugin_definition['entity_type']);
84 $this->entityManager = $entity_manager;
90 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
91 // Note that we ignore the plugin $configuration because mappers have
92 // nothing to configure in themselves.
96 $container->get('config.factory'),
97 $container->get('config.typed'),
98 $container->get('locale.config_manager'),
99 $container->get('plugin.manager.config_translation.mapper'),
100 $container->get('router.route_provider'),
101 $container->get('string_translation'),
102 $container->get('entity.manager'),
103 $container->get('language_manager'),
104 $container->get('event_dispatcher')
111 public function populateFromRouteMatch(RouteMatchInterface $route_match) {
112 $entity = $route_match->getParameter($this->entityType);
113 $this->setEntity($entity);
114 parent::populateFromRouteMatch($route_match);
118 * Gets the entity instance for this mapper.
120 * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
121 * The configuration entity.
123 public function getEntity() {
124 return $this->entity;
128 * Sets the entity instance for this mapper.
130 * This method can only be invoked when the concrete entity is known, that is
131 * in a request for an entity translation path. After this method is called,
132 * the mapper is fully populated with the proper display title and
133 * configuration names to use to check permissions or display a translation
136 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
137 * The configuration entity to set.
140 * TRUE, if the entity was set successfully; FALSE otherwise.
142 public function setEntity(ConfigEntityInterface $entity) {
143 if (isset($this->entity)) {
147 $this->entity = $entity;
149 // Add the list of configuration IDs belonging to this entity. We add on a
150 // possibly existing list of names. This allows modules to alter the entity
151 // page with more names if form altering added more configuration to an
152 // entity. This is not a Drupal 8 best practice (ideally the configuration
153 // would have pluggable components), but this may happen as well.
154 /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */
155 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
156 $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $entity->id());
164 public function getTitle() {
165 return $this->entity->label() . ' ' . $this->pluginDefinition['title'];
171 public function getBaseRouteParameters() {
172 return [$this->entityType => $this->entity->id()];
176 * Set entity type for this mapper.
178 * This should be set in initialization. A mapper that knows its type but
179 * not yet its names is still useful for router item and tab generation. The
180 * concrete entity only turns out later with actual controller invocations,
181 * when the setEntity() method is invoked before the rest of the methods are
184 * @param string $entity_type
185 * The entity type to set.
188 * TRUE if the entity type was set correctly; FALSE otherwise.
190 public function setType($entity_type) {
191 if (isset($this->entityType)) {
194 $this->entityType = $entity_type;
199 * Gets the entity type from this mapper.
203 public function getType() {
204 return $this->entityType;
210 public function getTypeName() {
211 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
212 return $entity_type_info->getLabel();
218 public function getTypeLabel() {
219 $entityType = $this->entityManager->getDefinition($this->entityType);
220 return $entityType->getLabel();
226 public function getOperations() {
229 'title' => $this->t('List'),
230 'url' => Url::fromRoute('config_translation.entity_list', [
231 'mapper_id' => $this->getPluginId(),
240 public function getContextualLinkGroup() {
241 // @todo Contextual groups do not map to entity types in a predictable
242 // way. See https://www.drupal.org/node/2134841 to make them predictable.
243 switch ($this->entityType) {
246 return $this->entityType;
248 return 'entity.view.edit_form';
257 public function getOverviewRouteName() {
258 return 'entity.' . $this->entityType . '.config_translation_overview';
264 protected function processRoute(Route $route) {
265 // Add entity upcasting information.
266 $parameters = $route->getOption('parameters') ?: [];
268 $this->entityType => [
269 'type' => 'entity:' . $this->entityType,
272 $route->setOption('parameters', $parameters);