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