diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 56f6dcf..1daa05d 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1047,22 +1047,38 @@ function node_query_node_access_alter(AlterableInterface $query) { $tables = $query->getTables(); $base_table = $query->getMetaData('base_table'); - // If the base table is not given, default to node if present. + // If the base table is not given, default to one of the node base tables. if (!$base_table) { /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */ $table_mapping = \Drupal::entityTypeManager()->getStorage('node')->getTableMapping(); $node_base_tables = $table_mapping->getTableNames(); + $potential_base_tables = []; foreach ($tables as $table_info) { if (!($table_info instanceof SelectInterface)) { $table = $table_info['table']; - // If one of the node base tables are in the query, it wins immediately. + // 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; - break; + $potential_base_tables[] = $table; } } } + + // Ensure that 'node' and 'node_field_data' are always preferred over + // 'node_revision' and 'node_field_revision'. + if (!empty($potential_base_tables)) { + if (in_array('node', $potential_base_tables)) { + $base_table = 'node'; + } + elseif (in_array('node_field_data', $potential_base_tables)) { + $base_table = 'node_field_data'; + } + else { + $base_table = reset($potential_base_tables); + } + } + // Bail out if the base table is missing. if (!$base_table) { throw new Exception(t('Query tagged for node access but there is no node table, specify the base_table using meta data.'));