You are here

function entity_translation_field_attach_view_alter in Entity Translation 7

Implements hook_field_attach_view_alter().

Hide the entity if no translation is available for the current language and language fallback is disabled.

File

./entity_translation.module, line 961

Code

function entity_translation_field_attach_view_alter(&$output, $context) {
  if (!variable_get('locale_field_language_fallback', TRUE) && entity_translation_enabled($context['entity_type'])) {
    $handler = entity_translation_get_handler($context['entity_type'], $context['entity']);
    $translations = $handler
      ->getTranslations();
    $langcode = !empty($context['language']) ? $context['language'] : $GLOBALS['language_content']->language;

    // If fallback is disabled we need to notify the user that the translation
    // is unavailable (missing or unpublished).
    if (!empty($translations->data) && (!isset($translations->data[$langcode]) && !isset($translations->data[LANGUAGE_NONE]) || isset($translations->data[$langcode]) && !entity_translation_access($context['entity_type'], $translations->data[$langcode]))) {

      // Provide context for rendering.
      $output['#entity'] = $context['entity'];
      $output['#entity_type'] = $context['entity_type'];
      $output['#view_mode'] = $context['view_mode'];

      // We perform theming here because the theming function might need to set
      // system messages. It would be too late in the #post_render callback.
      $output['#entity_translation_unavailable'] = theme('entity_translation_unavailable', array(
        'element' => $output,
      ));

      // As we used a string key, other modules implementing
      // hook_field_attach_view_alter() may unset/override this.
      $output['#post_render']['entity_translation'] = 'entity_translation_unavailable';
    }
  }
}