Commit be1661a1 authored by Dries's avatar Dries

git commit -m 'Issue #2369035 by jhodgdon, Gábor Hojtsy, robertdbailey: Config...

git commit -m 'Issue #2369035 by jhodgdon, Gábor Hojtsy, robertdbailey: Config entities should not always be untranslated in admin routes'
parent dbde59f0
......@@ -18,7 +18,9 @@
* Converts entity route arguments to unmodified entities as opposed to
* converting to entities with overrides, such as the negotiated language.
*
* This converter applies only if the path is an admin path.
* This converter applies only if the path is an admin path, the entity is
* a config entity, and the "use_current_language" element is not set to TRUE
* on the parameter definition.
*
* Due to this converter having a higher weight than the default
* EntityConverter, every time this applies, it takes over the conversion duty
......@@ -88,6 +90,10 @@ public function convert($value, $definition, $name, array $defaults) {
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route) {
if (isset($definition['use_current_language']) && $definition['use_current_language']) {
return FALSE;
}
if (parent::applies($definition, $name, $route)) {
$entity_type_id = substr($definition['type'], strlen('entity:'));
// If the entity type is dynamic, defer checking to self::convert().
......
<?php
/**
* @file
* Contains \Drupal\language\Tests\AdminPathEntityConverterLanguageTest.
*/
namespace Drupal\language\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
* Test administration path based conversion of entities.
*
* @group language
*/
class AdminPathEntityConverterLanguageTest extends WebTestBase {
public static $modules = array('language', 'language_test');
protected function setUp() {
parent::setUp();
$permissions = array(
'access administration pages',
'administer site configuration',
);
$this->drupalLogin($this->drupalCreateUser($permissions));
ConfigurableLanguage::createFromLangcode('es')->save();
}
/**
* Tests the translated and untranslated config entities are loaded properly.
*/
public function testConfigUsingCurrentLanguage() {
\Drupal::languageManager()
->getLanguageConfigOverride('es', 'language.entity.es')
->set('label', 'Español')
->save();
$this->drupalGet('es/admin/language_test/entity_using_current_language/es');
$this->assertNoRaw(t('Loaded %label.', array('%label' => 'Spanish')));
$this->assertRaw(t('Loaded %label.', array('%label' => 'Español')));
$this->drupalGet('es/admin/language_test/entity_using_original_language/es');
$this->assertRaw(t('Loaded %label.', array('%label' => 'Spanish')));
$this->assertNoRaw(t('Loaded %label.', array('%label' => 'Español')));
}
}
......@@ -11,3 +11,23 @@ language_test.subrequest:
_controller: '\Drupal\language_test\Controller\LanguageTestController::testSubRequest'
requirements:
_access: 'TRUE'
language_test.entity_using_original_language:
path: '/admin/language_test/entity_using_original_language/{configurable_language}'
defaults:
_controller: '\Drupal\language_test\Controller\LanguageTestController::testEntity'
requirements:
_access: 'TRUE'
language_test.entity_using_current_language:
path: '/admin/language_test/entity_using_current_language/{configurable_language}'
defaults:
_controller: '\Drupal\language_test\Controller\LanguageTestController::testEntity'
requirements:
_access: 'TRUE'
options:
parameters:
configurable_language:
type: entity:configurable_language
# Force load in current interface language.
use_current_language: 'TRUE'
......@@ -9,7 +9,9 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
......@@ -19,6 +21,8 @@
*/
class LanguageTestController implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The HTTP kernel service.
*
......@@ -51,6 +55,19 @@ public static function create(ContainerInterface $container) {
return new static($container->get('http_kernel'), $container->get('language_manager'));
}
/**
* Route entity upcasting test helper.
*
* @param \Drupal\language\ConfigurableLanguageInterface $language
* The ConfigurableLanguage object from the route.
*
* @return string
* Testing feedback based on (translated) entity title.
*/
public function testEntity(ConfigurableLanguageInterface $configurable_language) {
return array('#markup' => $this->t('Loaded %label.', array('%label' => $configurable_language->label())));
}
/**
* Returns links to the current page with different langcodes.
*
......
......@@ -321,6 +321,31 @@
* Configuration entity classes expose dependencies by overriding the
* \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies()
* method.
* - On routes for paths staring with '\admin' with configuration entity
* placeholders, configuration entities are normally loaded in their original
* language, not translated. This is usually desirable, because most admin
* paths are for editing configuration, and you need that to be in the source
* language. If for some reason you need to have your configuration entity
* loaded in the currently-selected language on an admin path (for instance,
* if you go to example.com/es/admin/your_path and you need the entity to be
* in Spanish), then you can add a 'use_current_language' parameter option to
* your route. Here's an example using the configurable_language config
* entity:
* @code
* mymodule.myroute:
* path: '/admin/mypath/{configurable_language}'
* defaults:
* _controller: '\Drupal\mymodule\MyController::myMethod'
* options:
* parameters:
* configurable_language:
* type: entity:configurable_language
* use_current_language: 'TRUE'
* @endcode
* With the route defined this way, the $configurable_language parameter to
* your controller method will come in translated to the current language.
* Without the parameter options section, it would be in the original
* language, untranslated.
*
* @see i18n
*
......
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