Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / config_translation / src / ConfigEntityMapper.php
1 <?php
2
3 namespace Drupal\config_translation;
4
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;
13 use Drupal\Core\Url;
14 use Drupal\locale\LocaleConfigManager;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17 use Symfony\Component\Routing\Route;
18
19 /**
20  * Configuration mapper for configuration entities.
21  */
22 class ConfigEntityMapper extends ConfigNamesMapper {
23
24   /**
25    * The entity manager.
26    *
27    * @var \Drupal\Core\Entity\EntityManagerInterface
28    */
29   protected $entityManager;
30
31   /**
32    * Configuration entity type name.
33    *
34    * @var string
35    */
36   protected $entityType;
37
38   /**
39    * Loaded entity instance to help produce the translation interface.
40    *
41    * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
42    */
43   protected $entity;
44
45   /**
46    * The label for the entity type.
47    *
48    * @var string
49    */
50   protected $typeLabel;
51
52   /**
53    * Constructs a ConfigEntityMapper.
54    *
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
70    *   The route provider.
71    * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
72    *   The string translation manager.
73    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
74    *   The 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.
79    */
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']);
83
84     $this->entityManager = $entity_manager;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
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.
93     return new static (
94       $plugin_id,
95       $plugin_definition,
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')
105     );
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function populateFromRouteMatch(RouteMatchInterface $route_match) {
112     $entity = $route_match->getParameter($this->entityType);
113     $this->setEntity($entity);
114     parent::populateFromRouteMatch($route_match);
115   }
116
117   /**
118    * Gets the entity instance for this mapper.
119    *
120    * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
121    *   The configuration entity.
122    */
123   public function getEntity() {
124     return $this->entity;
125   }
126
127   /**
128    * Sets the entity instance for this mapper.
129    *
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
134    * screen.
135    *
136    * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
137    *   The configuration entity to set.
138    *
139    * @return bool
140    *   TRUE, if the entity was set successfully; FALSE otherwise.
141    */
142   public function setEntity(ConfigEntityInterface $entity) {
143     if (isset($this->entity)) {
144       return FALSE;
145     }
146
147     $this->entity = $entity;
148
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());
157
158     return TRUE;
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function getTitle() {
165     return $this->entity->label() . ' ' . $this->pluginDefinition['title'];
166   }
167
168   /**
169    * {@inheritdoc}
170    */
171   public function getBaseRouteParameters() {
172     return [$this->entityType => $this->entity->id()];
173   }
174
175   /**
176    * Set entity type for this mapper.
177    *
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
182    * used.
183    *
184    * @param string $entity_type
185    *   The entity type to set.
186    *
187    * @return bool
188    *   TRUE if the entity type was set correctly; FALSE otherwise.
189    */
190   public function setType($entity_type) {
191     if (isset($this->entityType)) {
192       return FALSE;
193     }
194     $this->entityType = $entity_type;
195     return TRUE;
196   }
197
198   /**
199    * Gets the entity type from this mapper.
200    *
201    * @return string
202    */
203   public function getType() {
204     return $this->entityType;
205   }
206
207   /**
208    * {@inheritdoc}
209    */
210   public function getTypeName() {
211     $entity_type_info = $this->entityManager->getDefinition($this->entityType);
212     return $entity_type_info->getLabel();
213   }
214
215   /**
216    * {@inheritdoc}
217    */
218   public function getTypeLabel() {
219     $entityType = $this->entityManager->getDefinition($this->entityType);
220     return $entityType->getLabel();
221   }
222
223   /**
224    * {@inheritdoc}
225    */
226   public function getOperations() {
227     return [
228       'list' => [
229         'title' => $this->t('List'),
230         'url' => Url::fromRoute('config_translation.entity_list', [
231           'mapper_id' => $this->getPluginId(),
232         ]),
233       ],
234     ];
235   }
236
237   /**
238    * {@inheritdoc}
239    */
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) {
244       case 'menu':
245       case 'block':
246         return $this->entityType;
247       case 'view':
248         return 'entity.view.edit_form';
249       default:
250         return NULL;
251     }
252   }
253
254   /**
255    * {@inheritdoc}
256    */
257   public function getOverviewRouteName() {
258     return 'entity.' . $this->entityType . '.config_translation_overview';
259   }
260
261   /**
262    * {@inheritdoc}
263    */
264   protected function processRoute(Route $route) {
265     // Add entity upcasting information.
266     $parameters = $route->getOption('parameters') ?: [];
267     $parameters += [
268       $this->entityType => [
269         'type' => 'entity:' . $this->entityType,
270       ],
271     ];
272     $route->setOption('parameters', $parameters);
273   }
274
275 }