diff --git a/core/modules/config_translation/src/Access/ConfigTranslationFormAccess.php b/core/modules/config_translation/src/Access/ConfigTranslationFormAccess.php index d7baae3..af584de 100644 --- a/core/modules/config_translation/src/Access/ConfigTranslationFormAccess.php +++ b/core/modules/config_translation/src/Access/ConfigTranslationFormAccess.php @@ -8,8 +8,8 @@ namespace Drupal\config_translation\Access; use Drupal\Core\Access\AccessResult; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; -use Symfony\Component\Routing\Route; /** * Checks access for displaying the translation add, edit, and delete forms. @@ -19,10 +19,10 @@ class ConfigTranslationFormAccess extends ConfigTranslationOverviewAccess { /** * {@inheritdoc} */ - public function access(Route $route, AccountInterface $account, $langcode = NULL) { + public function access(RouteMatchInterface $route_match, AccountInterface $account, $langcode = NULL) { // For the translation forms we have a target language, so we need some // checks in addition to the checks performed for the translation overview. - $base_access = parent::access($route, $account); + $base_access = parent::access($route_match, $account); if ($base_access->isAllowed()) { $target_language = $this->languageManager->getLanguage($langcode); diff --git a/core/modules/config_translation/src/Access/ConfigTranslationOverviewAccess.php b/core/modules/config_translation/src/Access/ConfigTranslationOverviewAccess.php index 237e89a..ec588bb 100644 --- a/core/modules/config_translation/src/Access/ConfigTranslationOverviewAccess.php +++ b/core/modules/config_translation/src/Access/ConfigTranslationOverviewAccess.php @@ -11,8 +11,8 @@ use Drupal\config_translation\ConfigMapperManagerInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Routing\Access\AccessInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; -use Symfony\Component\Routing\Route; /** * Checks access for displaying the configuration translation overview. @@ -54,17 +54,20 @@ public function __construct(ConfigMapperManagerInterface $config_mapper_manager, /** * Checks access to the overview based on permissions and translatability. * - * @param \Symfony\Component\Routing\Route $route - * The route to check against. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The route_match to check against. * @param \Drupal\Core\Session\AccountInterface $account * The currently logged in account. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ - public function access(Route $route, AccountInterface $account) { + public function access(RouteMatchInterface $route_match, AccountInterface $account) { + $route = $route_match->getRouteObject(); + /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ $mapper = $this->configMapperManager->createInstance($route->getDefault('plugin_id')); + $mapper->populateFromRouteMatch($route_match); $this->sourceLanguage = $this->languageManager->getLanguage($mapper->getLangcode()); // Allow access to the translation overview if the proper permission is diff --git a/core/modules/config_translation/src/ConfigEntityMapper.php b/core/modules/config_translation/src/ConfigEntityMapper.php index fd7fc53..541101b 100644 --- a/core/modules/config_translation/src/ConfigEntityMapper.php +++ b/core/modules/config_translation/src/ConfigEntityMapper.php @@ -13,12 +13,12 @@ use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RouteMatch; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Url; use Drupal\locale\LocaleConfigManager; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; /** @@ -110,9 +110,9 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function populateFromRequest(Request $request) { - parent::populateFromRequest($request); - $entity = $request->attributes->get($this->entityType); + public function populateFromRouteMatch(RouteMatchInterface $route_match) { + parent::populateFromRouteMatch($route_match); + $entity = $route_match->getParameter($this->entityType); $this->setEntity($entity); } diff --git a/core/modules/config_translation/src/ConfigMapperInterface.php b/core/modules/config_translation/src/ConfigMapperInterface.php index f794a10..9815f85 100644 --- a/core/modules/config_translation/src/ConfigMapperInterface.php +++ b/core/modules/config_translation/src/ConfigMapperInterface.php @@ -8,6 +8,7 @@ namespace Drupal\config_translation; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\RouteCollection; @@ -205,6 +206,16 @@ public function getConfigData(); 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. * * @return string @@ -267,10 +278,10 @@ public function hasTranslation(LanguageInterface $language); * * @todo Replace $request with RouteMatch https://www.drupal.org/node/2295255. * - * @param \Symfony\Component\HttpFoundation\Request $request - * Page request object. + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * 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. diff --git a/core/modules/config_translation/src/ConfigNamesMapper.php b/core/modules/config_translation/src/ConfigNamesMapper.php index ca4e8d8..4d98e2a 100644 --- a/core/modules/config_translation/src/ConfigNamesMapper.php +++ b/core/modules/config_translation/src/ConfigNamesMapper.php @@ -13,13 +13,13 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\PluginBase; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Url; use Drupal\locale\LocaleConfigManager; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -371,13 +371,8 @@ public function getWeight() { /** * {@inheritdoc} */ - public function populateFromRequest(Request $request) { - if ($request->attributes->has('langcode')) { - $this->langcode = $request->attributes->get('langcode'); - } - else { - $this->langcode = NULL; - } + public function populateFromRouteMatch(RouteMatchInterface $route_match) { + $this->langcode = $route_match->getParameter('langcode'); } /** @@ -410,6 +405,14 @@ public function getLangcode() { /** * {@inheritdoc} */ + public function setLangcode($langcode) { + $this->langcode = $langcode; + return $this; + } + + /** + * {@inheritdoc} + */ public function getConfigData() { $config_data = array(); foreach ($this->getConfigNames() as $name) { @@ -435,11 +438,11 @@ public function hasSchema() { */ public function hasTranslatable() { foreach ($this->getConfigNames() as $name) { - if (!$this->configMapperManager->hasTranslatable($name)) { - return FALSE; + if ($this->configMapperManager->hasTranslatable($name)) { + return TRUE; } } - return TRUE; + return FALSE; } /** diff --git a/core/modules/config_translation/src/Controller/ConfigTranslationController.php b/core/modules/config_translation/src/Controller/ConfigTranslationController.php index 0340758..98ab72b 100644 --- a/core/modules/config_translation/src/Controller/ConfigTranslationController.php +++ b/core/modules/config_translation/src/Controller/ConfigTranslationController.php @@ -13,6 +13,7 @@ use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\PathProcessor\InboundPathProcessorInterface; +use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; @@ -122,7 +123,7 @@ public static function create(ContainerInterface $container) { public function itemPage(Request $request, RouteMatchInterface $route_match, $plugin_id) { /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ $mapper = $this->configMapperManager->createInstance($plugin_id); - $mapper->populateFromRequest($request); + $mapper->populateFromRouteMatch($route_match); $page = array(); $page['#title'] = $this->t('Translations for %label', array('%label' => $mapper->getTitle())); @@ -140,7 +141,7 @@ public function itemPage(Request $request, RouteMatchInterface $route_match, $pl } // 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 // possible nor performant. $fake_request = $request->duplicate(); @@ -155,8 +156,9 @@ public function itemPage(Request $request, RouteMatchInterface $route_match, $pl // This is needed because // ConfigMapperInterface::getAddRouteParameters(), for example, // needs to return the correct language code for each table row. - $fake_request->attributes->set('langcode', $langcode); - $mapper->populateFromRequest($fake_request); + $fake_route_match = RouteMatch::createFromRequest($fake_request); + $mapper->populateFromRouteMatch($fake_route_match); + $mapper->setLangcode($langcode); // Prepare the language name and the operations depending on whether this // is the original language or not. diff --git a/core/modules/config_translation/src/Form/ConfigTranslationAddForm.php b/core/modules/config_translation/src/Form/ConfigTranslationAddForm.php index 883574d..5961c47 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationAddForm.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationAddForm.php @@ -8,7 +8,7 @@ namespace Drupal\config_translation\Form; use Drupal\Core\Form\FormStateInterface; -use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Routing\RouteMatchInterface; /** * Defines a form for adding configuration translations. @@ -25,8 +25,8 @@ public function getFormId() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { - $form = parent::buildForm($form, $form_state, $request, $plugin_id, $langcode); + public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) { + $form = parent::buildForm($form, $form_state, $route_match, $plugin_id, $langcode); $form['#title'] = $this->t('Add @language translation for %label', array( '%label' => $this->mapper->getTitle(), '@language' => $this->language->getName(), diff --git a/core/modules/config_translation/src/Form/ConfigTranslationDeleteForm.php b/core/modules/config_translation/src/Form/ConfigTranslationDeleteForm.php index 0707a74..8e2b669 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationDeleteForm.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationDeleteForm.php @@ -12,6 +12,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\language\ConfigurableLanguageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -116,10 +117,10 @@ public function getFormId() { /** * {@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 */ $mapper = $this->configMapperManager->createInstance($plugin_id); - $mapper->populateFromRequest($request); + $mapper->populateFromRouteMatch($route_match); $language = $this->languageManager->getLanguage($langcode); if (!$language) { diff --git a/core/modules/config_translation/src/Form/ConfigTranslationEditForm.php b/core/modules/config_translation/src/Form/ConfigTranslationEditForm.php index 2c1fc3c..bafbfa2 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationEditForm.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationEditForm.php @@ -8,7 +8,7 @@ namespace Drupal\config_translation\Form; use Drupal\Core\Form\FormStateInterface; -use Symfony\Component\HttpFoundation\Request; +use Drupal\Core\Routing\RouteMatchInterface; /** * Defines a form for editing configuration translations. @@ -25,8 +25,8 @@ public function getFormId() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $plugin_id = NULL, $langcode = NULL) { - $form = parent::buildForm($form, $form_state, $request, $plugin_id, $langcode); + public function buildForm(array $form, FormStateInterface $form_state, RouteMatchInterface $route_match = NULL, $plugin_id = NULL, $langcode = NULL) { + $form = parent::buildForm($form, $form_state, $route_match, $plugin_id, $langcode); $form['#title'] = $this->t('Edit @language translation for %label', array( '%label' => $this->mapper->getTitle(), '@language' => $this->language->getName(), diff --git a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php index 9e5372f..f7b0497 100644 --- a/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php +++ b/core/modules/config_translation/src/Form/ConfigTranslationFormBase.php @@ -9,6 +9,7 @@ use Drupal\config_translation\ConfigMapperManagerInterface; use Drupal\Core\Config\TypedConfigManagerInterface; +use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\Core\TypedData\TraversableTypedDataInterface; use Drupal\Core\Form\BaseFormIdInterface; @@ -16,7 +17,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\language\ConfigurableLanguageManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -132,10 +132,10 @@ public function getBaseFormId() { * Throws an exception if the language code provided as a query parameter in * 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 */ $mapper = $this->configMapperManager->createInstance($plugin_id); - $mapper->populateFromRequest($request); + $mapper->populateFromRouteMatch($route_match); $language = $this->languageManager->getLanguage($langcode); if (!$language) { diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationFormTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationFormTest.php index b162b86..074b654 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationFormTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationFormTest.php @@ -55,8 +55,8 @@ protected function setUp() { */ public function testConfigTranslationFormAlter() { $form_builder = \Drupal::formBuilder(); - $add_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationAddForm', \Drupal::request(), $this->pluginId, $this->langcode); - $edit_form = $form_builder->getForm('Drupal\config_translation\Form\ConfigTranslationEditForm', \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::routeMatch(), $this->pluginId, $this->langcode); // Test that hook_form_BASE_FORM_ID_alter() was called for the base form ID // 'config_translation_form'. diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php index 13890c0..dd3e033 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php @@ -659,7 +659,7 @@ public function testPluralConfigStringsSourceElements() { $config->save(); // Go to the translation page of the 'files' view. - $translation_url = 'admin/structure/views/view/files/translate/' . $langcode . '/add'; + $translation_url = 'admin/structure/views/view/files/translate/en/add'; $this->drupalGet($translation_url); // Check if the expected number of source elements are present. diff --git a/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php b/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php index 796012f..db3dc88 100644 --- a/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php +++ b/core/modules/config_translation/tests/src/Unit/ConfigNamesMapperTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Language\Language; +use Drupal\Core\Routing\RouteMatch; use Drupal\Core\Url; use Drupal\Tests\UnitTestCase; use Symfony\Component\Routing\Route; @@ -239,9 +240,8 @@ public function testGetAddRouteName() { * Tests ConfigNamesMapper::getAddRouteParameters(). */ public function testGetAddRouteParameters() { - $request = Request::create(''); - $request->attributes->set('langcode', 'xx'); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']); + $this->configNamesMapper->populateFromRouteMatch($route_match); $expected = array('langcode' => 'xx'); $result = $this->configNamesMapper->getAddRouteParameters(); @@ -278,9 +278,8 @@ public function testGetEditRouteName() { * Tests ConfigNamesMapper::getEditRouteParameters(). */ public function testGetEditRouteParameters() { - $request = Request::create(''); - $request->attributes->set('langcode', 'xx'); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']); + $this->configNamesMapper->populateFromRouteMatch($route_match); $expected = array('langcode' => 'xx'); $result = $this->configNamesMapper->getEditRouteParameters(); @@ -317,9 +316,8 @@ public function testGetDeleteRouteName() { * Tests ConfigNamesMapper::getDeleteRouteParameters(). */ public function testGetDeleteRouteParameters() { - $request = Request::create(''); - $request->attributes->set('langcode', 'xx'); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']); + $this->configNamesMapper->populateFromRouteMatch($route_match); $expected = array('langcode' => 'xx'); $result = $this->configNamesMapper->getDeleteRouteParameters(); $this->assertSame($expected, $result); @@ -370,25 +368,25 @@ public function testGetWeight() { } /** - * Tests ConfigNamesMapper::populateFromRequest(). + * Tests ConfigNamesMapper::populateFromRouteMatch(). */ - public function testPopulateFromRequest() { + public function testPopulateFromRouteMatch() { // Make sure the language code is not set initially. $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); // Test that an empty request does not set the language code. - $request = Request::create(''); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}')); + $this->configNamesMapper->populateFromRouteMatch($route_match); $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); // Test that a request with a 'langcode' attribute sets the language code. - $request->attributes->set('langcode', 'xx'); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}'), ['langcode' => 'xx']); + $this->configNamesMapper->populateFromRouteMatch($route_match); $this->assertSame('xx', $this->configNamesMapper->getInternalLangcode()); // Test that the language code gets unset with the wrong request. - $request->attributes->remove('langcode'); - $this->configNamesMapper->populateFromRequest($request); + $route_match = new RouteMatch('example', new Route('/test/{langcode}')); + $this->configNamesMapper->populateFromRouteMatch($route_match); $this->assertSame(NULL, $this->configNamesMapper->getInternalLangcode()); } @@ -540,7 +538,7 @@ public function testHasTranslatable(array $mock_return_values, $expected) { $map = array(); foreach ($config_names as $i => $config_name) { - $map[] = array($config_name, $mock_return_values[$i]); + $map[] = isset($mock_return_values[$i]) ? array($config_name, $mock_return_values[$i]) : array(); } $this->configMapperManager ->expects($this->any()) @@ -562,10 +560,12 @@ public function testHasTranslatable(array $mock_return_values, $expected) { */ public function providerTestHasTranslatable() { return array( + array(array(), FALSE), array(array(TRUE), TRUE), array(array(FALSE), FALSE), array(array(TRUE, TRUE, TRUE), TRUE), - array(array(TRUE, FALSE, TRUE), FALSE), + array(array(FALSE, FALSE, FALSE), FALSE), + array(array(TRUE, FALSE, TRUE), TRUE), ); }