annotate core/modules/config_translation/src/ConfigEntityMapper.php @ 19:fa3358dc1485 tip

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