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'

I found this issue in #2003570: Enabling RTL and then changing theme colors moves the hexadecimal # to the right.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tstoeckler’s picture

Btw, 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.

tstoeckler’s picture

Ahh, #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.

tstoeckler’s picture

Component: base system » language system
YesCT’s picture

Issue summary: View changes
tim.plunkett’s picture

Honestly, 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.

shnark’s picture

Title: Change values of Language::DIRECTION_(LTR/RTL) to ('ltr'/'rtl') » Change values of LanguageInterface::DIRECTION_(LTR/RTL) to ('ltr'/'rtl')
Issue summary: View changes
Status: Active » Needs review
FileSize
637 bytes

In 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.

YesCT’s picture

yeah, there will probably be default config to change

for example..
core/modules/language/config/install/language.entity.en.yml

id: en
label: English
direction: 0
weight: 0
locked: false
status: true
langcode: en

the value for direction there should be 'ltr'

(and maybe some places in code... maybe that are not using the const)

YesCT’s picture

@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

tim.plunkett’s picture

It'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.

martin107’s picture

direction: 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 :)

Status: Needs review » Needs work

The last submitted patch, 6: change_values_of-2070737-6.patch, failed testing.

shnark’s picture

Status: Needs work » Needs review
FileSize
1.84 KB
1.22 KB

I 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.

shnark’s picture

I forgot to put single quotes around two of the ltrs, I fixed that.

martin107’s picture

Status: Needs review » Needs work

Hi, @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;

tim.plunkett’s picture

@martin107 we can't do that, since they are YAML files

YesCT’s picture

Status: Needs work » Needs review

The last submitted patch, 12: change_values_of-2070737-12.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 13: change_values_of-2070737-13.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
FileSize
4.96 KB
6.55 KB

updated the patch.
I think the current interdiff shows how this can be a cleanup, since most places just need the 'ltr' / 'rtl' strings anyway.

Status: Needs review » Needs work

The last submitted patch, 19: change_values_of-2070737-19.patch, failed testing.

YesCT’s picture

+++ b/core/modules/ckeditor/ckeditor.admin.inc
@@ -61,7 +60,7 @@ function template_preprocess_ckeditor_settings_toolbar(&$variables) {
-  $rtl = $language_direction === 'rtl' ? '_rtl' : '';
+  $rtl = $language_interface->direction === LanguageInterface::DIRECTION_RTL ? '_rtl' : '';

hm. 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

-  $attributes['dir'] = $language_interface->direction ? 'rtl' : 'ltr';
+  $attributes['dir'] = $language_interface->direction;
-  $attributes['dir'] = $language_interface->direction ? 'rtl' : 'ltr';
+  $attributes['dir'] = $language_interface->direction ? DIRECTION_RLT : DIRECTION_LTR;
herom’s picture

Status: Needs work » Needs review
FileSize
515 bytes
7.05 KB

This is green.

hm. another issue to make a const for _rtl? would have to actually read the code to tell.

I don't think so. It's being used in a render array as '#uri' => $button['image' . $rtl],. It's just some local variable.

alexpott’s picture

Status: Needs review » Needs work
Issue tags: +beta deadline
+++ b/core/includes/theme.inc
@@ -1799,7 +1799,7 @@ function template_preprocess_page(&$variables) {
-  $variables['language']->dir     = $language_interface->direction ? 'rtl' : 'ltr';
+  $variables['language']->dir     = $language_interface->direction;

Just 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.

alexpott’s picture

Status: Needs work » Needs review
FileSize
10.42 KB
10.42 KB

That hunk from theme.inc is not even required anymore :) This use case is now handled by DefaultHtmlFragmentRenderer.

Status: Needs review » Needs work

The last submitted patch, 24: 2070737.24.patch, failed testing.

herom’s picture

Status: Needs work » Needs review
FileSize
921 bytes
11.32 KB
vijaycs85’s picture

Issue tags: +D8MI
+++ b/core/includes/theme.inc
@@ -1799,7 +1799,6 @@ function template_preprocess_page(&$variables) {
-  $variables['language']->dir     = $language_interface->direction ? 'rtl' : 'ltr';

Was 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.

vijaycs85’s picture

Status: Needs review » Reviewed & tested by the community

Ok, let's get this in.

catch’s picture

Discussed 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.

tstoeckler’s picture

Awesome!

Wrote a draft change notification.

I will also add a note about this to https://www.drupal.org/node/2011418 after this is committed.

tstoeckler’s picture

dawehner’s picture

Awesome patch!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 26: 2070737.26.patch, failed testing.

martin107’s picture

The failures are the "Too many connections" type which mean,s testbot at the end of a long DRUPALCON has fallen over

Status: Needs work » Needs review

chx queued 26: 2070737.26.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 26: 2070737.26.patch, failed testing.

Status: Needs work » Needs review

herom queued 26: 2070737.26.patch for re-testing.

herom’s picture

Status: Needs review » Reviewed & tested by the community

back to rtbc.

alexpott’s picture

Applying 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.

  • catch committed 7476af9 on 8.0.x
    Issue #2070737 by herom, EllaTheHarpy, alexpott: Change values of...
catch’s picture

Probably 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!

tstoeckler’s picture

Awesome! Published the change notice.

catch’s picture

Status: Reviewed & tested by the community » Fixed
tstoeckler’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.