comparison core/modules/config_translation/src/ConfigEntityMapper.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\Routing\Route;
17
18 /**
19 * Configuration mapper for configuration entities.
20 */
21 class ConfigEntityMapper extends ConfigNamesMapper {
22
23 /**
24 * The entity manager.
25 *
26 * @var \Drupal\Core\Entity\EntityManagerInterface
27 */
28 protected $entityManager;
29
30 /**
31 * Configuration entity type name.
32 *
33 * @var string
34 */
35 protected $entityType;
36
37 /**
38 * Loaded entity instance to help produce the translation interface.
39 *
40 * @var \Drupal\Core\Config\Entity\ConfigEntityInterface
41 */
42 protected $entity;
43
44 /**
45 * The label for the entity type.
46 *
47 * @var string
48 */
49 protected $typeLabel;
50
51 /**
52 * Constructs a ConfigEntityMapper.
53 *
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
69 * The route provider.
70 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
71 * The string translation manager.
72 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
73 * The entity manager.
74 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
75 * The language manager.
76 */
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']);
80
81 $this->entityManager = $entity_manager;
82 }
83
84 /**
85 * {@inheritdoc}
86 */
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.
90 return new static (
91 $plugin_id,
92 $plugin_definition,
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')
101 );
102 }
103
104 /**
105 * {@inheritdoc}
106 */
107 public function populateFromRouteMatch(RouteMatchInterface $route_match) {
108 parent::populateFromRouteMatch($route_match);
109 $entity = $route_match->getParameter($this->entityType);
110 $this->setEntity($entity);
111 }
112
113 /**
114 * Gets the entity instance for this mapper.
115 *
116 * @return \Drupal\Core\Config\Entity\ConfigEntityInterface
117 * The configuration entity.
118 */
119 public function getEntity() {
120 return $this->entity;
121 }
122
123 /**
124 * Sets the entity instance for this mapper.
125 *
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
130 * screen.
131 *
132 * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity
133 * The configuration entity to set.
134 *
135 * @return bool
136 * TRUE, if the entity was set successfully; FALSE otherwise.
137 */
138 public function setEntity(ConfigEntityInterface $entity) {
139 if (isset($this->entity)) {
140 return FALSE;
141 }
142
143 $this->entity = $entity;
144
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());
153
154 return TRUE;
155 }
156
157 /**
158 * {@inheritdoc}
159 */
160 public function getTitle() {
161 return $this->entity->label() . ' ' . $this->pluginDefinition['title'];
162 }
163
164 /**
165 * {@inheritdoc}
166 */
167 public function getBaseRouteParameters() {
168 return [$this->entityType => $this->entity->id()];
169 }
170
171 /**
172 * Set entity type for this mapper.
173 *
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
178 * used.
179 *
180 * @param string $entity_type
181 * The entity type to set.
182 *
183 * @return bool
184 * TRUE if the entity type was set correctly; FALSE otherwise.
185 */
186 public function setType($entity_type) {
187 if (isset($this->entityType)) {
188 return FALSE;
189 }
190 $this->entityType = $entity_type;
191 return TRUE;
192 }
193
194 /**
195 * Gets the entity type from this mapper.
196 *
197 * @return string
198 */
199 public function getType() {
200 return $this->entityType;
201 }
202
203 /**
204 * {@inheritdoc}
205 */
206 public function getTypeName() {
207 $entity_type_info = $this->entityManager->getDefinition($this->entityType);
208 return $entity_type_info->getLabel();
209 }
210
211 /**
212 * {@inheritdoc}
213 */
214 public function getTypeLabel() {
215 $entityType = $this->entityManager->getDefinition($this->entityType);
216 return $entityType->getLabel();
217 }
218
219 /**
220 * {@inheritdoc}
221 */
222 public function getOperations() {
223 return [
224 'list' => [
225 'title' => $this->t('List'),
226 'url' => Url::fromRoute('config_translation.entity_list', [
227 'mapper_id' => $this->getPluginId(),
228 ]),
229 ],
230 ];
231 }
232
233 /**
234 * {@inheritdoc}
235 */
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) {
240 case 'menu':
241 case 'block':
242 return $this->entityType;
243 case 'view':
244 return 'entity.view.edit_form';
245 default:
246 return NULL;
247 }
248 }
249
250 /**
251 * {@inheritdoc}
252 */
253 public function getOverviewRouteName() {
254 return 'entity.' . $this->entityType . '.config_translation_overview';
255 }
256
257 /**
258 * {@inheritdoc}
259 */
260 protected function processRoute(Route $route) {
261 // Add entity upcasting information.
262 $parameters = $route->getOption('parameters') ?: [];
263 $parameters += [
264 $this->entityType => [
265 'type' => 'entity:' . $this->entityType,
266 ]
267 ];
268 $route->setOption('parameters', $parameters);
269 }
270
271 }