Issue #2295255 by dawehner, tstoeckler, k4v, kgoel: Convert ConfigMapperInterface::populateFromRequest() to use RouteMatch

8.0.x
Nathaniel Catchpole 2015-09-30 17:37:37 +01:00
parent a4bf860d7e
commit 5976b79262
10 changed files with 64 additions and 49 deletions

View File

@ -13,12 +13,12 @@ use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
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\HttpFoundation\Request;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
/** /**
@ -110,9 +110,9 @@ class ConfigEntityMapper extends ConfigNamesMapper {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function populateFromRequest(Request $request) { public function populateFromRouteMatch(RouteMatchInterface $route_match) {
parent::populateFromRequest($request); parent::populateFromRouteMatch($route_match);
$entity = $request->attributes->get($this->entityType); $entity = $route_match->getParameter($this->entityType);
$this->setEntity($entity); $this->setEntity($entity);
} }

View File

@ -8,6 +8,7 @@
namespace Drupal\config_translation; namespace Drupal\config_translation;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
@ -204,6 +205,16 @@ interface ConfigMapperInterface {
*/ */
public function getLangcode(); public function getLangcode();
/**
* Sets the original language code.
*
* @param string $langcode
* The langcode.
*
* @return $this
*/
public function setLangcode($langcode);
/** /**
* Returns the name of the type of data the mapper encapsulates. * Returns the name of the type of data the mapper encapsulates.
* *
@ -267,10 +278,10 @@ interface ConfigMapperInterface {
* *
* @todo Replace $request with RouteMatch https://www.drupal.org/node/2295255. * @todo Replace $request with RouteMatch https://www.drupal.org/node/2295255.
* *
* @param \Symfony\Component\HttpFoundation\Request $request * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* Page request object. * The route match.
*/ */
public function populateFromRequest(Request $request); public function populateFromRouteMatch(RouteMatchInterface $route_match);
/** /**
* Returns the name of the contextual link group to add contextual links to. * Returns the name of the contextual link group to add contextual links to.

View File

@ -13,13 +13,13 @@ use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
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\HttpFoundation\Request;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
@ -371,13 +371,8 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function populateFromRequest(Request $request) { public function populateFromRouteMatch(RouteMatchInterface $route_match) {
if ($request->attributes->has('langcode')) { $this->langcode = $route_match->getParameter('langcode');
$this->langcode = $request->attributes->get('langcode');
}
else {
$this->langcode = NULL;
}
} }
/** /**
@ -407,6 +402,14 @@ class ConfigNamesMapper extends PluginBase implements ConfigMapperInterface, Con
return reset($langcodes); return reset($langcodes);
} }
/**
* {@inheritdoc}
*/
public function setLangcode($langcode) {
$this->langcode = $langcode;
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -13,6 +13,7 @@ use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\PathProcessor\InboundPathProcessorInterface; use Drupal\Core\PathProcessor\InboundPathProcessorInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
@ -122,7 +123,7 @@ class ConfigTranslationController extends ControllerBase {
public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) { public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) {
/** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
$mapper = $this->configMapperManager->createInstance($plugin_id); $mapper = $this->configMapperManager->createInstance($plugin_id);
$mapper->populateFromRequest($request); $mapper->populateFromRouteMatch($route_match);
$page = array(); $page = array();
$page['#title'] = $this->t('Translations for %label', array('%label' => $mapper->getTitle())); $page['#title'] = $this->t('Translations for %label', array('%label' => $mapper->getTitle()));
@ -140,7 +141,7 @@ class ConfigTranslationController extends ControllerBase {
} }
// We create a fake request object to pass into // We create a fake request object to pass into
// ConfigMapperInterface::populateFromRequest() for the different languages. // ConfigMapperInterface::populateFromRouteMatch() for the different languages.
// Creating a separate request for each language and route is neither easily // Creating a separate request for each language and route is neither easily
// possible nor performant. // possible nor performant.
$fake_request = $request->duplicate(); $fake_request = $request->duplicate();
@ -155,8 +156,9 @@ class ConfigTranslationController extends ControllerBase {
// This is needed because // This is needed because
// ConfigMapperInterface::getAddRouteParameters(), for example, // ConfigMapperInterface::getAddRouteParameters(), for example,
// needs to return the correct language code for each table row. // needs to return the correct language code for each table row.
$fake_request->attributes->set('langcode', $langcode); $fake_route_match = RouteMatch::createFromRequest($fake_request);
$mapper->populateFromRequest($fake_request); $mapper->populateFromRouteMatch($fake_route_match);
$mapper->setLangcode($langcode);
// Prepare the language name and the operations depending on whether this // Prepare the language name and the operations depending on whether this
// is the original language or not. // is the original language or not.

View File

@ -8,7 +8,7 @@
namespace Drupal\config_translation\Form; namespace Drupal\config_translation\Form;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request; use Drupal\Core\Routing\RouteMatchInterface;
/** /**
* Defines a form for adding configuration translations. * Defines a form for adding configuration translations.
@ -25,8 +25,8 @@ class ConfigTranslationAddForm extends ConfigTranslationFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
$form = parent::buildForm($form, $form_state, $request, $plugin_id, $langcode); $form = parent::buildForm($form, $form_state, $route_match, $plugin_id, $langcode);
$form['#title'] = $this->t('Add @language translation for %label', array( $form['#title'] = $this->t('Add @language translation for %label', array(
'%label' => $this->mapper->getTitle(), '%label' => $this->mapper->getTitle(),
'@language' => $this->language->getName(), '@language' => $this->language->getName(),

View File

@ -12,6 +12,7 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageManagerInterface; use Drupal\language\ConfigurableLanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
@ -116,10 +117,10 @@ class ConfigTranslationDeleteForm extends ConfirmFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
/** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
$mapper = $this->configMapperManager->createInstance($plugin_id); $mapper = $this->configMapperManager->createInstance($plugin_id);
$mapper->populateFromRequest($request); $mapper->populateFromRouteMatch($route_match);
$language = $this->languageManager->getLanguage($langcode); $language = $this->languageManager->getLanguage($langcode);
if (!$language) { if (!$language) {

View File

@ -8,7 +8,7 @@
namespace Drupal\config_translation\Form; namespace Drupal\config_translation\Form;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request; use Drupal\Core\Routing\RouteMatchInterface;
/** /**
* Defines a form for editing configuration translations. * Defines a form for editing configuration translations.
@ -25,8 +25,8 @@ class ConfigTranslationEditForm extends ConfigTranslationFormBase {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
$form = parent::buildForm($form, $form_state, $request, $plugin_id, $langcode); $form = parent::buildForm($form, $form_state, $route_match, $plugin_id, $langcode);
$form['#title'] = $this->t('Edit @language translation for %label', array( $form['#title'] = $this->t('Edit @language translation for %label', array(
'%label' => $this->mapper->getTitle(), '%label' => $this->mapper->getTitle(),
'@language' => $this->language->getName(), '@language' => $this->language->getName(),

View File

@ -9,6 +9,7 @@ namespace Drupal\config_translation\Form;
use Drupal\config_translation\ConfigMapperManagerInterface; use Drupal\config_translation\ConfigMapperManagerInterface;
use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TraversableTypedDataInterface; use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\Core\Form\BaseFormIdInterface; use Drupal\Core\Form\BaseFormIdInterface;
@ -16,7 +17,6 @@ use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\language\ConfigurableLanguageManagerInterface; use Drupal\language\ConfigurableLanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
@ -132,10 +132,10 @@ abstract class ConfigTranslationFormBase extends FormBase implements BaseFormIdI
* Throws an exception if the language code provided as a query parameter in * Throws an exception if the language code provided as a query parameter in
* the request does not match an active language. * the request does not match an active language.
*/ */
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) {
/** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */
$mapper = $this->configMapperManager->createInstance($plugin_id); $mapper = $this->configMapperManager->createInstance($plugin_id);
$mapper->populateFromRequest($request); $mapper->populateFromRouteMatch($route_match);
$language = $this->languageManager->getLanguage($langcode); $language = $this->languageManager->getLanguage($langcode);
if (!$language) { if (!$language) {

View File

@ -55,8 +55,8 @@ class ConfigTranslationFormTest extends WebTestBase {
*/ */
public function testConfigTranslationFormAlter() { public function testConfigTranslationFormAlter() {
$form_builder = \Drupal::formBuilder(); $form_builder = \Drupal::formBuilder();
$add_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationAddForm', \Drupal::request(), $this->pluginId, $this->langcode); $add_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationAddForm', \Drupal::routeMatch(), $this->pluginId, $this->langcode);
$edit_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationEditForm', \Drupal::request(), $this->pluginId, $this->langcode); $edit_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationEditForm', \Drupal::routeMatch(), $this->pluginId, $this->langcode);
// Test that hook_form_BASE_FORM_ID_alter() was called for the base form ID // Test that hook_form_BASE_FORM_ID_alter() was called for the base form ID
// 'config_translation_form'. // 'config_translation_form'.

View File

@ -11,6 +11,7 @@ use Drupal\config_translation\ConfigNamesMapper;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
@ -239,9 +240,8 @@ class ConfigNamesMapperTest extends UnitTestCase {
* Tests ConfigNamesMapper::getAddRouteParameters(). * Tests ConfigNamesMapper::getAddRouteParameters().
*/ */
public function testGetAddRouteParameters() { public function testGetAddRouteParameters() {
$request = Request::create(''); $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
$request->attributes->set('langcode', 'xx'); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->configNamesMapper->populateFromRequest($request);
$expected = array('langcode' => 'xx'); $expected = array('langcode' => 'xx');
$result = $this->configNamesMapper->getAddRouteParameters(); $result = $this->configNamesMapper->getAddRouteParameters();
@ -278,9 +278,8 @@ class ConfigNamesMapperTest extends UnitTestCase {
* Tests ConfigNamesMapper::getEditRouteParameters(). * Tests ConfigNamesMapper::getEditRouteParameters().
*/ */
public function testGetEditRouteParameters() { public function testGetEditRouteParameters() {
$request = Request::create(''); $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
$request->attributes->set('langcode', 'xx'); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->configNamesMapper->populateFromRequest($request);
$expected = array('langcode' => 'xx'); $expected = array('langcode' => 'xx');
$result = $this->configNamesMapper->getEditRouteParameters(); $result = $this->configNamesMapper->getEditRouteParameters();
@ -317,9 +316,8 @@ class ConfigNamesMapperTest extends UnitTestCase {
* Tests ConfigNamesMapper::getDeleteRouteParameters(). * Tests ConfigNamesMapper::getDeleteRouteParameters().
*/ */
public function testGetDeleteRouteParameters() { public function testGetDeleteRouteParameters() {
$request = Request::create(''); $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
$request->attributes->set('langcode', 'xx'); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->configNamesMapper->populateFromRequest($request);
$expected = array('langcode' => 'xx'); $result = $this->configNamesMapper->getDeleteRouteParameters(); $expected = array('langcode' => 'xx'); $result = $this->configNamesMapper->getDeleteRouteParameters();
$this->assertSame($expected, $result); $this->assertSame($expected, $result);
@ -370,25 +368,25 @@ class ConfigNamesMapperTest extends UnitTestCase {
} }
/** /**
* Tests ConfigNamesMapper::populateFromRequest(). * Tests ConfigNamesMapper::populateFromRouteMatch().
*/ */
public function testPopulateFromRequest() { public function testPopulateFromRouteMatch() {
// Make sure the language code is not set initially. // Make sure the language code is not set initially.
$this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
// Test that an empty request does not set the language code. // Test that an empty request does not set the language code.
$request = Request::create(''); $route_match = new RouteMatch('example', new Route('/test/{langcode}'));
$this->configNamesMapper->populateFromRequest($request); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
// Test that a request with a 'langcode' attribute sets the language code. // Test that a request with a 'langcode' attribute sets the language code.
$request->attributes->set('langcode', 'xx'); $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']);
$this->configNamesMapper->populateFromRequest($request); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->assertSame('xx', $this->configNamesMapper->getInternalLangcode()); $this->assertSame('xx', $this->configNamesMapper->getInternalLangcode());
// Test that the language code gets unset with the wrong request. // Test that the language code gets unset with the wrong request.
$request->attributes->remove('langcode'); $route_match = new RouteMatch('example', new Route('/test/{langcode}'));
$this->configNamesMapper->populateFromRequest($request); $this->configNamesMapper->populateFromRouteMatch($route_match);
$this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode());
} }