--- a/entity_translation.admin.inc 2012-07-02 10:43:05.000000000 +0200 +++ b/entity_translation.admin.inc 2012-07-09 17:25:34.137156248 +0200 @@ -18,6 +18,18 @@ '#default_value' => variable_get('locale_field_language_fallback', TRUE), ); + $form['entity_translation_display_unavailable'] = array( + '#type' => 'checkbox', + '#title' => t("Display unavailable translations"), + '#description' => t('If language fallback is disabled, replace the entity output with a themeable "unavailable translation" message.'), + '#default_value' => variable_get('entity_translation_display_unavailable', TRUE), + '#states' => array( + 'visible' => array( + ':input[name="locale_field_language_fallback"]' => array('checked' => FALSE), + ), + ), + ); + $form['entity_translation_shared_labels'] = array( '#type' => 'checkbox', '#title' => t('Display shared labels'), --- a/entity_translation.module 2012-07-02 10:43:05.000000000 +0200 +++ b/entity_translation.module 2012-07-09 17:23:47.408627010 +0200 @@ -635,6 +635,46 @@ } /** + * Implements hook_query_node_access_alter(). + * + * Rewrite node queries so if language fallback and the display of unavailable + * translations are disabled, untranslated nodes are not listed. + */ +function entity_translation_query_node_access_alter($query) { + $only_availables = ! variable_get('locale_field_language_fallback', TRUE) && ! variable_get('entity_translation_display_unavailable', TRUE); + if ($only_availables && 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 et.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