Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\config_translation;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Config\ConfigFactoryInterface;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityInterface;
|
Chris@0
|
7 use Drupal\Core\Config\TypedConfigManagerInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
9 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@0
|
10 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
11 use Drupal\Core\Routing\RouteProviderInterface;
|
Chris@0
|
12 use Drupal\Core\StringTranslation\TranslationInterface;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14 use Drupal\locale\LocaleConfigManager;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
16 use Symfony\Component\Routing\Route;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Configuration mapper for configuration entities.
|
Chris@0
|
20 */
|
Chris@0
|
21 class ConfigEntityMapper extends ConfigNamesMapper {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The entity manager.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $entityManager;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Configuration entity type name.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var string
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $entityType;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Loaded entity instance to help produce the translation interface.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $entity;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * The label for the entity type.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var string
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $typeLabel;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Constructs a ConfigEntityMapper.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @param string $plugin_id
|
Chris@0
|
55 * The config mapper plugin ID.
|
Chris@0
|
56 * @param mixed $plugin_definition
|
Chris@0
|
57 * An array of plugin information as documented in
|
Chris@0
|
58 * ConfigNamesMapper::__construct() with the following additional keys:
|
Chris@0
|
59 * - entity_type: The name of the entity type this mapper belongs to.
|
Chris@0
|
60 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
Chris@0
|
61 * The configuration factory.
|
Chris@0
|
62 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
|
Chris@0
|
63 * The typed configuration manager.
|
Chris@0
|
64 * @param \Drupal\locale\LocaleConfigManager $locale_config_manager
|
Chris@0
|
65 * The locale configuration manager.
|
Chris@0
|
66 * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager
|
Chris@0
|
67 * The mapper plugin discovery service.
|
Chris@0
|
68 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
|
Chris@0
|
69 * The route provider.
|
Chris@0
|
70 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
|
Chris@0
|
71 * The string translation manager.
|
Chris@0
|
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
73 * The entity manager.
|
Chris@0
|
74 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
75 * The language manager.
|
Chris@0
|
76 */
|
Chris@0
|
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) {
|
Chris@0
|
78 parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager);
|
Chris@0
|
79 $this->setType($plugin_definition['entity_type']);
|
Chris@0
|
80
|
Chris@0
|
81 $this->entityManager = $entity_manager;
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * {@inheritdoc}
|
Chris@0
|
86 */
|
Chris@0
|
87 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
88 // Note that we ignore the plugin $configuration because mappers have
|
Chris@0
|
89 // nothing to configure in themselves.
|
Chris@0
|
90 return new static (
|
Chris@0
|
91 $plugin_id,
|
Chris@0
|
92 $plugin_definition,
|
Chris@0
|
93 $container->get('config.factory'),
|
Chris@0
|
94 $container->get('config.typed'),
|
Chris@0
|
95 $container->get('locale.config_manager'),
|
Chris@0
|
96 $container->get('plugin.manager.config_translation.mapper'),
|
Chris@0
|
97 $container->get('router.route_provider'),
|
Chris@0
|
98 $container->get('string_translation'),
|
Chris@0
|
99 $container->get('entity.manager'),
|
Chris@0
|
100 $container->get('language_manager')
|
Chris@0
|
101 );
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritdoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function populateFromRouteMatch(RouteMatchInterface $route_match) {
|
Chris@0
|
108 parent::populateFromRouteMatch($route_match);
|
Chris@0
|
109 $entity = $route_match->getParameter($this->entityType);
|
Chris@0
|
110 $this->setEntity($entity);
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Gets the entity instance for this mapper.
|
Chris@0
|
115 *
|
Chris@0
|
116 * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
|
Chris@0
|
117 * The configuration entity.
|
Chris@0
|
118 */
|
Chris@0
|
119 public function getEntity() {
|
Chris@0
|
120 return $this->entity;
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * Sets the entity instance for this mapper.
|
Chris@0
|
125 *
|
Chris@0
|
126 * This method can only be invoked when the concrete entity is known, that is
|
Chris@0
|
127 * in a request for an entity translation path. After this method is called,
|
Chris@0
|
128 * the mapper is fully populated with the proper display title and
|
Chris@0
|
129 * configuration names to use to check permissions or display a translation
|
Chris@0
|
130 * screen.
|
Chris@0
|
131 *
|
Chris@0
|
132 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
|
Chris@0
|
133 * The configuration entity to set.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return bool
|
Chris@0
|
136 * TRUE, if the entity was set successfully; FALSE otherwise.
|
Chris@0
|
137 */
|
Chris@0
|
138 public function setEntity(ConfigEntityInterface $entity) {
|
Chris@0
|
139 if (isset($this->entity)) {
|
Chris@0
|
140 return FALSE;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 $this->entity = $entity;
|
Chris@0
|
144
|
Chris@0
|
145 // Add the list of configuration IDs belonging to this entity. We add on a
|
Chris@0
|
146 // possibly existing list of names. This allows modules to alter the entity
|
Chris@0
|
147 // page with more names if form altering added more configuration to an
|
Chris@0
|
148 // entity. This is not a Drupal 8 best practice (ideally the configuration
|
Chris@0
|
149 // would have pluggable components), but this may happen as well.
|
Chris@0
|
150 /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */
|
Chris@0
|
151 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
|
Chris@0
|
152 $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $entity->id());
|
Chris@0
|
153
|
Chris@0
|
154 return TRUE;
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * {@inheritdoc}
|
Chris@0
|
159 */
|
Chris@0
|
160 public function getTitle() {
|
Chris@0
|
161 return $this->entity->label() . ' ' . $this->pluginDefinition['title'];
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * {@inheritdoc}
|
Chris@0
|
166 */
|
Chris@0
|
167 public function getBaseRouteParameters() {
|
Chris@0
|
168 return [$this->entityType => $this->entity->id()];
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Set entity type for this mapper.
|
Chris@0
|
173 *
|
Chris@0
|
174 * This should be set in initialization. A mapper that knows its type but
|
Chris@0
|
175 * not yet its names is still useful for router item and tab generation. The
|
Chris@0
|
176 * concrete entity only turns out later with actual controller invocations,
|
Chris@0
|
177 * when the setEntity() method is invoked before the rest of the methods are
|
Chris@0
|
178 * used.
|
Chris@0
|
179 *
|
Chris@0
|
180 * @param string $entity_type
|
Chris@0
|
181 * The entity type to set.
|
Chris@0
|
182 *
|
Chris@0
|
183 * @return bool
|
Chris@0
|
184 * TRUE if the entity type was set correctly; FALSE otherwise.
|
Chris@0
|
185 */
|
Chris@0
|
186 public function setType($entity_type) {
|
Chris@0
|
187 if (isset($this->entityType)) {
|
Chris@0
|
188 return FALSE;
|
Chris@0
|
189 }
|
Chris@0
|
190 $this->entityType = $entity_type;
|
Chris@0
|
191 return TRUE;
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * Gets the entity type from this mapper.
|
Chris@0
|
196 *
|
Chris@0
|
197 * @return string
|
Chris@0
|
198 */
|
Chris@0
|
199 public function getType() {
|
Chris@0
|
200 return $this->entityType;
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 /**
|
Chris@0
|
204 * {@inheritdoc}
|
Chris@0
|
205 */
|
Chris@0
|
206 public function getTypeName() {
|
Chris@0
|
207 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
|
Chris@0
|
208 return $entity_type_info->getLabel();
|
Chris@0
|
209 }
|
Chris@0
|
210
|
Chris@0
|
211 /**
|
Chris@0
|
212 * {@inheritdoc}
|
Chris@0
|
213 */
|
Chris@0
|
214 public function getTypeLabel() {
|
Chris@0
|
215 $entityType = $this->entityManager->getDefinition($this->entityType);
|
Chris@0
|
216 return $entityType->getLabel();
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * {@inheritdoc}
|
Chris@0
|
221 */
|
Chris@0
|
222 public function getOperations() {
|
Chris@0
|
223 return [
|
Chris@0
|
224 'list' => [
|
Chris@0
|
225 'title' => $this->t('List'),
|
Chris@0
|
226 'url' => Url::fromRoute('config_translation.entity_list', [
|
Chris@0
|
227 'mapper_id' => $this->getPluginId(),
|
Chris@0
|
228 ]),
|
Chris@0
|
229 ],
|
Chris@0
|
230 ];
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 /**
|
Chris@0
|
234 * {@inheritdoc}
|
Chris@0
|
235 */
|
Chris@0
|
236 public function getContextualLinkGroup() {
|
Chris@0
|
237 // @todo Contextual groups do not map to entity types in a predictable
|
Chris@0
|
238 // way. See https://www.drupal.org/node/2134841 to make them predictable.
|
Chris@0
|
239 switch ($this->entityType) {
|
Chris@0
|
240 case 'menu':
|
Chris@0
|
241 case 'block':
|
Chris@0
|
242 return $this->entityType;
|
Chris@0
|
243 case 'view':
|
Chris@0
|
244 return 'entity.view.edit_form';
|
Chris@0
|
245 default:
|
Chris@0
|
246 return NULL;
|
Chris@0
|
247 }
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 /**
|
Chris@0
|
251 * {@inheritdoc}
|
Chris@0
|
252 */
|
Chris@0
|
253 public function getOverviewRouteName() {
|
Chris@0
|
254 return 'entity.' . $this->entityType . '.config_translation_overview';
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * {@inheritdoc}
|
Chris@0
|
259 */
|
Chris@0
|
260 protected function processRoute(Route $route) {
|
Chris@0
|
261 // Add entity upcasting information.
|
Chris@0
|
262 $parameters = $route->getOption('parameters') ?: [];
|
Chris@0
|
263 $parameters += [
|
Chris@0
|
264 $this->entityType => [
|
Chris@0
|
265 'type' => 'entity:' . $this->entityType,
|
Chris@0
|
266 ]
|
Chris@0
|
267 ];
|
Chris@0
|
268 $route->setOption('parameters', $parameters);
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 }
|