diff --git a/core/modules/field_ui/field_ui.services.yml b/core/modules/field_ui/field_ui.services.yml index f6744eb..f62748a 100644 --- a/core/modules/field_ui/field_ui.services.yml +++ b/core/modules/field_ui/field_ui.services.yml @@ -19,3 +19,8 @@ services: arguments: ['@entity.manager'] tags: - { name: access_check, applies_to: _field_ui_form_mode_access } + field_ui.config_translation_subscriber: + class: Drupal\field_ui\ConfigTranslation\EntityDisplaySubscriber + arguments: ['@entity.manager', '@config.factory'] + tags: + - { name: event_subscriber } diff --git a/core/modules/field_ui/src/ConfigTranslation/EntityDisplaySubscriber.php b/core/modules/field_ui/src/ConfigTranslation/EntityDisplaySubscriber.php new file mode 100644 index 0000000..4039c5e --- /dev/null +++ b/core/modules/field_ui/src/ConfigTranslation/EntityDisplaySubscriber.php @@ -0,0 +1,91 @@ +configFactory = $config_factory; + } + + /** + * Reacts to the population of a configuration mapper. + * + * @param \Drupal\config_translation\Event\ConfigMapperEvent $event + * The configuration mapper event. + * + * @see \Drupal\field_ui\Routing\RouteSubscriber::alterRoutes() + */ + public function onMapperPopulate(ConfigMapperEvent $event) { + $mapper = $event->getMapper(); + $base_route = $mapper->getBaseRoute(); + + if ( + !($mapper instanceof ConfigEntityMapper) || + !($entity_type_id = $base_route->getOption('_field_ui_base_route')) + ) { + return; + } + $entity = $mapper->getEntity(); + + // Determine the bundle (if any) of the route. + $bundle = NULL; + if ($entity_type_id === $entity->getEntityType()->getBundleOf()) { + $bundle = $entity->id(); + } + elseif ($base_route->getDefault('bundle')) { + $bundle = $base_route->getDefault('bundle'); + } + + // Add all form and view displays of the respective entity type and bundle + // (if any) to the mapper. + foreach (['entity_form_display', 'entity_view_display'] as $display_type) { + $prefix = "core.$display_type.$entity_type_id."; + if ($bundle) { + $prefix .= "$bundle."; + } + + $config_names = $this->configFactory->listAll($prefix); + foreach ($config_names as $config_name) { + $mapper->addConfigName($config_name); + } + } + + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + return [ConfigTranslationEvents::POPULATE_MAPPER => 'onMapperPopulate']; + } + +} diff --git a/core/modules/field_ui/src/Routing/RouteSubscriber.php b/core/modules/field_ui/src/Routing/RouteSubscriber.php index 8f1b8f1..de6a286 100644 --- a/core/modules/field_ui/src/Routing/RouteSubscriber.php +++ b/core/modules/field_ui/src/Routing/RouteSubscriber.php @@ -45,6 +45,12 @@ protected function alterRoutes(RouteCollection $collection) { if (!$entity_route = $collection->get($route_name)) { continue; } + // Set a route option to easily identify all Field UI base routes and to + // determine the entity type the route is for. + $entity_route->setOption('_field_ui_base_route', $entity_type_id); + // RouteCollection::add() overwrites exiting routes with the same name. + $collection->add($route_name, $entity_route); + $path = $entity_route->getPath(); $options = $entity_route->getOptions(); @@ -53,7 +59,11 @@ protected function alterRoutes(RouteCollection $collection) { 'type' => 'entity:' . $bundle_entity_type, ); } - // Special parameter used to easily recognize all Field UI routes. + // Set a route option to easily identify all Field UI base routes and to + // determine the entity type the routes are for. + $options['_field_ui_route'] = $entity_type_id; + // This is retained for backwards-compatibility. + // @todo Remove in Drupal 9 $options['_field_ui'] = TRUE; $defaults = array(