Problem/Motivation
I believe there is a bug in node_query_node_access_alter()
when node entity query is used with a condition on the entity reference field when accessCheck(TRUE)
and at least one node grant exist.
Because Query::prepare() has required info , but doesn't set 'base_table' in metadata , node_query_node_access_alter()
doing it's guess work, specifically here:
foreach ($tables as $table_info) {
if (!($table_info instanceof SelectInterface)) {
$table = $table_info['table'];
// Ensure that 'node' and 'node_field_data' are always preferred over
// 'node_revision' and 'node_field_revision'.
if ($table == 'node' || $table == 'node_field_data') {
$base_table = $table;
break;
}
// If one of the node base tables are in the query, add it to the list
// of possible base tables to join against.
if (in_array($table, $node_base_tables)) {
$base_table = $table;
}
}
}
in describe scenario $tables
would have two tables:
- base_table - node_revision
- field table, like node_revision__field_reference
with both present in $node_base_tables
the last one would be used.
Steps to reproduce
$query = $entity_type_storage->getQuery();
$query->allRevisions();
$query->condition("$field_name.target_id", $entity->id());
$ids = $query->execute();
Expected
What is observed currently is :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_revision__field_reference.nid' in 'where clause': SELECT "base_table"."vid" AS "vid", "base_table"."nid" AS "nid"
FROM "node_revision" "base_table"
LEFT JOIN "node_revision__field_reference" "node_revision__field_reference" ON "node_revision__field_reference"."revision_id" = "base_table"."vid"
WHERE ("node_revision__field_reference"."field_dm_tender_results_target_id" = :db_condition_placeholder_0) OR (EXISTS (SELECT "na"."nid" AS "nid"
FROM
"node_access" "na"
WHERE ((("gid" IN (:db_condition_placeholder_1)) AND ("realm" = :db_condition_placeholder_2)) OR (("gid" IN (:db_condition_placeholder_3)) AND ("realm" = :db_condition_placeholder_4)) OR (("gid" IN (:db_condition_placeholder_5)) AND ("realm" = :db_condition_placeholder_6)) OR (("gid" IN (:db_condition_placeholder_7)) AND ("realm" = :db_condition_placeholder_8))) AND ("na"."grant_view" >= :db_condition_placeholder_9) AND (("node_revision__field_reference"."nid" = "na"."nid"))));
the issue is clearly in the last condition - "node_revision__field_reference"."nid" = "na"."nid"
, which should be "node_revision"."nid" = "na"."nid"
Proposed resolution
Add the test covering scenario from description.
Based on the testing outcome figure out what needs fixing.
Remaining tasks
Write a test - see
#2649268-24: Entity Field Query throws QueryException when querying all revisions and setting condition on entity reference field where target doesn't support revisions →
for example.
User interface changes
API changes
Data model changes
Release notes snippet