You are here

function entity_translation_overview in Entity Translation 7

Translations overview page callback.

3 string references to 'entity_translation_overview'
entity_translation_menu_alter in ./entity_translation.module
Implements hook_menu_alter().
entity_translation_node_menu_alter in ./entity_translation.node.inc
Node-specific menu alterations.
entity_translation_taxonomy_term_menu_alter in ./entity_translation.taxonomy.inc
Taxonomy-term-specific menu alterations.

File

./entity_translation.admin.inc, line 259
The entity translation user interface.

Code

function entity_translation_overview($entity_type, $entity, $callback = NULL) {
  $args = func_get_args();

  // Entity translation and node translation share the same system path.
  if ($callback && !entity_translation_enabled($entity_type, $entity)) {
    $callback_args = array_slice($args, 3);
    return entity_translation_overview_callback($callback, $callback_args);
  }
  $handler = entity_translation_get_handler($entity_type, $entity);

  // Ensure $entity holds an entity object and not an id.
  $entity = $handler
    ->getEntity();
  $handler
    ->initPathScheme();

  // Initialize translations if they are empty.
  $translations = $handler
    ->getTranslations();
  if (empty($translations->original)) {
    $handler
      ->initTranslations();
    $handler
      ->saveTranslations();
  }

  // Ensure that we have a coherent status between entity language and field
  // languages.
  if ($handler
    ->initOriginalTranslation()) {

    // FIXME!
    entity_translation_entity_save($entity_type, $entity);
  }
  $header = array(
    t('Language'),
    t('Source language'),
    t('Translation'),
    t('Status'),
    t('Operations'),
  );
  $languages = entity_translation_languages();
  $source = $translations->original;
  $path = $handler
    ->getViewPath();
  $rows = array();
  if (drupal_multilingual()) {

    // If we have a view path defined for the current entity get the switch
    // links based on it.
    if ($path) {
      $links = EntityTranslationDefaultHandler::languageSwitchLinks($path);
    }
    $show_fallback = variable_get('locale_field_language_fallback', TRUE) && variable_get('entity_translation_show_fallback_on_overview_pages', FALSE);
    foreach ($languages as $language) {
      $classes = array();
      $options = array();
      $language_name = $language->name;
      $langcode = $language->language;
      $edit_path = $handler
        ->getEditPath($langcode);
      $add_path = "{$handler->getEditPath()}/add/{$source}/{$langcode}";
      if ($edit_path) {
        $add_links = EntityTranslationDefaultHandler::languageSwitchLinks($add_path);
        $edit_links = EntityTranslationDefaultHandler::languageSwitchLinks($edit_path);
      }
      if (isset($translations->data[$langcode])) {
        list(, , $bundle) = entity_extract_ids($entity_type, $entity);

        // Existing translation in the translation set: display status.
        $is_original = $langcode == $translations->original;
        $translation = $translations->data[$langcode];
        $label = _entity_translation_label($entity_type, $entity, $langcode);
        $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array(
          'href' => $path,
          'language' => $language,
        );
        $row_title = l($label, $link['href'], $link);
        if (empty($link['href'])) {
          $row_title = $is_original ? $label : t('n/a');
        }
        if ($edit_path && $handler
          ->getAccess('update') && $handler
          ->getTranslationAccess($langcode)) {
          $link = isset($edit_links->links[$langcode]['href']) ? $edit_links->links[$langcode] : array(
            'href' => $edit_path,
            'language' => $language,
          );
          $link['query'] = isset($_GET['destination']) ? drupal_get_destination() : FALSE;
          $options[] = l(t('edit'), $link['href'], $link);
        }
        $status = $translation['status'] ? t('Published') : t('Not published');
        $classes[] = $translation['status'] ? 'published' : 'not-published';
        $status .= isset($translation['translate']) && $translation['translate'] ? theme('entity_translation_overview_outdated', array(
          'message' => t('outdated'),
        )) : '';
        $classes[] = isset($translation['translate']) && $translation['translate'] ? 'outdated' : '';
        if ($is_original) {
          $language_name = t('<strong>@language_name</strong>', array(
            '@language_name' => $language_name,
          ));
          $source_name = t('(original content)');
        }
        else {
          $source_name = $languages[$translation['source']]->name;
        }
      }
      else {

        // No such translation in the set yet: help user to create it.
        $row_title = $source_name = t('n/a');
        if ($source != $langcode && $handler
          ->getAccess('update') && $handler
          ->getTranslationAccess($langcode)) {
          list(, , $bundle) = entity_extract_ids($entity_type, $entity);
          $translatable = FALSE;
          foreach (field_info_instances($entity_type, $bundle) as $instance) {
            $field_name = $instance['field_name'];
            $field = field_info_field($field_name);
            if ($field['translatable']) {
              $translatable = TRUE;
              break;
            }
          }
          $link = isset($add_links->links[$langcode]['href']) ? $add_links->links[$langcode] : array(
            'href' => $add_path,
            'language' => $language,
          );
          $link['query'] = isset($_GET['destination']) ? drupal_get_destination() : FALSE;
          $options[] = $translatable ? l(t('add'), $link['href'], $link) : t('No translatable fields');
          $classes[] = $translatable ? '' : 'non-translatable';
        }
        $status = t('Not translated');

        // Show fallback information if required.
        if ($show_fallback) {
          $language_fallback_candidates = _entity_translation_language_fallback_get_candidates($language);
          $fallback_candidates = array_intersect_key(drupal_map_assoc($language_fallback_candidates), $translations->data);
          $fallback_langcode = reset($fallback_candidates);
          if ($fallback_langcode !== FALSE) {
            $status = t('Fallback from @language', array(
              '@language' => $languages[$fallback_langcode]->name,
            ));
            $label = _entity_translation_label($entity_type, $entity, $fallback_langcode);
            $link = isset($links->links[$langcode]['href']) ? $links->links[$langcode] : array(
              'href' => $path,
              'language' => $language,
            );
            $row_title = l($label, $link['href'], $link);
          }
        }
      }
      $rows[] = array(
        'data' => array(
          $language_name,
          $source_name,
          $row_title,
          $status,
          implode(" | ", $options),
        ),
        'class' => $classes,
      );
    }
  }
  drupal_set_title(t('Translations of %label', array(
    '%label' => $handler
      ->getLabel(),
  )), PASS_THROUGH);

  // Add metadata to the build render allow to let other modules know about
  // which entity this is.
  $build['#entity_type'] = $entity_type;
  $build['#entity'] = $entity;
  $build['entity_translation_overview'] = array(
    '#theme' => 'entity_translation_overview',
    '#header' => $header,
    '#rows' => $rows,
  );
  return $build;
}