Chris@0: 'entity.manager', Chris@18: ]; Chris@18: Chris@0: /** Chris@0: * The entity manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Configuration entity type name. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $entityType; Chris@0: Chris@0: /** Chris@0: * Loaded entity instance to help produce the translation interface. Chris@0: * Chris@0: * @var \Drupal\Core\Config\Entity\ConfigEntityInterface Chris@0: */ Chris@0: protected $entity; Chris@0: Chris@0: /** Chris@0: * The label for the entity type. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $typeLabel; Chris@0: Chris@0: /** Chris@0: * Constructs a ConfigEntityMapper. Chris@0: * Chris@0: * @param string $plugin_id Chris@0: * The config mapper plugin ID. Chris@0: * @param mixed $plugin_definition Chris@0: * An array of plugin information as documented in Chris@0: * ConfigNamesMapper::__construct() with the following additional keys: Chris@0: * - entity_type: The name of the entity type this mapper belongs to. Chris@0: * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory Chris@0: * The configuration factory. Chris@0: * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config Chris@0: * The typed configuration manager. Chris@0: * @param \Drupal\locale\LocaleConfigManager $locale_config_manager Chris@0: * The locale configuration manager. Chris@0: * @param \Drupal\config_translation\ConfigMapperManagerInterface $config_mapper_manager Chris@0: * The mapper plugin discovery service. Chris@0: * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider Chris@0: * The route provider. Chris@0: * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager Chris@0: * The string translation manager. Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@0: * The entity manager. Chris@0: * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager Chris@0: * The language manager. Chris@17: * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher Chris@17: * The event dispatcher. Chris@0: */ Chris@18: 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: 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: $this->setType($plugin_definition['entity_type']); Chris@0: Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { Chris@0: // Note that we ignore the plugin $configuration because mappers have Chris@0: // nothing to configure in themselves. Chris@18: return new static( Chris@0: $plugin_id, Chris@0: $plugin_definition, Chris@0: $container->get('config.factory'), Chris@0: $container->get('config.typed'), Chris@0: $container->get('locale.config_manager'), Chris@0: $container->get('plugin.manager.config_translation.mapper'), Chris@0: $container->get('router.route_provider'), Chris@0: $container->get('string_translation'), Chris@18: $container->get('entity_type.manager'), Chris@17: $container->get('language_manager'), Chris@17: $container->get('event_dispatcher') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function populateFromRouteMatch(RouteMatchInterface $route_match) { Chris@0: $entity = $route_match->getParameter($this->entityType); Chris@0: $this->setEntity($entity); Chris@17: parent::populateFromRouteMatch($route_match); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the entity instance for this mapper. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Entity\ConfigEntityInterface Chris@0: * The configuration entity. Chris@0: */ Chris@0: public function getEntity() { Chris@0: return $this->entity; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the entity instance for this mapper. Chris@0: * Chris@0: * This method can only be invoked when the concrete entity is known, that is Chris@0: * in a request for an entity translation path. After this method is called, Chris@0: * the mapper is fully populated with the proper display title and Chris@0: * configuration names to use to check permissions or display a translation Chris@0: * screen. Chris@0: * Chris@0: * @param \Drupal\Core\Config\Entity\ConfigEntityInterface $entity Chris@0: * The configuration entity to set. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE, if the entity was set successfully; FALSE otherwise. Chris@0: */ Chris@0: public function setEntity(ConfigEntityInterface $entity) { Chris@0: if (isset($this->entity)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: $this->entity = $entity; Chris@0: Chris@0: // Add the list of configuration IDs belonging to this entity. We add on a Chris@0: // possibly existing list of names. This allows modules to alter the entity Chris@0: // page with more names if form altering added more configuration to an Chris@0: // entity. This is not a Drupal 8 best practice (ideally the configuration Chris@0: // would have pluggable components), but this may happen as well. Chris@0: /** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $entity_type_info */ Chris@18: $entity_type_info = $this->entityTypeManager->getDefinition($this->entityType); Chris@0: $this->addConfigName($entity_type_info->getConfigPrefix() . '.' . $entity->id()); Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTitle() { Chris@0: return $this->entity->label() . ' ' . $this->pluginDefinition['title']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getBaseRouteParameters() { Chris@0: return [$this->entityType => $this->entity->id()]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set entity type for this mapper. Chris@0: * Chris@0: * This should be set in initialization. A mapper that knows its type but Chris@0: * not yet its names is still useful for router item and tab generation. The Chris@0: * concrete entity only turns out later with actual controller invocations, Chris@0: * when the setEntity() method is invoked before the rest of the methods are Chris@0: * used. Chris@0: * Chris@0: * @param string $entity_type Chris@0: * The entity type to set. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the entity type was set correctly; FALSE otherwise. Chris@0: */ Chris@0: public function setType($entity_type) { Chris@0: if (isset($this->entityType)) { Chris@0: return FALSE; Chris@0: } Chris@0: $this->entityType = $entity_type; Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the entity type from this mapper. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getType() { Chris@0: return $this->entityType; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTypeName() { Chris@18: $entity_type_info = $this->entityTypeManager->getDefinition($this->entityType); Chris@0: return $entity_type_info->getLabel(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTypeLabel() { Chris@18: $entityType = $this->entityTypeManager->getDefinition($this->entityType); Chris@0: return $entityType->getLabel(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOperations() { Chris@0: return [ Chris@0: 'list' => [ Chris@0: 'title' => $this->t('List'), Chris@0: 'url' => Url::fromRoute('config_translation.entity_list', [ Chris@0: 'mapper_id' => $this->getPluginId(), Chris@0: ]), Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getContextualLinkGroup() { Chris@0: // @todo Contextual groups do not map to entity types in a predictable Chris@0: // way. See https://www.drupal.org/node/2134841 to make them predictable. Chris@0: switch ($this->entityType) { Chris@0: case 'menu': Chris@0: case 'block': Chris@0: return $this->entityType; Chris@0: case 'view': Chris@0: return 'entity.view.edit_form'; Chris@0: default: Chris@0: return NULL; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOverviewRouteName() { Chris@0: return 'entity.' . $this->entityType . '.config_translation_overview'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function processRoute(Route $route) { Chris@0: // Add entity upcasting information. Chris@0: $parameters = $route->getOption('parameters') ?: []; Chris@0: $parameters += [ Chris@0: $this->entityType => [ Chris@0: 'type' => 'entity:' . $this->entityType, Chris@17: ], Chris@0: ]; Chris@0: $route->setOption('parameters', $parameters); Chris@0: } Chris@0: Chris@0: }