Problem/Motivation

#2513094: ContentEntityBase::getTranslatedField and ContentEntityBase::__clone break field reference to parent entity apparently introduced a nasty regression that all the reviewers missed. There was test coverage, but it was changed by the patch.

See #2540556: Translated paragraphs is not displayed in the correct translation, this broke tests in paragraphs. I reproduced it manually with an entity reference to nodes as well:

Viewing a german node, with a translated paragraph and referencing a node that also has a german translation, both the paragraph and the node is displayed in the EN version.:

We have some test coverage for this in EntityReferenceFieldTranslatedReferenceViewTest but that uses a translatable field. Which is actually a rather uncommon case when you are displaying translated references, since the reference usually doesn't change.

I'm not sure if I fully understand the problem, but have a look at the following line in EntityReferenceFormatter:getEntitiesToView():

$parent_entity_langcode = $items->getEntity()->language()->getId();

Because $items is a non-translatable field, it points to the original translation of the parent entity, so that's en. Somehow, we need to know there that the entity that we are displaying is actually being displayed in german. And i have no idea how to get that information now. We can *not* rely on the current content language, since we want to display the reference in the same language as the parent entity, if possible. There's a second bug here, and that's that it should use getTranslationFromContext(), to fall back to the best possible available translation.

Proposed resolution

Pass $langcode as an optional argument to view(), default it there to the current content language and pass it through to viewElements() and getEntitiesToView(), then use that language to get the referenced entity, using the proper API's.

Remaining tasks

None

User interface changes

None

API changes

See CR: https://www.drupal.org/node/2575729

public function view(FieldItemListInterface $items);
+  public function view(FieldItemListInterface $items, $langcode = NULL);
-  public function viewElements(FieldItemListInterface $items);
+  public function viewElements(FieldItemListInterface $items, $langcode);

Data model changes

None

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because this breaks translatable entity references
Issue priority Major because it is not a data loss or security issue but it is a serious problem for I18N and a contributed modules blocker.
Prioritized changes None of the official priorized changes points, but it is a major bug, the only viable solution and a contributed modules blocker without workaround.
Disruption Minimally disruptive for contrib and custom field formatters but trivial to update.
CommentFileSizeAuthor
#52 entity_references_of-2543258-52-interdiff.txt9.67 KBBerdir
#52 entity_references_of-2543258-52.patch46.7 KBBerdir
#50 interdiff_46_48.txt11.22 KBLKS90
#50 entity_references_of-2543258-48.patch50.03 KBLKS90
#46 interdiff_39-46.txt42.38 KBLKS90
#46 entity_references_of-2543258-46.patch51.39 KBLKS90
#43 entity_references_of-2543258-42.patch47.75 KBtassilogroeper
#43 interdiff_2543258-39-42.txt13.51 KBtassilogroeper
#41 interdiff_2543258-39-41.txt78.11 KBtassilogroeper
#41 entity_references_of-2543258-41.patch52.11 KBtassilogroeper
#39 interdiff_35-39.txt1.83 KBLKS90
#39 entity_references_of-2543258-39.patch52.04 KBLKS90
#35 entity_references_of-2543258-35.patch52.25 KBLKS90
#30 entity_references_of-2543258-30.patch52.28 KBLKS90
#29 entity_references_of-2543258-29.patch52.2 KBLKS90
#24 interdiff_23-24.txt3.3 KBLKS90
#24 entity_references_of-2543258-24.patch52.2 KBLKS90
#23 interdiff_21-23.txt35.29 KBLKS90
#23 entity_references_of-2543258-23.patch50.84 KBLKS90
#21 interdiff_18-21.txt9.25 KBLKS90
#21 entity_references_of-2543258-21.patch17.9 KBLKS90
#18 interdiff_16-18.txt9.93 KBLKS90
#18 entity_references_of-2543258-18.patch16.7 KBLKS90
#16 2543258-16.patch10.57 KBhchonov
#16 interdiff-10-16.txt1.33 KBhchonov
#16 interdiff-7-16.txt1.51 KBhchonov
#13 interdiff_10-13.txt1.28 KBLKS90
#13 entity_references_of-2543258-13.patch12.48 KBLKS90
#10 entity_references_of-2543258-10-interdiff.txt1.66 KBsasanikolic
#10 entity_references_of-2543258-10.patch12.07 KBsasanikolic
#7 6-7-interdiff.txt1.72 KBhchonov
#7 2543258-7.patch9.7 KBhchonov
#6 entity_references_of-2543258-6-interdiff.txt4.96 KBsasanikolic
#6 entity_references_of-2543258-6.patch9.12 KBsasanikolic
#5 2543258-5.patch5.53 KBhchonov
#1 2513094-44.patch4.58 KBBerdir
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Status: Active » Needs review
FileSize
4.58 KB

Patch from @plach from the other issue.

Status: Needs review » Needs work

The last submitted patch, 1: 2513094-44.patch, failed testing.

hchonov’s picture

That behaviour was expected, I also mentioned it in the other issue:
#2513094-22: ContentEntityBase::getTranslatedField and ContentEntityBase::__clone break field reference to parent entity

So with my changes you still can see the translated referenced entity on the referer view, but for that to happen you should make the entity reference field translatable.

Berdir’s picture

You might have expected it, but it is wrong.

Whether the *field* is translatable isn't related to in which language the referenced entity is displayed. Or isn't supposed to.

The field being translatable just means that you can have *different* references (e.g. a different image). IMHO, the 80% use case for references is to make them not translatable and point to the same reference, which is then displaying the correct translation.

hchonov’s picture

Status: Needs work » Needs review
FileSize
5.53 KB

Making ImageFormatterBase::getEntitiesToView compatible to EntityReferenceFormatterBase::getEntitiesToView.

sasanikolic’s picture

Created a subclass for EntityReferenceFieldTranslatedReferenceViewTest that sets the fields to untranslatable, reverted the trait from the other patch, fixed the test warning and added a @param for the langcode.

hchonov’s picture

resolved some nitpicks.

I would say, now the changes reflecting ER from the other issue are reverted properly and with the additional changes from plach we get back the right behaviour.

Berdir’s picture

Issue summary: View changes
Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -76,8 +76,8 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    -  public function view(FieldItemListInterface $items) {
    -    $elements = $this->viewElements($items);
    +  public function view(FieldItemListInterface $items, $langcode = NULL) {
    +    $elements = $this->viewElements($items, $langcode);
    

    We need to add $langcode to FormatterInterface and document it.

    What are we going to do about FieldItem(List)Interface::view() and the bunch of methods that it calls until it ends up in EntityViewDisplay::buildMultiple() again?

    Because that also calls $item->getEntity(), so the entity is then again in the wrong language... should we extend that as well with a $langcode column?

  2. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php
    @@ -36,24 +36,30 @@
    +    // TODO
    +    if (!isset($langcode)) {
    +      $langcode = $items->getEntity()->language()->getId();
    +    }
    

    Also, we discussed in IRC that $langcode NULL should default to the current content language. I guess viewElements() and so on would then have $langcode as a required parameter and can always rely on it.

    Then the fallback here can do away.

  3. +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldUntranslatedReferenceViewTest.php
    @@ -0,0 +1,24 @@
    +namespace Drupal\entity_reference\Tests;
    +
    +
    

    too many empty lines.

  4. +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldUntranslatedReferenceViewTest.php
    @@ -0,0 +1,24 @@
    +class EntityReferenceFieldUntranslatedReferenceViewTest extends EntityReferenceFieldTranslatedReferenceViewTest {
    +  /**
    

    missing empty line between class and docblock.

yched’s picture

Something bugs me here.

The original code in EntityReferenceFormatterBase::getEntitiesToView() tries to display referenced entities in the same language than the containing entity (which, agreed, is the behavior we want), with this code :

$parent_entity_langcode = $items->getEntity()->language()->getId();
if ($entity instanceof TranslatableInterface && $entity->hasTranslation($parent_entity_langcode)) {
  $entity = $entity->getTranslation($parent_entity_langcode);
}

This worked before #2513094: ContentEntityBase::getTranslatedField and ContentEntityBase::__clone break field reference to parent entity. If it doesn't now, this seems to mean we now have :

$fr_entity = $orig_entity->getTranslation('fr');
$fr_entity->translatable_field->getEntity()->language()->getId() == 'fr' 
$fr_entity->untranslatable_field->getEntity()->language()->getId() == 'en';

Meaning,

$fr_entity->translatable_field->getEntity() === $fr_entity
$fr_entity->untranslatable_field->getEntity() === $orig_entity

That looks very wrong ?
$entity->field->getEntity() sould be === $entity, whether the $entity is translated or not, and the field translatable or not ?

sasanikolic’s picture

Added $langcode to FormatterInterface, corrected the test.

Status: Needs review » Needs work

The last submitted patch, 10: entity_references_of-2543258-10.patch, failed testing.

plach’s picture

That looks very wrong ?
$entity->field->getEntity() sould be === $entity, whether the $entity is translated or not, and the field translatable or not ?

We cannot have that: untranslatable field items are shared among translation objects.

LKS90’s picture

Status: Needs work » Needs review
FileSize
12.48 KB
1.28 KB

I amended the test so it should pass tests instead of causing exceptions (that the exception happens is the whole point of the test, isn't it?). Is no one working on the issue currently or what's the status? I am waiting for a fix so I can continue with this issuse.

Edit: Nope, I think we want the test to pass again?

LKS90’s picture

Berdir’s picture

Status: Needs review » Needs work

That test is completely unrelated and shouldn't be in here it was accidently added from a different issue/patch.

hchonov’s picture

Status: Needs work » Needs review
FileSize
1.51 KB
1.33 KB
10.57 KB

removed the test.

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -76,8 +76,8 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    -  public function view(FieldItemListInterface $items) {
    -    $elements = $this->viewElements($items);
    +  public function view(FieldItemListInterface $items, $langcode = NULL) {
    +    $elements = $this->viewElements($items, $langcode);
    

    if empty($langcode) then use the current content language, see EntityViewBuilder.

  2. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php
    @@ -36,24 +36,30 @@
        */
    -  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) {
    +  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode = NULL) {
    

    As discussed, $langcode should be a required argument at this point.

  3. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php
    @@ -36,24 +36,30 @@
    +    // TODO
    +    if (!isset($langcode)) {
    +      $langcode = $items->getEntity()->language()->getId();
    +    }
    

    Then this can go away completely.

  4. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceLabelFormatter.php
    @@ -60,11 +60,11 @@ public function settingsSummary() {
        */
    -  public function viewElements(FieldItemListInterface $items) {
    +  public function viewElements(FieldItemListInterface $items, $langcode = NULL) {
    

    viewElements() also has $langcode no longer optional then.

  5. +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php
    @@ -18,6 +19,12 @@
       /**
    +   * Flag indicating whether the field is translatable.
    +   *
    +   * @var bool
    +   */
    +  protected $translatable = TRUE;
    +  /**
        * The langcode of the source language.
    

    Missin empty lines above the docblocks.

  6. +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
    @@ -19,7 +19,7 @@
        */
    -  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) {
    +  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode = NULL) {
         // Add the default image if needed.
    

    Update this too.

LKS90’s picture

Status: Needs work » Needs review
FileSize
16.7 KB
9.93 KB

Here is your feedback implemented.
I had a problem with point 4 though:

viewElements() also has $langcode no longer optional then.

The viewElements() method has to match the FormatterInterface, which we can't modify anymore. So I left the $langcode = NULL there and use the language manager to get the current language in case the the langcode isn't defined.

Edit:

Uhhh...?

Exception message

Status: Needs review » Needs work

The last submitted patch, 18: entity_references_of-2543258-18.patch, failed testing.

Berdir’s picture

  1. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -71,12 +82,16 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    +    $this->languageManager = $language_manager;
    

    There are around 10 formatters that have their own injection. This requires that every single of them gets changed + all those in contrib that are doing it "correctly".

    I suggest you just access the language manager directly where you need it.

  2. +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php
    @@ -19,7 +20,7 @@
        */
    -  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode = NULL) {
    +  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
    

    looks like you tried to make the $langcode change here.

LKS90’s picture

Status: Needs work » Needs review
FileSize
17.9 KB
9.25 KB

Here is an updated patch, using \Drupal::languageManager instead of modifying the constructor.
Also fixed a couple of errors I had which complained about the missing $langcode attribute.

Status: Needs review » Needs work

The last submitted patch, 21: entity_references_of-2543258-21.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
FileSize
50.84 KB
35.29 KB

Here is the complete version. All core formatters that use viewElements now have the langcode as a parameter, where necessary I added the languageManager()->getCurrentLanguage() to ensure we always have the langcode.

LKS90’s picture

Forgot some usages of LanguageInterface, added them.

miro_dietiker’s picture

Nice!
But if the use statements from #24 are needed why didn't the patch in #23 fail?

LKS90’s picture

The tests all supply a langcode, it seems. When I add the line $langcode = NULL; before any of the missing Interfaces, the tests fail and complain about the missing interface.

plach’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, thanks :)

+++ b/core/lib/Drupal/Core/Field/FormatterBase.php
@@ -76,8 +77,11 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
+    if (empty($langcode)) {
+      $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
+    }
+    $elements = $this->viewElements($items, $langcode);

Not to be fixed here, but I'm wondering whether it would make sense to have a trait encapsulating this logic once and for all. We have already many instances of it in core...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 24: entity_references_of-2543258-24.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
FileSize
52.2 KB

Here is a freshly rebased patch. This one should apply again.

LKS90’s picture

Another rebase, if someone could finally review this thing and help get this committed, that would be awesome!

yched’s picture

Status: Needs review » Reviewed & tested by the community

Was RTBC before, so back to RTBC if the reroll is green ?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 30: entity_references_of-2543258-30.patch, failed testing.

The last submitted patch, 30: entity_references_of-2543258-30.patch, failed testing.

LKS90’s picture

+++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
@@ -1,3 +1,4 @@
+

That's not supposed to be there...

LKS90’s picture

Status: Needs work » Needs review
FileSize
52.25 KB

Here is the correctly rebased version.

LKS90’s picture

Status: Needs review » Reviewed & tested by the community

Test green, setting back to RTBC.
Edit: I also checked all changes this time, I'm not just relying on the testbot :).

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. The amount of formatters changed in this patch does not seem to be match the added test coverage. The only tests that seem to be improved are the entity reference tests - the formatter where the bug was noticed. But every other formatter has changed - are we missing coverage?
  2. +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php
    @@ -10,6 +10,7 @@
    +use Symfony\Component\Validator\Constraints\False;
    

    Not used.

  3. +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php
    @@ -41,6 +42,20 @@ class ImageFormatter extends ImageFormatterBase implements ContainerFactoryPlugi
       /**
    +   * The link generator.
    +   *
    +   * @var \Drupal\Core\Utility\LinkGeneratorInterface
    +   */
    +  protected $linkGenerator;
    

    Whilst this might be correct it is totally out of scope.

alexpott’s picture

Issue tags: +rc target

Given the solution on this issue I think we have to get this fixed before the release candidate - tagging as such.

LKS90’s picture

Status: Needs work » Needs review
FileSize
52.04 KB
1.83 KB

Feedback implemented, mostly:

1. The interface change forced me to update a lot of classes that might need testing, I'm not really sure, someone who knows what could go wrong could possibly create an issue to extend test coverage for untranslatable fields and their content in a translated entity (I can understand the issue with entity_references, but I have a hard time to imagine the same scenario for the ImageFormatter, as an example).

2. removed

3. removed

tassilogroeper’s picture

Just to be sure, there are several scenarios we are talking about:

  • a) entity reference field is not translatable, pointing to a not translated eg. taxonomy term
  • b) entity reference field is not translatable, pointing to a translated eg. taxonomy term (current language included)
  • c) entity reference field is not translatable, pointing to a translated eg. taxonomy term (current language NOT included, using the default language of the taxonomy term)
  • d) entity reference field translatable, pointing to a not translated eg. taxonomy term (only default language)
  • e) entity reference field translatable, pointing to a translated eg. taxonomy term with current or default fall back language

so the bug used to mess up scenario b) resulting in only rendering the default language of the referenced entity (eg. taxonomy term)
hopefully this helps a bit

tassilogroeper’s picture

I had a look at the code base. If we would abstract away the check for the langcode, we'd end up with a much cleaner code. Hope this will help a bit.

Status: Needs review » Needs work

The last submitted patch, 41: entity_references_of-2543258-41.patch, failed testing.

tassilogroeper’s picture

Berdir’s picture

+++ core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php	(revision 45ec61e78c457fc6e8c59cf5e49a1e5cdf490d43)
@@ -187,4 +189,21 @@
+   *
+   * @return string
+   */
+  private function safeLangcode($langcode) {

Missing description on return. We usually also don't use private unless we have a really good reason. Not sure about the name, safe sounds like it has something to do with security (safe markup), but it really doesn't. maybe just make it really explicit, with getLangcodeOrFallback() or something like that.

I'd rather not make the users of $langcode responsible for figuring out the default. I'm still not sure about the $langcode = NULL in the view() method. It was just an attempt to not change the interface but if we add it in the FormatterInterface, then we can just as well make it required there so that it's clear that can not be NULL.

The last submitted patch, 41: entity_references_of-2543258-41.patch, failed testing.

LKS90’s picture

Status: Needs work » Needs review
FileSize
51.39 KB
42.38 KB

Alright, I made the $langcode non-optional, updated all the formatters and added a method which set's the langcode in case it isn't defined yet.
Still no additional tests, I was thinking about adding one for the EntityReferenceTaxonomyTermRssFormatter, but I'd like to have some feedback about the general patch first (like a testbot run) :).

yched’s picture

  1. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -110,6 +112,15 @@ public function view(FieldItemListInterface $items) {
    +  public function setMissingLangcode(&$langcode) {
    +    if (empty($langcode)) {
    +      $langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
    +    }
    +  }
    

    That's... really weird :-)

    - A method named as a setter, that doesn't set anything but instead adjusts the passed param by ref. When you see a setter you expect it changes some state in the object, so that makes you think Formatters hold a state on langcode, that's misleading.

    - That's just helper code for "fallback to some langcode if I don't have one", I'm not sure it belongs to Formatters / FormatterInterface. I'd really like to avoid adding kludge to FormatterInterface.

    - Also, it's feels really a bummer that each (ER) formatter seems to have to call the method in various places (the patch adds calls from within view() implementations and getEntitiesToView() implementations) ?
    What case is it actually needed for ? What is this trying to cover ?

  2. +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php
    @@ -36,24 +37,25 @@
    +   * @param $langcode
    +   *   The language code of entities.
        *
        * @return \Drupal\Core\Entity\EntityInterface[]
        *   The array of referenced entities to display, keyed by delta.
        *
        * @see ::prepareView()
        */
    -  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items) {
    +  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
    

    phpdoc misses the typehint, and the description is not really accurate. In the context of that method, that's the langcode in which we want to display the referenced entities.

kataku’s picture

Status: Needs review » Active

#46 - I've applied this to the head of 8.0.x and tested it as part of my investigation of #2540556 and can confirm that it fixes this issue.

LKS90’s picture

Status: Active » Needs review

I've renamed the helper method (setMissingLangcode -> languageFallback), better suggestions are welcome.
The method is not in the FormatterInterface anymore.
I only call it once in the FormatterBase now, let's see if it is necessary to call the method in other classes.
I also fixed the missing type in the phpdocblock and added the information that it is the language code of the entities to reference.

More test coverage is necessary, but I'm still not sure how to actually test it. (Currently, one test has the added $translatable = true; property and extends the same test but sets the translatable to false, can I do the same for tests which are also field formatters of entity references?)

LKS90’s picture

Here is the patch, had some problems after some comment suddenly appeared :D.

Berdir’s picture

  1. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -76,8 +77,9 @@ public function __construct($plugin_id, $plugin_definition, FieldDefinitionInter
    +  public function view(FieldItemListInterface $items, $langcode) {
    +    $this->languageFallback($langcode);
    +    $elements = $this->viewElements($items, $langcode);
    

    We only need a fallback in the place(s) where langcode is an optional argument. For the methods where we make it required, we don't.

    view() is the main entry point that only has a single override. So lets make it optional here with $langcode = NULL and ensure that we have it set. Then viewElements() and everything below that can assume that the correct langcode is set.

    Looking at the patch, I see that's basically what you are doing now, it's just that the argument actually needs to be optional now and the documentation fixed, see below.

  2. +++ b/core/lib/Drupal/Core/Field/FormatterBase.php
    @@ -108,6 +110,20 @@ public function view(FieldItemListInterface $items) {
    +   * Sets the language code variable to the current user interface language.
    

    As @yched said, this doesn't set anything, it returns.

    Also, if we only have a single call left, then we don't really need a helper method for that now, do we? That would avoid that problem (naming, documentation).

  3. +++ b/core/lib/Drupal/Core/Field/FormatterInterface.php
    @@ -69,23 +69,29 @@ public function prepareView(array $entities_items);
        * @param \Drupal\Core\Field\FieldItemListInterface $items
        *   The field values to be rendered.
    +   * @param string $langcode
    +   *   The language that should be used to render the field. Defaults to the
    +   *   current content language.
        *
        * @return array
        *   A renderable array for a themed field with its label and all its values.
        */
    -  public function view(FieldItemListInterface $items);
    +  public function view(FieldItemListInterface $items, $langcode);
     
       /**
        * Builds a renderable array for a field value.
        *
        * @param \Drupal\Core\Field\FieldItemListInterface $items
        *   The field values to be rendered.
    +   * @param string $langcode
    +   *   (optional) The language that should be used to render the field. Defaults
    +   *   to the current content language.
        *
        * @return array
        *   A renderable array for $items, as an array of child elements keyed by
        *   consecutive numeric indexes starting from 0.
        */
    -  public function viewElements(FieldItemListInterface $items);
    +  public function viewElements(FieldItemListInterface $items, $langcode);
     
    

    This then needs to change accordingly, it's optional on view() but not on viewElements() so there won't be a default there.

Berdir’s picture

Did that. Removed the helper method, fixed docs, remove use statements that aren't needed.

This is as minimal as possible now I think.

yched’s picture

Status: Needs review » Reviewed & tested by the community

Yay. RTBC if green

Berdir’s picture

Issue summary: View changes
Berdir’s picture

CR created (https://www.drupal.org/node/2575729), issue summary and beta evaluation updated.

We should be good to go here.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Discussed with @yched and @Berdir - without this change we can;t deliver the expected functionality of entity references in Drupal 8 - this for me is very close to a critical - therefore I think implementing this with a very obvious and easy to fix API break makes sense. Committed d0c1376 and pushed to 8.0.x. Thanks!

  • alexpott committed d0c1376 on 8.0.x
    Issue #2543258 by LKS90, hchonov, tassilogroeper, sasanikolic, Berdir,...
s_leu’s picture

I had some issues with untranslatable entities and translatable entity references that is quite related to this, documented this here #2584745: Entity references should be displayed translated on non translatable entity types.

Status: Fixed » Closed (fixed)

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