Sub-task of #1763640: Introduce config context to make original config and different overrides accessible
Problem/Motivation
Currently there is no direct way to activate language specific configuration overrides. One workaround is to create an user with specific language and end up in a user context with this new user, something like below:
$account = entity_create('user', array(
'name' => 'French user',
'mail' => 'test@example.com',
'created' => REQUEST_TIME,
'status' => 1,
'preferred_langcode' => 'fr',
));
$user_config_context = config_context_enter('Drupal\user\UserConfigContext');
$user_config_context->setAccount($account);
The problem with this approach is we can't create new user for every time we need to access config of another language, and this also makes actual user context responding modules believe that a user is present in the context, while we just used it to work around limitations. A good example where this is needed is config_translation module which needs to display admin pages in different specific languages for various site configuration pages.
Proposed solution
Introduce LanguageConfigContext so that the above code changes to:
$language = language_load('fr');
$user_config_context = config_context_enter('Drupal\language\LanguageConfigContext');
$user_config_context->setLanguage($language);
References
Example of where this should be used: #2020347: Temporary fix for config_translation_enter_context until language context becomes an option
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | 2020361-language-config-context-15.patch | 996 bytes | vijaycs85 |
| #11 | 2020361-language-config-context-11.patch | 6.05 KB | vijaycs85 |
| #11 | 2020361-diff-9-11.txt | 1.84 KB | vijaycs85 |
| #9 | 2020361-language-config-context-9.patch | 6.05 KB | vijaycs85 |
| #9 | 2020361-diff-6-9.txt | 3.97 KB | vijaycs85 |
Comments
Comment #1
vijaycs85Initial patch... not sure is it a valid approach. If it is valid, will update test cases.
Comment #2
vijaycs85Initial patch... not sure is it a valid approach. If it is valid, will update test cases.
Comment #3
gábor hojtsyI think this in itself looks good, however I'm not sure the locale event listener will obey this language. How it looks to me is it will attempt to take language from the user, then if that is not there, it will take from the negotiation. Not from a specific language set in the context. Tests here would prove that.
Also found a minor code style issue.
Extra whitespace.
Comment #4
gábor hojtsyComment #5
gábor hojtsyComment #6
vijaycs85Thanks for your review @Gábor Hojtsy. Updated the LocaleEventSubscriber to take language set by LanguageConfigContext on priority. Not sure whether it is OK to override in context iteself (i.e. instead of setting set('language') and assigning to 'locale.language', can we directly set('locale.language') in language contexet set).so far updated test cases are working locally.
Comment #7
gábor hojtsyI think its a mistake to modify the user context tests, they are supposed to test user contexts. Parallel tests for language contexts should be added :)
Comment #8
gábor hojtsyBTW I like how you modified the locale override though; it disconnects the locale computed language from an explicitly set one, which is good. :)
Comment #9
vijaycs85Thanks for the quick review @Gábor Hojtsy. Added new test case for language.
Comment #10
gábor hojtsyLooks good! Except: :)
These all mention "user context", not appropriate here :) just "context" or "language context" sounds good instead.
Comment #11
vijaycs85Fixed #10
Comment #12
gábor hojtsyThis patch looks great. I also made some minor edits on the issue summary to make it read better.
Comment #13
dries commented- 'in current context' should be 'in the current context'.
- 'otherwise' should be 'Otherwise'.
Comment #14
alexpottCommitted beffe8a and pushed to 8.x. Thanks!
Comment #15
vijaycs85Thanks @alexpott and @Dries. Here is the fix for #13.
Comment #16
gábor hojtsyLooks good :)
Comment #17
gábor hojtsyBTW added documentation on language contexts in https://drupal.org/node/1928898/revisions/view/2659996/2735165 and https://drupal.org/node/1928898/revisions/view/2735165/2735167.
Also added this issue to the existing change notice at https://drupal.org/node/1928922
Comment #18
gábor hojtsyComment #19
dries commentedCommitted to 8.x.
Comment #20
gábor hojtsyThanks!
Comment #22
gábor hojtsySuperb, removing from sprint.
Comment #22.0
gábor hojtsyUpdated issue summary.
Comment #23
gábor hojtsy