Updated: Comment #0
Problem/Motivation
It would be cool if in the render array context one could do:
use Drupal\Core\Language\LanguageInterface;
$build['whatever'] = array(
'#attributes' => array('dir' => Language::DIRECTION_RTL),
);
However, LanguageInterface::DIRECTION_LTR
and LanguageInterface::DIRECTION_RTL
currently return 0 and 1, respectively, while the HTML 'dir' attributes expect 'ltr' and 'rtl', respectively.
Additionally, when debug()-ing a $language object, 0 and 1 are not particularly helpful values to know what's going on.
And it makes config difficult to read and diff.
Proposed resolution
Change the constant values to 'ltr' and 'rtl'.
API changes
The constant value of \Drupal\Core\Language\Language::DIRECTION_LTR
has been changed to 'ltr'
and the value of \Drupal\Core\Language\Language::DIRECTION_RTL
has been changed to 'rtl'
Related Issues
I found this issue in #2003570: Enabling RTL and then changing theme colors moves the hexadecimal # to the right.
Comment | File | Size | Author |
---|---|---|---|
#26 | 2070737.26.patch | 11.32 KB | herom |
#26 | interdiff-2070737-24-26.txt | 921 bytes | herom |
#24 | 2070737.24.patch | 10.42 KB | alexpott |
#24 | 22-24-interdiff.txt | 10.42 KB | alexpott |
#22 | change_values_of-2070737-22.patch | 7.05 KB | herom |
Comments
Comment #1
tstoecklerBtw, I would like some confirmation that this makes sense before working on this.
Due to the upgrade path that would be needed, this is a non-trivial undertaking.
Comment #2
tstoecklerAhh, #fail. After frantically searching core for where the {language} table is defined I realized that Languages are actually configuration entities, so no upgrade path needed.
Comment #3
tstoecklerComment #4
YesCT CreditAttribution: YesCT commentedComment #5
tim.plunkettHonestly, the only time I've ever used this constant was when casting it to a bool, to know if RTL was "on" or not.
So this would make more work.
Comment #6
shnark CreditAttribution: shnark commentedIn Drupal/Core/Language/LanguageInterface, I changed it to:
DIRECTION_LTR = 'ltr'
and
DIRECTION_RTL = 'rtl'
.
Uploading to test. There will need to be more changes.
Comment #7
YesCT CreditAttribution: YesCT commentedyeah, there will probably be default config to change
for example..
core/modules/language/config/install/language.entity.en.yml
the value for direction there should be 'ltr'
(and maybe some places in code... maybe that are not using the const)
Comment #8
YesCT CreditAttribution: YesCT commented@tim.plunkett hm. can you remember where that was?
casting 1 to a bool would be true.. so it was like if(bool $direction) { // if rtl
should probably have been if ($direction == LanguageInterface::DIRECTION_RTL) anyway
Comment #9
tim.plunkettIt's in contrib (can't remember where!), not core. And yes, that's better code for sure, I was just stating my experience using it.
Comment #10
martin107 CreditAttribution: martin107 commenteddirection: 0
FWIW a month or so ago there was an issue to sweep all such magic numbers from core...
#2286403: Inconsistent use of magic number when constants DIRECTION_LTR and DIRECTION_RTL already defined.
There is always the change some have crept back in :)
Comment #12
shnark CreditAttribution: shnark commentedI did
$ grep -R 'direction: 0' core/*
it gave
core/modules/language/config/install/language.entity.en.yml:direction: 0
core/modules/language/config/install/language.entity.und.yml:direction: 0
core/modules/language/config/install/language.entity.zxx.yml:direction: 0
In those files, I changed
direction: 0
to
direction: 'ltr'
I looked for direction: 1, however I couldn't find any.
Comment #13
shnark CreditAttribution: shnark commentedI forgot to put single quotes around two of the ltrs, I fixed that.
Comment #14
martin107 CreditAttribution: martin107 commentedHi, @EllaTheHarpy
Can I suggest setting a value of LanguageInterface::DIRECTION_LTR, instead
so the line would look like
direction: LanguageInterface::DIRECTION_LTR,
you may have to add a line at the top like
use Drupal\core\language\languageInterface;
Comment #15
tim.plunkett@martin107 we can't do that, since they are YAML files
Comment #16
YesCT CreditAttribution: YesCT commentedComment #19
herom CreditAttribution: herom commentedupdated the patch.
I think the current interdiff shows how this can be a cleanup, since most places just need the 'ltr' / 'rtl' strings anyway.
Comment #21
YesCT CreditAttribution: YesCT commentedhm. another issue to make a const for _rtl? would have to actually read the code to tell.
----
so some of this is changes since we can now assume direction is always set, right?
If we are worried about changing that assumption here, we could just do
instead of
Comment #22
herom CreditAttribution: herom commentedThis is green.
I don't think so. It's being used in a render array as
'#uri' => $button['image' . $rtl],
. It's just some local variable.Comment #23
alexpottJust call the method in let's stop polluting this object with weird properties. Also considering what we are changing let's protect the property in both Language and ConfigurableLanguage and use the getDirection() method.
Comment #24
alexpottThat hunk from theme.inc is not even required anymore :) This use case is now handled by
DefaultHtmlFragmentRenderer
.Comment #26
herom CreditAttribution: herom commentedFixed the new 'direction: ' config values added in #2336743: When more than one language is added in the profile, the installer ignores those.
Comment #27
vijaycs85Was the only suspect out of this issue scope initially. But found(from git history) that it is not in use and suppose to be removed long time back.
+1 to RTBC.
Comment #28
vijaycs85Ok, let's get this in.
Comment #29
catchDiscussed this with Alex, it's a small change to the YAML but won't have an impact on porting modules (unless they provide default language entities but that seems unlikely). Re-tagging D8 upgrade path/beta target.
Comment #30
tstoecklerAwesome!
Wrote a draft change notification.
I will also add a note about this to https://www.drupal.org/node/2011418 after this is committed.
Comment #31
tstoecklerComment #32
dawehnerAwesome patch!
Comment #34
martin107 CreditAttribution: martin107 commentedThe failures are the "Too many connections" type which mean,s testbot at the end of a long DRUPALCON has fallen over
Comment #38
herom CreditAttribution: herom commentedback to rtbc.
Comment #39
alexpottApplying the rules outlined in #2350615: [policy, no patch] What changes can be accepted during the Drupal 8 beta phase? this change is bc breaking without the possibility of adding a bc layer. It is only a normal task - If this change was not rtbc'd at or worked on at Amsterdam we should not proceed but because it was, I think we can.
Comment #41
catchProbably spent more time per line of code talking about this patch than any other at DrupalCon Amsterdam when trying to figure out exactly what the implications of post-beta criteria were. Glad we found a way to apply the criteria consistently but also not punt this to 9.x due to the close review.
Committed/pushed to 8.0.x, thanks!
Comment #42
tstoecklerAwesome! Published the change notice.
Comment #43
catchComment #44
tstoecklerMinor nice-to-have follow-up: #2353393: Use LanguageInterface::DIRECTION_LTR instead of 'ltr' directly