diff --git a/modules/node/node.module b/modules/node/node.module index d86c74d..2b28d80 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -3220,6 +3220,8 @@ function _node_query_node_access_alter($query, $type) { $tables = $query->getTables(); $base_table = $query->getMetaData('base_table'); + //Flag outer join to conditionnaly don't execute exists clause with subquery + $is_null = FALSE; // If no base table is specified explicitly, search for one. if (!$base_table) { $fallback = ''; @@ -3229,6 +3231,11 @@ function _node_query_node_access_alter($query, $type) { // If the node table is in the query, it wins immediately. if ($table == 'node') { $base_table = $table; + // Check if join type is an outer join + // Depending on query constructor, we need to exclude "OUTER" string + if (in_array(trim(str_replace('OUTER', '', $table_info['join type'])), array('LEFT', 'RIGHT', 'FULL'), TRUE)) { + $is_null = TRUE; + } break; } // Check whether the table has a foreign key to node.nid. If it does, @@ -3242,6 +3249,10 @@ function _node_query_node_access_alter($query, $type) { foreach ($schema['foreign keys'] as $relation) { if ($relation['table'] === 'node' && $relation['columns'] === array('nid' => 'nid')) { $base_table = $table; + // Check if join type is an outer join + if (in_array(trim(str_replace('OUTER', '', $table_info['join type'])), array('LEFT', 'RIGHT', 'FULL'), TRUE)) { + $is_null = TRUE; + } } } } @@ -3325,13 +3336,24 @@ function _node_query_node_access_alter($query, $type) { } $subquery->where("$nalias.$field = na.nid"); - // For an entity query, attach the subquery to entity conditions. + //Set default condition with subquery exists + $clause = 'exists'; + $sub_conditions = $subquery; + //Set null condition on nid for outer join node table + if ($is_null) { + $clause = 'condition'; + $sub_conditions = db_or() + ->isNull("$nalias.$field") + ->exists($subquery); + } + + // For an entity query, attach it to entity conditions. if ($type == 'entity') { - $node_conditions->exists($subquery); + $node_conditions->{$clause}($sub_conditions); } // Otherwise attach it to the node query itself. else { - $query->exists($subquery); + $query->{$clause}($sub_conditions); } } }