Unverified Commit ffc8ecd9 authored by larowlan's avatar larowlan

Issue #2577761 by maxocub, tstoeckler, andypost, Kristen Pol, rodrigoaguilera,...

Issue #2577761 by maxocub, tstoeckler, andypost, Kristen Pol, rodrigoaguilera, bojanz, larowlan, Wim Leers, vijaycs85: We need a way to dynamically alter the list of config names for config mappers
parent 0f336ae8
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\locale\LocaleConfigManager; use Drupal\locale\LocaleConfigManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
/** /**
...@@ -73,9 +74,11 @@ class ConfigEntityMapper extends ConfigNamesMapper { ...@@ -73,9 +74,11 @@ class ConfigEntityMapper extends ConfigNamesMapper {
* The entity manager. * The entity manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The 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) { 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); 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->setType($plugin_definition['entity_type']);
$this->entityManager = $entity_manager; $this->entityManager = $entity_manager;
...@@ -97,7 +100,8 @@ public static function create(ContainerInterface $container, array $configuratio ...@@ -97,7 +100,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('router.route_provider'), $container->get('router.route_provider'),
$container->get('string_translation'), $container->get('string_translation'),
$container->get('entity.manager'), $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 ...@@ -105,9 +109,9 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc} * {@inheritdoc}
*/ */
public function populateFromRouteMatch(RouteMatchInterface $route_match) { public function populateFromRouteMatch(RouteMatchInterface $route_match) {
parent::populateFromRouteMatch($route_match);
$entity = $route_match->getParameter($this->entityType); $entity = $route_match->getParameter($this->entityType);
$this->setEntity($entity); $this->setEntity($entity);
parent::populateFromRouteMatch($route_match);
} }
/** /**
......
...@@ -282,12 +282,12 @@ public function hasTranslatable(); ...@@ -282,12 +282,12 @@ public function hasTranslatable();
public function hasTranslation(LanguageInterface $language); public function hasTranslation(LanguageInterface $language);
/** /**
* Populate the config mapper with request data. * Populate the config mapper with route match data.
*
* @todo Replace $request with RouteMatch https://www.drupal.org/node/2295255.
* *
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match. * The route match.
*
* @see \Drupal\config_translation\Event\ConfigTranslationEvents::POPULATE_MAPPER
*/ */
public function populateFromRouteMatch(RouteMatchInterface $route_match); public function populateFromRouteMatch(RouteMatchInterface $route_match);
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\config_translation; namespace Drupal\config_translation;
use Drupal\config_translation\Event\ConfigMapperPopulateEvent;
use Drupal\config_translation\Event\ConfigTranslationEvents;
use Drupal\config_translation\Exception\ConfigMapperLanguageException; use Drupal\config_translation\Exception\ConfigMapperLanguageException;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Config\TypedConfigManagerInterface;
...@@ -15,6 +17,7 @@ ...@@ -15,6 +17,7 @@
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\locale\LocaleConfigManager; use Drupal\locale\LocaleConfigManager;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
...@@ -86,6 +89,13 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con ...@@ -86,6 +89,13 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con
*/ */
protected $languageManager; protected $languageManager;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/** /**
* Constructs a ConfigNamesMapper. * Constructs a ConfigNamesMapper.
* *
...@@ -115,12 +125,14 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con ...@@ -115,12 +125,14 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con
* The string translation manager. * The string translation manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager. * The language manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* (optional) The event dispatcher.
* *
* @throws \Symfony\Component\Routing\Exception\RouteNotFoundException * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException
* Throws an exception if the route specified by the 'base_route_name' in * Throws an exception if the route specified by the 'base_route_name' in
* the plugin definition could not be found by the route provider. * 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->pluginId = $plugin_id;
$this->pluginDefinition = $plugin_definition; $this->pluginDefinition = $plugin_definition;
$this->routeProvider = $route_provider; $this->routeProvider = $route_provider;
...@@ -132,6 +144,7 @@ public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterfa ...@@ -132,6 +144,7 @@ public function __construct($plugin_id, $plugin_definition, ConfigFactoryInterfa
$this->stringTranslation = $string_translation; $this->stringTranslation = $string_translation;
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
$this->eventDispatcher = $event_dispatcher ?: \Drupal::service('event_dispatcher');
} }
/** /**
...@@ -149,7 +162,8 @@ public static function create(ContainerInterface $container, array $configuratio ...@@ -149,7 +162,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('plugin.manager.config_translation.mapper'), $container->get('plugin.manager.config_translation.mapper'),
$container->get('router.route_provider'), $container->get('router.route_provider'),
$container->get('string_translation'), $container->get('string_translation'),
$container->get('language_manager') $container->get('language_manager'),
$container->get('event_dispatcher')
); );
} }
...@@ -368,6 +382,9 @@ public function getWeight() { ...@@ -368,6 +382,9 @@ public function getWeight() {
*/ */
public function populateFromRouteMatch(RouteMatchInterface $route_match) { public function populateFromRouteMatch(RouteMatchInterface $route_match) {
$this->langcode = $route_match->getParameter('langcode'); $this->langcode = $route_match->getParameter('langcode');
$event = new ConfigMapperPopulateEvent($this, $route_match);
$this->eventDispatcher->dispatch(ConfigTranslationEvents::POPULATE_MAPPER, $event);
} }
/** /**
......
<?php
namespace Drupal\config_translation\Event;
use Drupal\config_translation\ConfigMapperInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\EventDispatcher\Event;
/**
* Provides a class for events related to configuration translation mappers.
*/
class ConfigMapperPopulateEvent extends Event {
/**
* The configuration mapper this event is related to.
*
* @var \Drupal\config_translation\ConfigMapperInterface
*/
protected $mapper;
/**
* The route match this event is related to.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* Constructs a ConfigMapperPopulateEvent object.
*
* @param \Drupal\config_translation\ConfigMapperInterface $mapper
* The configuration mapper this event is related to.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match this event is related to.
*/
public function __construct(ConfigMapperInterface $mapper, RouteMatchInterface $route_match) {
$this->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;
}
}
<?php
namespace Drupal\config_translation\Event;
/**
* Provides a list of events dispatched by the Configuration Translation module.
*/
final class ConfigTranslationEvents {
/**
* The name of the event dispatched when a configuration mapper is populated.
*
* Allows modules to add related config for translation on a specific
* translation form.
*
* @see \Drupal\config_translation\ConfigMapperInterface::populateFromRouteMatch()
*/
const POPULATE_MAPPER = 'config_translation.populate_mapper';
}
services:
config_translation_test_event_subscriber:
class: Drupal\config_translation_test\EventSubscriber\ConfigTranslationTestSubscriber
tags:
- {name: event_subscriber}
<?php
namespace Drupal\config_translation_test\EventSubscriber;
use Drupal\config_translation\Event\ConfigMapperPopulateEvent;
use Drupal\config_translation\Event\ConfigTranslationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Adds configuration names to configuration mapper on POPULATE_MAPPER event.
*/
class ConfigTranslationTestSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ConfigTranslationEvents::POPULATE_MAPPER => [
['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');
}
}
}
<?php
namespace Drupal\Tests\config_translation\Kernel;
use Drupal\Core\Routing\RouteMatch;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\Routing\Route;
/**
* Tests config mapper.
*
* @group config_translation
*/
class ConfigMapperTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'config_translation',
'config_translation_test',
'language',
'locale',
'system',
];
/**
* Tests adding config names to mapper.
*/
public function testAddingConfigNames() {
// Get a config names mapper.
$mappers = \Drupal::service('plugin.manager.config_translation.mapper')->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);
}
}
...@@ -49,6 +49,13 @@ class ConfigEntityMapperTest extends UnitTestCase { ...@@ -49,6 +49,13 @@ class ConfigEntityMapperTest extends UnitTestCase {
*/ */
protected $languageManager; protected $languageManager;
/**
* The mocked event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventDispatcher;
protected function setUp() { protected function setUp() {
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
...@@ -79,6 +86,8 @@ protected function setUp() { ...@@ -79,6 +86,8 @@ protected function setUp() {
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->configEntityMapper = new ConfigEntityMapper( $this->configEntityMapper = new ConfigEntityMapper(
'configurable_language', 'configurable_language',
$definition, $definition,
...@@ -89,7 +98,8 @@ protected function setUp() { ...@@ -89,7 +98,8 @@ protected function setUp() {
$this->routeProvider, $this->routeProvider,
$this->getStringTranslationStub(), $this->getStringTranslationStub(),
$this->entityManager, $this->entityManager,
$this->languageManager $this->languageManager,
$this->eventDispatcher
); );
} }
......
...@@ -35,6 +35,13 @@ class ConfigFieldMapperTest extends UnitTestCase { ...@@ -35,6 +35,13 @@ class ConfigFieldMapperTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The mocked event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventDispatcher;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -54,6 +61,8 @@ protected function setUp() { ...@@ -54,6 +61,8 @@ protected function setUp() {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->configFieldMapper = new ConfigFieldMapper( $this->configFieldMapper = new ConfigFieldMapper(
'node_fields', 'node_fields',
$definition, $definition,
...@@ -64,7 +73,8 @@ protected function setUp() { ...@@ -64,7 +73,8 @@ protected function setUp() {
$this->getMock('Drupal\Core\Routing\RouteProviderInterface'), $this->getMock('Drupal\Core\Routing\RouteProviderInterface'),
$this->getStringTranslationStub(), $this->getStringTranslationStub(),
$this->entityManager, $this->entityManager,
$this->getMock('Drupal\Core\Language\LanguageManagerInterface') $this->getMock('Drupal\Core\Language\LanguageManagerInterface'),
$this->eventDispatcher
); );
} }
......
...@@ -88,6 +88,13 @@ class ConfigNamesMapperTest extends UnitTestCase { ...@@ -88,6 +88,13 @@ class ConfigNamesMapperTest extends UnitTestCase {
*/ */
protected $languageManager; protected $languageManager;
/**
* The mocked event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $eventDispatcher;
protected function setUp() { protected function setUp() {
$this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface'); $this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
...@@ -122,6 +129,8 @@ protected function setUp() { ...@@ -122,6 +129,8 @@ protected function setUp() {
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->configNamesMapper = new TestConfigNamesMapper( $this->configNamesMapper = new TestConfigNamesMapper(
'system.site_information_settings', 'system.site_information_settings',
$this->pluginDefinition, $this->pluginDefinition,
...@@ -131,7 +140,8 @@ protected function setUp() { ...@@ -131,7 +140,8 @@ protected function setUp() {
$this->configMapperManager, $this->configMapperManager,
$this->routeProvider, $this->routeProvider,
$this->getStringTranslationStub(), $this->getStringTranslationStub(),
$this->languageManager $this->languageManager,
$this->eventDispatcher
); );
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment