diff --git a/core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php b/core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php index 89c5914..3f3e9a7 100644 --- a/core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php +++ b/core/lib/Drupal/Core/ParamConverter/AdminPathConfigEntityConverter.php @@ -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(). diff --git a/core/modules/language/src/Tests/AdminPathEntityConverterLanguageTest.php b/core/modules/language/src/Tests/AdminPathEntityConverterLanguageTest.php new file mode 100644 index 0000000..1e74dbc --- /dev/null +++ b/core/modules/language/src/Tests/AdminPathEntityConverterLanguageTest.php @@ -0,0 +1,50 @@ +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'))); + } + +} diff --git a/core/modules/language/tests/language_test/language_test.routing.yml b/core/modules/language/tests/language_test/language_test.routing.yml index bc1168b..4f9a78c 100644 --- a/core/modules/language/tests/language_test/language_test.routing.yml +++ b/core/modules/language/tests/language_test/language_test.routing.yml @@ -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' diff --git a/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php b/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php index 6766d4a..c783d3a 100644 --- a/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php +++ b/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php @@ -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. * @@ -52,6 +56,19 @@ public static function create(ContainerInterface $container) { } /** + * 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. * * Using #type 'link' causes these links to be rendered with _l(). diff --git a/core/modules/system/core.api.php b/core/modules/system/core.api.php index 008e99c..96410cc 100644 --- a/core/modules/system/core.api.php +++ b/core/modules/system/core.api.php @@ -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 *