Problem/Motivation
Views has a system in order to render fields in different languages. Either it's determined from a langcode which is part of the result, or it's taken from the current content / interface language, or simply a hardcoded langcode.
While the basic case works fine, the rendering by langcode of the current row [which is the default in Views] does not support using a relationship.
Example:
- Add a text field to the User entity, and edit a user account to populate it.
- Create a node from this author.
- Create a node view, displaying fields (not teasers)
- Add the author as relationship
- Add the text field from User to the display
- Select the translation to be based upon the row language [default setting]
You'll end up in an exception similar to this [shown exception is from a Taxonomy relationship, but User exception is similar]:
Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'langcode' in field list is ambiguous: SELECT node_field_data.title AS node_field_data_title, node_field_data.nid AS nid, node_field_data.langcode AS node_field_data_langcode, taxonomy_term_field_data_node_field_data.name AS taxonomy_term_field_data_node_field_data_name, taxonomy_term_field_data_node_field_data.vid AS taxonomy_term_field_data_node_field_data_vid, taxonomy_term_field_data_node_field_data.tid AS taxonomy_term_field_data_node_field_data_tid, users_field_data_node_field_revision__users.uuid AS users_field_data_node_field_revision__users_uuid, taxonomy_term_field_data_node_field_data__taxonomy_term_data.uuid AS taxonomy_term_field_data_node_field_data__taxonomy_term_data, node_field_data.created AS node_field_data_created, users_field_data_node_field_revision.uid AS users_field_data_node_field_revision_uid, langcode AS langcode FROM {node_field_data} node_field_data LEFT JOIN (SELECT td.*, tn.nid AS nid FROM {taxonomy_term_field_data} td LEFT JOIN {taxonomy_index} tn ON tn.tid = td.tid WHERE (td.vid IN (:db_condition_placeholder_1)) ) taxonomy_term_field_data_node_field_data ON node_field_data.nid = taxonomy_term_field_data_node_field_data.nid INNER JOIN {node_field_revision} node_field_revision ON node_field_data.vid = node_field_revision.vid LEFT JOIN {users_field_data} users_field_data_node_field_revision ON node_field_revision.uid = users_field_data_node_field_revision.uid INNER JOIN {users} users_field_data_node_field_revision__users ON users_field_data_node_field_revision.uid = users_field_data_node_field_revision__users.uid INNER JOIN {taxonomy_term_data} taxonomy_term_field_data_node_field_data__taxonomy_term_data ON taxonomy_term_field_data_node_field_data.tid = taxonomy_term_field_data_node_field_data__taxonomy_term_data.tid WHERE (( (node_field_data.status = :db_condition_placeholder_0) )) ORDER BY node_field_data_created DESC LIMIT 10 OFFSET 0; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => tags ) in Drupal\Core\Database\Connection->query() (line 581 of core/lib/Drupal/Core/Database/Connection.php).
Proposed resolution
The problem is in the following piece of code:
$langcode_key = $this->entityType->getKey('langcode');
foreach (array('data_table', 'revision_table', 'base_table') as $key) {
if ($table = $this->entityType->get($key)) {
$table_alias = $query->ensureTable($table); // HERE
$this->langcodeAlias = $query->addField($table_alias, $langcode_key);
break;
}
}
This code is trying to add the right 'langcode' field to an entity table in the view, basically by guessing what table it needs to add it to. So, $table is found, in this example, to be {users}
. By default views doesn't know how to join from {node}
to {users}
.
In order to be able to do that, it has to take into account the relationship. Also, the langcode field that is added may need to have a better alias.
The solution is to pass along the relationship and leverage it.
Remaining tasks
Steps to reproduce on a supported version of Drupal
Postponed on #2457999: Cannot use relationship for rendered entity on Views, which may fix this.
User interface changes
API changes
Comment | File | Size | Author |
---|---|---|---|
#36 | 2446681-36.patch | 910 bytes | kostik1337 |
#26 | 2446681-26.test.patch | 4.97 KB | plach |
#22 | 2446681-22.patch | 10.28 KB | Sam152 |
#22 | interdiff.txt | 763 bytes | Sam152 |
#20 | 2446681-20.patch | 9.53 KB | Sam152 |
Comments
Comment #1
dawehnerSo far this is a version which works, but it seems to be that the test changes I made kinda broke stuff.
Comment #2
jhodgdonA few notes:
a) I originally came across this issue when doing manual testing in #2429447-50: Use data table as views base table, if available.
b) The exception in the issue summary is from joining on a taxonomy reference field. Any Relationship will do, but just noting that the specific query exception posted there is from a taxonomy join.
c) Currently to trigger this problem you need to add a field to the entity you are joining, such as adding a text field to Users, and joining on User, and then display that custom field you created.
d) And it is only a problem for field-based views.
I'll update the issue summary.
Comment #4
penyaskitoI couldn't reproduce:
1. Installed language, content_translation.
2. Enabled content translation for nodes and users.
3. Followed the issue summary "Example" instructions.
4. Created a user, filled the field.
5. Translated the user and the field.
6. Created a node with that user as author.
7. Translated it.
8. The view shows 4 rows (no exception): node en-user en, node es-user en, node en-user es, node es-user es.
Comment #5
jhodgdonThat is very interesting. I wonder how/when it would have been fixed? I was not able to make the problem unless I made a view with a relationship between Node and another entity, and there was a plain Text field on the second entity that was translated, and the view was field-based and displaying that field on the second entity. Can you verify that is what you tested?
Comment #6
plachI think this might have been fixed by #2454145: Replace user_name handler with Field API formatter, which added a relationship parameter to the renderer
::query
method:http://cgit.drupalcode.org/drupal/tree/core/modules/views/src/Entity/Ren...
Comment #7
jhodgdonOK then, let's close this then. Thanks!
Comment #8
Gábor HojtsyYay!
Comment #9
agentrickardI just saw this pop-up again in #2726507: Integrity Constraint Violation Exception when adding another language. Could be an issue with revisions? Not sure.
Comment #10
agentrickardIn this case it appears as if the langcode field is added twice, once (correctly) by {node_revision} and once incorrectly by {users}.
Comment #11
agentrickardThe issue may be that there are _two_ relationships in this view. Note the broken fieldAliases.
Comment #12
agentrickardInteresting. In my case, the View has 5 fields. 4 use a relationship. One does not. It's that last one that causes the error.
I can fix that in the View by adding the missing relationship, but this still seems like a bug.
Comment #13
agentrickardThis never got bumped when versions changed.
Comment #15
cilefen CreditAttribution: cilefen commented@timplunkett, @alexpott, @dawehner, @xjm and I discussed this in a triage session at DrupalCon Dublin. This is an exception thrown in the UI with a few simple steps. We agree this is major priority.
Comment #17
LendudeThe fix for #2457999: Cannot use relationship for rendered entity on Views adds relationship handling to TranslationLanguageRenderer so would expect this to be fixed when that lands.
Comment #18
johnvThe same error occurs in a far easier example then in OP, using a View of Content Revisions
- Add a Field of type "List (integer) (module: options)" to a content type. Add a few keys. You may or may not use 'key-value combinations.
- create some content
- Create a view of Content Revisions, add the new field, add an exposed filter for the field to the view.
- Show the view: all is fine:
- Apply the filter: the following error appears:
The patch in #1 (which is outdated, so applied by hand manually) does not solve the problem.
Comment #19
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI tested the patch mentioned in #17 and that didn't fix this issue for me when it was manifested in #2862041: Provide useful Views filters for Content Moderation State fields.
Comment #20
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedHere is a test that proves this still exists, inspired by #1 but using a new view with a revision base table.
Hopefully helps work towards a solution for this.
Comment #22
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedHere is a solution which fixes the problem for me, but it doesn't feel right for some reason.
Comment #23
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedComment #24
plachWorking a bit on this
Comment #25
plachThis is blocking #2862041: Provide useful Views filters for Content Moderation State fields, so it's a blocker as well.
Comment #26
plachAfter digging into this, I think the issue we are having in #2862041: Provide useful Views filters for Content Moderation State fields is not actually related to this one, it's more about properly supporting entity revision views at entity row renderer level. Therefore I'm going to reopen the issue that was marked as duplicate and move there.
Attaching an updated version of the tests in #1 (plus some clean-up) applying to 8.4.x. I suspect this may be actually fixed by #2457999: Cannot use relationship for rendered entity on Views or should at least be postponed to that.
Comment #27
plachComment #29
samuel.mortensonJust tried the most recent patch in #2457999: Cannot use relationship for rendered entity on Views and it doesn't resolve this problem. #22 is a quick fix for now, for anyone landing on this issue.
Comment #30
tacituseu CreditAttribution: tacituseu commented@samuel.mortenson: Try #2896381: "TranslationLanguageRenderer" uses the wrong table for the "langcode" column with entity revision views if you're on 8.3.
Comment #36
kostik1337 CreditAttribution: kostik1337 commentedRerolled patch for 8.9.0 without tests, because I had errors
Comment #41
quietone CreditAttribution: quietone at PreviousNext commentedAdding what this is postponed on to the issue summary.
Comment #42
danflanagan8The blocking issue has been fixed. I'll un-postpone this.
There is some suspicion that the blocking issue will have fixed this as well, so I'm going to tag for manual testing. Can this still be reproduced?
Comment #43
danflanagan8Updating target branch to reflect where the former blocker was committed. That's where manual testing should happen to see if we can still reproduce this bug.
Comment #44
quietone CreditAttribution: quietone at PreviousNext commentedI tested this on Drupal 10.1.x, standard install. I tried to follow the steps the Issue Summary but I don't know what the last step means, "Select the translation to be based upon the row language [default setting]". I tried the steps in #4 and #18 and was not able to produce an error.
In order to continue here we need complete steps to reproduce the issue (starting from "Install Drupal core"). I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #46
quietone CreditAttribution: quietone at PreviousNext commentedSince we need more information to move forward with this issue,and it has not been supplied in the past two years, I am closing this issue.
If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").
Thanks!
Comment #47
damondt CreditAttribution: damondt commentedNot reopening without being sure of all steps to reproduce, but I ran into this on Drupal 10 with a paragraph relationship and a rendering language of "Content Language of View Row".
Possibly helpful context:
View is output in a block.
Avoiding this error by switching to a rendering language of entity default or applying a patch from here leads to warnings about fields on the referenced entity not existing.