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 ?
Comment | File | Size | Author |
---|---|---|---|
#2 | getUntranslated-2142603-2.patch | 648 bytes | yched |
Comments
Comment #1
BerdirPerformance 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.
Comment #2
yched CreditAttribution: yched commentedHm - 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.
Comment #3
fagoComment #4
plachWell, this will reduce efficiency instead...
Comment #7
yched CreditAttribution: yched commentedre: @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.
Comment #8
yched CreditAttribution: yched commented2: getUntranslated-2142603-2.patch queued for re-testing.
Comment #10
plachThat test passes locally and is failing in other unrelated issues too.
Comment #11
plach2: getUntranslated-2142603-2.patch queued for re-testing.
Comment #12
catchCommitted/pushed to 8.x, thanks!