diff --git a/core/modules/config_translation/src/ConfigEntityMapper.php b/core/modules/config_translation/src/ConfigEntityMapper.php index 7caf72c..8bf8cd4 100644 --- a/core/modules/config_translation/src/ConfigEntityMapper.php +++ b/core/modules/config_translation/src/ConfigEntityMapper.php @@ -7,6 +7,7 @@ namespace Drupal\config_translation; +use Drupal\config_translation\Event\ConfigTranslationEvents; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\TypedConfigManagerInterface; @@ -18,6 +19,7 @@ use Drupal\Core\Url; use Drupal\locale\LocaleConfigManager; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Routing\Route; /** @@ -74,13 +76,15 @@ class ConfigEntityMapper extends ConfigNamesMapper { * The route provider. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager * The string translation manager. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * The event dispatcher. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity 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) { - 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, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, EntityManagerInterface $entity_manager) { + 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; @@ -101,8 +105,9 @@ 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('entity.manager'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('event_dispatcher'), + $container->get('entity.manager') ); } @@ -110,9 +115,12 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function populateFromRouteMatch(RouteMatchInterface $route_match) { - parent::populateFromRouteMatch($route_match); + $this->setLangcode($route_match->getParameter('langcode')); + $entity = $route_match->getParameter($this->entityType); $this->setEntity($entity); + + $this->dispatchMapperEvent(ConfigTranslationEvents::POPULATE_MAPPER); } /** diff --git a/core/modules/config_translation/src/ConfigMapperInterface.php b/core/modules/config_translation/src/ConfigMapperInterface.php index b56e401..56f3a61 100644 --- a/core/modules/config_translation/src/ConfigMapperInterface.php +++ b/core/modules/config_translation/src/ConfigMapperInterface.php @@ -279,6 +279,8 @@ public function hasTranslation(LanguageInterface $language); * * @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 5d15dd5..dc11df2 100644 --- a/core/modules/config_translation/src/ConfigNamesMapper.php +++ b/core/modules/config_translation/src/ConfigNamesMapper.php @@ -7,6 +7,8 @@ namespace Drupal\config_translation; +use Drupal\config_translation\Event\ConfigMapperEvent; +use Drupal\config_translation\Event\ConfigTranslationEvents; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Language\LanguageInterface; @@ -19,6 +21,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; @@ -91,6 +94,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 @@ -119,12 +129,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) { $this->pluginId = $plugin_id; $this->pluginDefinition = $plugin_definition; $this->routeProvider = $route_provider; @@ -136,6 +148,8 @@ public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterfa $this->stringTranslation = $string_translation; $this->languageManager = $language_manager; + + $this->eventDispatcher = $event_dispatcher; } /** @@ -153,7 +167,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') ); } @@ -371,7 +386,21 @@ public function getWeight() { * {@inheritdoc} */ public function populateFromRouteMatch(RouteMatchInterface $route_match) { - $this->langcode = $route_match->getParameter('langcode'); + $this->setLangcode($route_match->getParameter('langcode')); + $this->dispatchMapperEvent(ConfigTranslationEvents::POPULATE_MAPPER); + } + + /** + * Dispatches the ConfigTranslationEvents::POPULATE_MAPPER event. + * + * @param string $event_name + * The event name of the configuration mapper event to dispatch. + * + * @see \Drupal\config_translation\Event\ConfigTranslationEvents + */ + protected function dispatchMapperEvent($event_name) { + $event = new ConfigMapperEvent($this); + $this->eventDispatcher->dispatch($event_name, $event); } /** diff --git a/core/modules/config_translation/src/Event/ConfigMapperEvent.php b/core/modules/config_translation/src/Event/ConfigMapperEvent.php new file mode 100644 index 0000000..5966563 --- /dev/null +++ b/core/modules/config_translation/src/Event/ConfigMapperEvent.php @@ -0,0 +1,45 @@ +mapper = $mapper; + } + + /** + * 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; + } + +} 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 @@ +entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); @@ -84,6 +91,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, @@ -93,8 +102,9 @@ protected function setUp() { $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'), $this->routeProvider, $this->getStringTranslationStub(), - $this->entityManager, - $this->languageManager + $this->languageManager, + $this->eventDispatcher, + $this->entityManager ); } diff --git a/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php index 3a78357..85fdd87 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigFieldMapperTest.php @@ -59,6 +59,8 @@ protected function setUp() { ->disableOriginalConstructor() ->getMock(); + $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); + $this->configFieldMapper = new ConfigFieldMapper( 'node_fields', $definition, @@ -68,8 +70,9 @@ protected function setUp() { $this->getMock('Drupal\config_translation\ConfigMapperManagerInterface'), $this->getMock('Drupal\Core\Routing\RouteProviderInterface'), $this->getStringTranslationStub(), - $this->entityManager, - $this->getMock('Drupal\Core\Language\LanguageManagerInterface') + $this->getMock('Drupal\Core\Language\LanguageManagerInterface'), + $this->eventDispatcher, + $this->entityManager ); } diff --git a/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php index c9d3c31..3a25aef 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 ); }