Problem/Motivation
(Why the issue was filed, steps to reproduce the problem, etc.)
Steps to reproduce
1) Create new node type (for example: referenced_page)
2) Navigate to admin/config/regional/content-language and set that node type to translatable
3) Create a piece of content of that node type (for example: This is the default language version of the referenced page)
4) Create another node type (for example: referencing_page)
5) Add an entity reference field to that node type that allows it to point to the referenced_page node type
6) Do NOT set that node type to be translatable (It doesn't actually matter if you do. You'll still run into the same problem. I'm just specifically addressing the point about having a non-translatable entity referencing a translatable entity. The heart of the issue is that the referenced entity is translatable).
7) Create a piece of content of the referencing page (for example: This is the default language of the referencing page)
8) Reference the first node type (for example, pointing to "This is the default language version of the referenced page.")
9) Create a block that uses: a contextual filter with a default argument of Content ID from URL; a relationship of "Content using {your_entity_reference_field}" and require the relationship; Title field (use the relationship) of referencing pages pointing to this referenced page.
10) Place the block in a region
11) Visit the "This is the default language version of the referenced page" node
12) Observe that there is only one title in the block
13) Add a translation "This is another language version of the referenced page"
14) Observe that the block now contains two titles.
15) Add another language to the site
16) Add a translation "This is a third language version of the referenced page"
17) Observe that there are now three titles in the block
Note: One workaround for the duplication is to use aggregation (for example Group column Entity ID or Language), although that's just masking the symptom and may have other undesirable side effects.
Proposed resolution
The primary key of an entity's data table is id, langcode
. When joining on entity data tables as part of a Views relationship for an entity reference, Views only joins on the ID, however, not on the ID+langcode combination. This leads to duplicate results in case the respective entities are translated.
Remaining tasks
Modify the entity views data for all entity reference relationships to include an extra
entry that additionally joins on the langcode. When joining from a non-translatable entity, to a translatable one, ideally there would need to be some way to select the language. Not sure about that, though. But we can definitely fix the case of joining two translatable entities as we can just make sure the langcodes match. (I guess that makes sense, but again, not really sure.)
User interface changes
?
API changes
?
Data model changes
?
Comments
Comment #2
Gábor HojtsyI think this has high relation to #2144377: Entity reference autocomplete lists entity labels only in current content language which is the form UI part of entering the reference itself. Whether you refer to specific languages or generally to an entity may be different use cases. I think Views should support fine-tuning the reference relation honestly.
Comment #3
tstoecklerYeah, thinking about this more I also think that we should create a
EntityRelationship
plugin, which allows choosing the language.Comment #4
tstoecklerFor reference, if someone hits this locally, this is something I committed to a site as workaround:
With taxonomy terms this is additionally annoying because of the
taxonomy_term_hierarchy
table as shown in the code comment. In other cases you should be able to use something likeEdit: fixed example code
Comment #5
YesCT CreditAttribution: YesCT commentedchanging version to 8.1.x
per https://www.drupal.org/core/release-cycle-overview#minor
and because this is a bug, (non-disruptive?) per https://www.drupal.org/core/d8-allowed-changes#patch
Comment #6
YesCT CreditAttribution: YesCT commentedComment #8
tstoecklerI just hit this same issue with multiple value base field. The dedicated field table is also joined without langcode so duplicate bogus results of the field data are displayed.
Comment #9
tstoecklerQuick patch to get the ball rolling. This is (somewhat) distinct from the relationship issue, but I think we should solve this first, as it does not have to problem of configurability. Once this agreed upon, we can consider adding a proper EntityJoin plugin (or whatever) in a follow-up. At least that's my take.
Comment #10
tstoecklerNot sure how exactly, but I guess this relates to #2451657: Views should not condition joins on the langcode of fields that are not translatable
Comment #11
tstoecklerUpdated patch to make use of the langcode key and also adds a check for field translatability per the above.
Comment #14
tstoecklerFix copy-paste errora and adapt unit tests.
Comment #16
vegantriathleteI think I've stumbled across the very issue and want to include my steps to reproduce my issue, just in case I've found a separate issue that needs to be fixed as well.
1) Create new node type (for example: referenced_page)
2) Navigate to
admin/config/regional/content-language
and set that node type to translatable3) Create a piece of content of that node type (for example: This is the default language version of the referenced page)
4) Create another node type (for example: referencing_page)
5) Add an entity reference field to that node type that allows it to point to the referenced_page node type
6) Do NOT set that node type to be translatable (It doesn't actually matter if you do. You'll still run into the same problem. I'm just specifically addressing the point about having a non-translatable entity referencing a translatable entity. The heart of the issue is that the referenced entity is translatable).
7) Create a piece of content of the referencing page (for example: This is the default language of the referencing page)
8) Reference the first node type (for example, pointing to "This is the default language version of the referenced page.")
9) Create a block that uses: a contextual filter with a default argument of Content ID from URL; a relationship of "Content using {your_entity_reference_field}" and require the relationship; Title field (use the relationship) of referencing pages pointing to this referenced page.
10) Place the block in a region
11) Visit the "This is the default language version of the referenced page" node
12) Observe that there is only one title in the block
13) Add a translation "This is another language version of the referenced page"
14) Observe that the block now contains two titles.
15) Add another language to the site
16) Add a translation "This is a third language version of the referenced page"
17) Observe that there are now three titles in the block
Note: One workaround for the duplication is to use aggregation (for example Group column Entity ID or Language), although that's just masking the symptom and may have other undesirable side effects.
Comment #18
anemes CreditAttribution: anemes at PitechPlus commentedHello, thanks for your work. I was checking the patch and I was wondering if the key for left field is correct. As I was checking the core I noticed that it should be left_field.
Comment #19
rutiolmaI noticed this problem when trying to create a view as described on #16
Maybe the patch #14 fixes the problem on some use cases, but for sure it doesn't fix it when there's a situation like described on #16.9
I'm providing a patch that fixes the use case as described on #16 but without tests since it would be nice to have other inputs on this.
Comment #22
mbovan CreditAttribution: mbovan as a volunteer and at MD Systems GmbH for Unic commentedI rerolled the patch #14, combined it with #19 and addressed the feedback from #18.
Comment #23
daffie CreditAttribution: daffie commentedComment #24
vood002 CreditAttribution: vood002 commentedI experienced this issue as well with 8.7.5.
In my situation, the patch didn't work. It appears to me that the patch assures that the langcode of the field is the same as the langcode of the referenced entity (?).
The solution for me was to make sure the langcode of the field was the same as the user's currently selected language. The code that solved it for me is:
Not sure how I feel about this solution, but needed something to work.
Comment #27
nehajyoti CreditAttribution: nehajyoti as a volunteer and at QED42 commentedBelow code worked for me
Comment #28
rutiolmaUpdate to patch 22, to make it work with Drupal 9.2.x.
I also added an extra test check.
I also updated the summary with the use case described at comment 16
Comment 24 and 27, it would be nice to understand what are the differences between your use cases and the described step-by-step
Comment #30
Willempje2 CreditAttribution: Willempje2 commentedI'm facing the same issue when adding a relation to a feed view.
If statement in modules/views/views.views.inc:841 is triggered and i can confirm '$data["node__MYFIELD"]["table"]["join"]["node_field_data"]["extra"][1]["left_field"]' exists in a hook_views_data_alter().
Although when hitting \Drupal\views\Plugin\views\relationship\Standard::query $this->definition['extra'] does not exist anymore.
It is not a step-by-step description but i hope it helps.
Comment #33
Piotr PakulskiRe-roll for 9.5.0
Comment #34
Piotr PakulskiComment #35
perarg CreditAttribution: perarg commentedHi,
I tried the last patch #34 but it has no impact with tables commerce_product_field_data and commerce_product_variation_field data. The join query has only the product_id but no langcode is present.
UPDATE: I tried a simpler scenario where I have in view only the product title and brand. Brand is a taxonomy term that is translatable and it is an entity reference in Product entity. Unfortunately there is the same issue. langcode does not take place in JOIN statement.
Comment #36
perarg CreditAttribution: perarg commentedThis doesn't work, at least with commerce.
I found out that due to this issue https://www.drupal.org/project/drupal/issues/2930736 if change from the patch #34 the variable $field_definition --> $field_storage_definition, it works.
Comment #37
LendudeMoving back to Needs work for automated test coverage.
Comment #38
rcodina CreditAttribution: rcodina commentedPatch on #34 works for me on Drupal 9.5.2. Thanks @Piotr Pakulski!
Comment #39
bernardopaulino CreditAttribution: bernardopaulino commentedI could not reproduce this issue on my D10.1.1 installation.
Comment #40
bernardopaulino CreditAttribution: bernardopaulino commentedMy last comment #39 is false. I tested again and I have duplicated values. Also the patch in #34 could not be applied.
Comment #41
bernardopaulino CreditAttribution: bernardopaulino commentedRe-roll for drupal 10.1.1
Comment #42
daou CreditAttribution: daou commented#34 works perfectly, what a relief. This was causing so many issues in our project. Thanks!
Comment #43
Shiraz DindarThis is a very real issue and the patch in #41 solved my problem.