diff --git a/core/modules/config_translation/src/ConfigEntityMapper.php b/core/modules/config_translation/src/ConfigEntityMapper.php index 2ae3095933..ba25b48a74 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 57c1ccd27d..9bb44b4884 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 03716e9725..02afedae7a 100644 --- a/core/modules/config_translation/src/ConfigNamesMapper.php +++ b/core/modules/config_translation/src/ConfigNamesMapper.php @@ -2,6 +2,8 @@ namespace Drupal\config_translation; +use Drupal\config_translation\Event\ConfigMapperPopulateEvent; +use Drupal\config_translation\Event\ConfigTranslationEvents; use Drupal\config_translation\Exception\ConfigMapperLanguageException; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\TypedConfigManagerInterface; @@ -15,6 +17,7 @@ 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; @@ -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,23 +125,26 @@ 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 + * (optional) 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. + * + * @todo Make $event_dispatcher required in Drupal 9. */ - 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; - $this->configFactory = $config_factory; $this->typedConfigManager = $typed_config; $this->localeConfigManager = $locale_config_manager; $this->configMapperManager = $config_mapper_manager; - + $this->routeProvider = $route_provider; $this->stringTranslation = $string_translation; $this->languageManager = $language_manager; + $this->eventDispatcher = $event_dispatcher ?: \Drupal::service('event_dispatcher'); } /** @@ -149,7 +162,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') ); } @@ -368,6 +382,9 @@ public function getWeight() { */ public function populateFromRouteMatch(RouteMatchInterface $route_match) { $this->langcode = $route_match->getParameter('langcode'); + + $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 0000000000..e36c41884b --- /dev/null +++ b/core/modules/config_translation/src/Event/ConfigMapperPopulateEvent.php @@ -0,0 +1,61 @@ +mapper = $mapper; + $this->routeMatch = $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->routeMatch; + } + +} 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 0000000000..da392fa5c7 --- /dev/null +++ b/core/modules/config_translation/src/Event/ConfigTranslationEvents.php @@ -0,0 +1,21 @@ + [ + ['addConfigNames'], + ], + ]; + } + + /** + * Reacts to the populating of a configuration mapper. + * + * @param \Drupal\config_translation\Event\ConfigMapperPopulateEvent $event + * The configuration mapper event. + */ + public function addConfigNames(ConfigMapperPopulateEvent $event) { + $mapper = $event->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 0000000000..773246cba9 --- /dev/null +++ b/core/modules/config_translation/tests/src/Kernel/ConfigMapperTest.php @@ -0,0 +1,48 @@ +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->assertNotContains('config_translation_test.content', $config_names); + + // Call populateFromRouteMatch() to dispatch the "config mapper populate" + // event. + $mapper->populateFromRouteMatch(new RouteMatch('test', new Route('/'))); + + // Test that it contains the new config name from config_translation_test. + $config_names = $mapper->getConfigNames(); + $this->assertContains('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 db316caf57..5a4f9d9a42 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 8e6d0b903b..3d17e4641e 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 b099c7fa21..aeb6c4a000 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 ); }