Problem/Motivation

The entity autocomplete form elemenet displays default values (entity labels) only in the default entity language.

Proposed resolution

Display them in the current content language, using \Drupal\Core\Entity\EntityRepositoryInterface::getTranslationFromContext.

Remaining tasks

None.

User interface changes

Entity labels will be displayed in the current content language.

API changes

Nope.

Data model changes

Nope.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Maouna created an issue. See original summary.

amateescu’s picture

Title: Entity reference autocomplete default entity labels only in default language » Entity autocomplete displays default value's entity labels only in the default entity language
Status: Needs review » Reviewed & tested by the community

I reviewed this patch in #2144377: Entity reference autocomplete lists entity labels only in current content language, which is actually about two different bugs.

This patch correctly solves the part where default value entity labels are not displayed in the correct language.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Nice find and the fix does look good. Just a concern about the changes to the test.

+++ b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php
@@ -140,17 +140,20 @@ protected function setUp() {
+    $this->drupalLogin($this->rootUser);
...
-    $this->drupalLogin($this->rootUser);

A couple of things here. Moving the login changes what is being tested so we're losing anonymous testing of:

     $this->referrerEntity = $this->createReferrerEntity();
     $this->assertEntityReferenceDisplay();

But also we're using $this->rootUser which means we're logged in as user 1 which sometimes can do and see things no other user can - which magnifies the danger of this change. A simple solution would be to just not do the first $this->assertEntityReferenceFields(); and not move the user login since that would make this concern out-of-scope for this issue.

Maouna’s picture

Yes, I understand the problem with using user 1 in this test. But I disagree with the proposal of removing the first check, because this means a reduced test coverage for this bug. So instead, I added another user with just the permission to open the form. Also, I made sure that user 1 is logged out after changing the entity's translatability.

Maouna’s picture

Assigned: Maouna » Unassigned
k4v’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

There should be an issue summary so that people know what is being fixed.

+++ b/core/lib/Drupal/Core/Entity/Element/EntityAutocomplete.php
@@ -320,6 +320,9 @@ protected static function matchEntityByTitle(SelectionInterface $handler, $input
+      // Set the entity in the correct language for display.
+      $entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity);

We're in a loop here we should get the service outside of the loop.

+++ b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php
@@ -174,19 +183,20 @@
   /**
-   * Assert entity reference form fields.
+   * Assert entity reference form display.
    */
-  protected function assertEntityReferenceFields() {
+  protected function assertEntityReferenceFormDisplay() {

Changing this is not really necessary for the fix - seems unnecessarily disruptive for the fix.

amateescu’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update
FileSize
4 KB
1.08 KB

#10.1: Fixed.
#10.2: I think you looked only at the interdiff, the change there is for the new assertion method added by this patch, so there is no disruption :)

Wrote an issue summary and putting back to RTBC because my change is really minor.

tstoeckler’s picture

RTBC++, thanks @alexpott for your never-failing eagle eyes and @amateescu for the quick fix! Awesome.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed daea0bf to 8.4.x and 5552eef to 8.3.x. Thanks!

I credited myself for review comments that resulted in changes to the patch.

  • alexpott committed daea0bf on 8.4.x
    Issue #2864020 by Maouna, amateescu, alexpott: Entity autocomplete...

  • alexpott committed 5552eef on 8.3.x
    Issue #2864020 by Maouna, amateescu, alexpott: Entity autocomplete...
alexpott’s picture

Also backported to 8.3.x because I cannot see any BC implications to this bugfix.

Status: Fixed » Closed (fixed)

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