Spin-off from #2076445: Make sure language codes for original field values always match entity language regardless of field translatability

EntityInterface explicitly doesn't have a defaultLanguage() method, only a language() method

As @plach explains #2076445-75: Make sure language codes for original field values always match entity language regardless of field translatability (last section of the comment) that is in order not to confuse developpers with "ok, so which one should I use ?".
For the vast majority of use cases, the answer is "just use language(), you shouldn't bother with defaultLanguage() or your code will likely wok in monolingual setups but badly break in multilingual envs"

Yet there *are* some specific cases in core that need to access the default language of an entity, and the only way currently is:
$entity->getUntranslated()->language()->id
- really unfriendly (but according to the above that's more a feature)
- efficiency seems problematic ?

CommentFileSizeAuthor
#2 getUntranslated-2142603-2.patch648 bytesyched
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Performance depends on how often you call it, but in any way, we already have an object, so getUntranslated() checks if it's the default language and just returns itself if yes, otherwise falls back to the existing default language object. So unless we call it *a lot*, I don't think we need to worry too much about it.

yched’s picture

Hm - makes sense I guess...

Looking at the actual methods code then, I don't think getUntranslated() should have $this->translations[$langcode]['entity'] logic, this should stay internal to getTranslation().

Patch attached.

fago’s picture

Status: Active » Needs review
plach’s picture

Well, this will reduce efficiency instead...

Status: Needs review » Needs work

The last submitted patch, 2: getUntranslated-2142603-2.patch, failed testing.

The last submitted patch, 2: getUntranslated-2142603-2.patch, failed testing.

yched’s picture

re: @plach - well, optimizing to save one method call is micro-optimization. Can be debated, but in this case I'd tend to favor proper logic encapsulation.

yched’s picture

Status: Needs work » Needs review

2: getUntranslated-2142603-2.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 2: getUntranslated-2142603-2.patch, failed testing.

plach’s picture

Status: Needs work » Reviewed & tested by the community

That test passes locally and is failing in other unrelated issues too.

plach’s picture

2: getUntranslated-2142603-2.patch queued for re-testing.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

Status: Fixed » Closed (fixed)

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