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
User interface changes
API changes