diff --git a/core/modules/config_translation/src/ConfigEntityMapper.php b/core/modules/config_translation/src/ConfigEntityMapper.php index faf8991..0367a0a 100644 --- a/core/modules/config_translation/src/ConfigEntityMapper.php +++ b/core/modules/config_translation/src/ConfigEntityMapper.php @@ -13,6 +13,7 @@ use Drupal\Core\Url; use Drupal\locale\LocaleConfigManager; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Routing\Route; /** @@ -73,9 +74,11 @@ class ConfigEntityMapper extends ConfigNamesMapper { * The entity manager. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * The event dispatcher. */ - 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) { - parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager); + 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) { + parent::__construct($plugin_id, $plugin_definition, $config_factory, $typed_config, $locale_config_manager, $config_mapper_manager, $route_provider, $translation_manager, $language_manager, $event_dispatcher); $this->setType($plugin_definition['entity_type']); $this->entityManager = $entity_manager; @@ -97,7 +100,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('router.route_provider'), $container->get('string_translation'), $container->get('entity.manager'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('event_dispatcher') ); } @@ -105,9 +109,9 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function populateFromRouteMatch(RouteMatchInterface $route_match) { - parent::populateFromRouteMatch($route_match); $entity = $route_match->getParameter($this->entityType); $this->setEntity($entity); + parent::populateFromRouteMatch($route_match); } /** diff --git a/core/modules/config_translation/src/ConfigMapperInterface.php b/core/modules/config_translation/src/ConfigMapperInterface.php index 57c1ccd..9bb44b4 100644 --- a/core/modules/config_translation/src/ConfigMapperInterface.php +++ b/core/modules/config_translation/src/ConfigMapperInterface.php @@ -282,12 +282,12 @@ public function hasTranslatable(); public function hasTranslation(LanguageInterface $language); /** - * Populate the config mapper with request data. - * - * @todo Replace $request with RouteMatch https://www.drupal.org/node/2295255. + * Populate the config mapper with route match data. * * @param \Drupal\Core\Routing\RouteMatchInterface $route_match * The route match. + * + * @see \Drupal\config_translation\Event\ConfigTranslationEvents::POPULATE_MAPPER */ public function populateFromRouteMatch(RouteMatchInterface $route_match); diff --git a/core/modules/config_translation/src/ConfigNamesMapper.php b/core/modules/config_translation/src/ConfigNamesMapper.php index 0784090..0193ca8 100644 --- a/core/modules/config_translation/src/ConfigNamesMapper.php +++ b/core/modules/config_translation/src/ConfigNamesMapper.php @@ -15,8 +15,11 @@ use Drupal\Core\Url; use Drupal\locale\LocaleConfigManager; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Drupal\config_translation\Event\ConfigMapperPopulateEvent; +use Drupal\config_translation\Event\ConfigTranslationEvents; /** * Configuration mapper base implementation. @@ -87,6 +90,13 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con protected $languageManager; /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + + /** * Constructs a ConfigNamesMapper. * * @param $plugin_id @@ -115,12 +125,14 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con * The string translation manager. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * The event dispatcher. * * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * Throws an exception if the route specified by the 'base_route_name' in * the plugin definition could not be found by the route provider. */ - 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 $string_translation, LanguageManagerInterface $language_manager) { + 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 $string_translation, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher = NULL) { $this->pluginId = $plugin_id; $this->pluginDefinition = $plugin_definition; $this->routeProvider = $route_provider; @@ -132,6 +144,8 @@ public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterfa $this->stringTranslation = $string_translation; $this->languageManager = $language_manager; + + $this->eventDispatcher = $event_dispatcher ?: \Drupal::service('event_dispatcher'); } /** @@ -149,7 +163,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('plugin.manager.config_translation.mapper'), $container->get('router.route_provider'), $container->get('string_translation'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('event_dispatcher') ); } @@ -367,7 +382,11 @@ public function getWeight() { * {@inheritdoc} */ public function populateFromRouteMatch(RouteMatchInterface $route_match) { - $this->langcode = $route_match->getParameter('langcode'); + $this->setLangcode($route_match->getParameter('langcode')); + + // Dispatches the ConfigTranslationEvents::POPULATE_MAPPER event + $event = new ConfigMapperPopulateEvent($this, $route_match); + $this->eventDispatcher->dispatch(ConfigTranslationEvents::POPULATE_MAPPER, $event); } /** diff --git a/core/modules/config_translation/src/Event/ConfigMapperPopulateEvent.php b/core/modules/config_translation/src/Event/ConfigMapperPopulateEvent.php new file mode 100644 index 0000000..f3c5c3a --- /dev/null +++ b/core/modules/config_translation/src/Event/ConfigMapperPopulateEvent.php @@ -0,0 +1,66 @@ +mapper = $mapper; + $this->route_match = $route_match; + } + + /** + * Gets the configuration mapper this event is related to. + * + * @return \Drupal\config_translation\ConfigMapperInterface + * The configuration mapper this event is related to. + */ + public function getMapper() { + return $this->mapper; + } + + /** + * Gets the route match this event is related to. + * + * @return \Drupal\Core\Routing\RouteMatchInterface + * The route match this event is related to. + */ + public function getRouteMatch() { + return $this->route_match; + } + +} diff --git a/core/modules/config_translation/src/Event/ConfigTranslationEvents.php b/core/modules/config_translation/src/Event/ConfigTranslationEvents.php new file mode 100644 index 0000000..a4bd1a6 --- /dev/null +++ b/core/modules/config_translation/src/Event/ConfigTranslationEvents.php @@ -0,0 +1,23 @@ +getMapper(); + if ($mapper->getBaseRouteName() === 'system.site_information_settings' && $mapper->getLangcode() === 'en') { + $mapper->addConfigName('config_translation_test.content'); + } + } + +} diff --git a/core/modules/config_translation/tests/src/Kernel/ConfigMapperTest.php b/core/modules/config_translation/tests/src/Kernel/ConfigMapperTest.php new file mode 100644 index 0000000..681ce7c --- /dev/null +++ b/core/modules/config_translation/tests/src/Kernel/ConfigMapperTest.php @@ -0,0 +1,52 @@ +getMappers(); + $mapper = $mappers['system.site_information_settings']; + + // Test that it doesn't contain a config name from config_translation_test. + $config_names = $mapper->getConfigNames(); + $this->assertFalse(in_array('config_translation_test.content', $config_names)); + + // Call populateFromRouteMatch() to dispatch an event. + $mapper->populateFromRouteMatch(new RouteMatch('test', new Route('/'))); + + // Test that it now contains the new config name from config_translation_test. + $config_names = $mapper->getConfigNames(); + $this->assertTrue(in_array('config_translation_test.content', $config_names)); + } + +} diff --git a/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php index dc56e8d..57958d8 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php @@ -49,6 +49,13 @@ class ConfigEntityMapperTest extends UnitTestCase { */ protected $languageManager; + /** + * The mocked event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $eventDispatcher; + protected function setUp() { $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); @@ -79,6 +86,8 @@ protected function setUp() { $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->configEntityMapper = new ConfigEntityMapper( 'configurable_language', $definition, @@ -89,7 +98,8 @@ protected function setUp() { $this->routeProvider, $this->getStringTranslationStub(), $this->entityManager, - $this->languageManager + $this->languageManager, + $this->eventDispatcher ); } diff --git a/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php index 204d84e..ce24b3d 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php @@ -36,6 +36,13 @@ class ConfigFieldMapperTest extends UnitTestCase { protected $entityManager; /** + * The mocked event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $eventDispatcher; + + /** * {@inheritdoc} */ protected function setUp() { @@ -54,6 +61,8 @@ protected function setUp() { ->disableOriginalConstructor() ->getMock(); + $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->configFieldMapper = new ConfigFieldMapper( 'node_fields', $definition, @@ -64,7 +73,8 @@ protected function setUp() { $this->getMock('Drupal\Core\Routing\RouteProviderInterface'), $this->getStringTranslationStub(), $this->entityManager, - $this->getMock('Drupal\Core\Language\LanguageManagerInterface') + $this->getMock('Drupal\Core\Language\LanguageManagerInterface'), + $this->eventDispatcher ); } diff --git a/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php index cbf42c8..1ec808d 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php @@ -88,6 +88,13 @@ class ConfigNamesMapperTest extends UnitTestCase { */ protected $languageManager; + /** + * The mocked event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $eventDispatcher; + protected function setUp() { $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface'); @@ -122,6 +129,8 @@ protected function setUp() { $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->configNamesMapper = new TestConfigNamesMapper( 'system.site_information_settings', $this->pluginDefinition, @@ -131,7 +140,8 @@ protected function setUp() { $this->configMapperManager, $this->routeProvider, $this->getStringTranslationStub(), - $this->languageManager + $this->languageManager, + $this->eventDispatcher ); } 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..7a53946 --- /dev/null +++ b/core/modules/field_ui/src/ConfigTranslation/EntityDisplaySubscriber.php @@ -0,0 +1,93 @@ +configFactory = $config_factory; + } + + /** + * Reacts to the population of a configuration mapper. + * + * @param \Drupal\config_translation\Event\ConfigMapperPopulateEvent $event + * The configuration mapper event. + * + * @see \Drupal\field_ui\Routing\RouteSubscriber::alterRoutes() + */ + public function onMapperPopulate(ConfigMapperPopulateEvent $event) { + $bundle = NULL; + $mapper = $event->getMapper(); + $base_route = $mapper->getBaseRoute(); + + if (!($base_route->hasOption('_field_ui_base_route'))) { + return; + } + + $entity_type_id = $base_route->getOption('_field_ui_entity_type_id'); + if ($mapper instanceof ConfigEntityMapper) { + $entity = $mapper->getEntity(); + if ($entity_type_id === $entity->getEntityType()->getBundleOf()) { + // Here, $entity is an extension of + // \Drupal\Core\Config\Entity\ConfigEntityBundleBase, + // so the entity's ID is a bundle name. + $bundle = $entity->id(); + } + } + + if (!$bundle && $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/FieldUiServiceProvider.php b/core/modules/field_ui/src/FieldUiServiceProvider.php new file mode 100644 index 0000000..361b8da --- /dev/null +++ b/core/modules/field_ui/src/FieldUiServiceProvider.php @@ -0,0 +1,33 @@ +getParameter('container.modules'); + + // Subscribe to config_translation's event if it's enabled. + if (isset($modules['config_translation']) ) { + $container->register('field_ui.config_translation_subscriber', '\Drupal\field_ui\ConfigTranslation\EntityDisplaySubscriber') + ->addArgument(new Reference('config.factory')) + ->addTag('event_subscriber'); + } + } + +} diff --git a/core/modules/field_ui/src/Routing/RouteSubscriber.php b/core/modules/field_ui/src/Routing/RouteSubscriber.php index 696a049..a93efc9 100644 --- a/core/modules/field_ui/src/Routing/RouteSubscriber.php +++ b/core/modules/field_ui/src/Routing/RouteSubscriber.php @@ -43,12 +43,25 @@ protected function alterRoutes(RouteCollection $collection) { $path = $entity_route->getPath(); $options = $entity_route->getOptions(); + + // Set route options 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', TRUE); + $entity_route->setOption('_field_ui_entity_type_id', $entity_type_id); + // RouteCollection::add() overwrites exiting routes with the same name. + $collection->add($route_name, $entity_route); + if ($bundle_entity_type = $entity_type->getBundleEntityType()) { $options['parameters'][$bundle_entity_type] = array( 'type' => 'entity:' . $bundle_entity_type, ); } - // Special parameter used to easily recognize all Field UI routes. + // Set route options to easily identify all Field UI routes and to + // determine the entity type the routes are for. + $options['_field_ui_route'] = TRUE; + $options['_field_ui_entity_type_id'] = $entity_type_id; + // This is retained for backwards-compatibility. + // @todo Remove in Drupal 9 $options['_field_ui'] = TRUE; $defaults = array(