diff --git a/entity_translation.module b/entity_translation.module index 7640720..1b7217e 100644 --- a/entity_translation.module +++ b/entity_translation.module @@ -645,6 +645,45 @@ function entity_translation_field_language_alter(&$display_language, $context) { } /** + * Implements hook_query_node_access_alter(). + * + * Rewrite node queries so if language fallback is disabled, + * untranslated nodes are not listed. + */ +function entity_translation_query_node_access_alter($query) { + if (!variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled('node')) { + // Get the node table alias if this request is on a node table. + foreach ($query->getTables() as $table) { + if ($table['join type'] == NULL && in_array($table['table'], array('node', 'taxonomy_index'))) { + $node_alias = empty($table['alias']) ? $table['table'] : $table['alias']; + } + } + + // Only alter queries on nodes (not comments for example). + if (isset($node_alias)) { + // @TODO: hack. See http://drupal.org/node/1452642. + // There's a status field in entity_translation table, + // similar to the node table, and conditions are not prefixed by + // the node alias in node.module. + // So we have to add the node alias to avoid errors. + $conditions = &$query->conditions(); + foreach ($conditions as $key => $condition) { + $have_field = is_array($condition) && isset($condition['field']) && is_string($condition['field']); + $field_aliased = is_string($condition['field']) && strpos($condition['field'], '.') !== FALSE; + if ($have_field && !$field_aliased) { + $conditions[$key]['field'] = $node_alias . '.' . $condition['field']; + } + } + + $query->join('entity_translation', 'et', "et.entity_id = $node_alias.nid AND entity_type = :node", array(':node' => 'node')); + $query->condition('et.language', array($GLOBALS['language_content']->language, LANGUAGE_NONE)); + $query->condition('et.status', 1, '='); + $query->addTag('entity_translation'); + } + } +} + +/** * Implements hook_field_attach_view_alter(). * * Hide the entity if no translation is available for the current language and