annotate core/modules/config_translation/src/ConfigEntityMapper.php @ 17:129ea1e6d783

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