You are here

public function EntityTranslationDefaultHandler::entityForm in Entity Translation 7

Overrides EntityTranslationHandlerInterface::entityForm

See also

EntityTranslationHandlerInterface::entityForm()

4 calls to EntityTranslationDefaultHandler::entityForm()
EntityTranslationCommentHandler::entityForm in includes/translation.handler.comment.inc
EntityTranslationNodeHandler::entityForm in includes/translation.handler.node.inc
Convert the translation update status fieldset into a vartical tab.
EntityTranslationTaxonomyTermHandler::entityForm in includes/translation.handler.taxonomy_term.inc
EntityTranslationUserHandler::entityForm in includes/translation.handler.user.inc
4 methods override EntityTranslationDefaultHandler::entityForm()
EntityTranslationCommentHandler::entityForm in includes/translation.handler.comment.inc
EntityTranslationNodeHandler::entityForm in includes/translation.handler.node.inc
Convert the translation update status fieldset into a vartical tab.
EntityTranslationTaxonomyTermHandler::entityForm in includes/translation.handler.taxonomy_term.inc
EntityTranslationUserHandler::entityForm in includes/translation.handler.user.inc

File

includes/translation.handler.inc, line 1240
Default translation handler for the translation module.

Class

EntityTranslationDefaultHandler
Class implementing the default entity translation behaviours.

Code

public function entityForm(&$form, &$form_state) {
  $this->entityForm = TRUE;
  $translations = $this
    ->getTranslations();
  $form_langcode = $this
    ->getActiveLanguage();
  $langcode = $this
    ->getLanguage();
  $is_translation = $this
    ->isTranslationForm();
  $new_translation = !isset($translations->data[$form_langcode]);
  $no_translations = count($translations->data) < 2;
  $languages = language_list();
  $access = user_access('translate any entity') || user_access("translate {$this->entityType} entities");

  // Store contextual information in the form state.
  $form_state['entity_translation']['form_langcode'] = $form_langcode;
  $form_state['entity_translation']['source_langcode'] = $this
    ->getSourceLanguage();

  // The only way to determine whether we are editing the original values is
  // comparing form language and entity language. Since a language change
  // might render impossible to make this check after form submission, we
  // store the related information here.
  $form_state['entity_translation']['is_translation'] = $is_translation;

  // Adjust page title to specify the current language being edited, if we
  // have at least one translation.
  if ($form_langcode != LANGUAGE_NONE && (!$no_translations || $new_translation)) {
    drupal_set_title($this
      ->entityFormTitle() . ' [' . t($languages[$form_langcode]->name) . ']', PASS_THROUGH);
  }

  // Display source language selector only if we are creating a new
  // translation and there are at least two translations available.
  if (!$no_translations && $new_translation) {
    $form['source_language'] = array(
      '#type' => 'fieldset',
      '#title' => t('Source language'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => -100,
      '#access' => $access,
      '#multilingual' => TRUE,
      'language' => array(
        '#type' => 'select',
        '#default_value' => $this
          ->getSourceLanguage(),
        '#options' => array(),
      ),
      'submit' => array(
        '#type' => 'submit',
        '#value' => t('Change'),
        '#submit' => array(
          'entity_translation_entity_form_source_language_submit',
        ),
      ),
    );
    foreach (language_list() as $language) {
      if (isset($translations->data[$language->language])) {
        $form['source_language']['language']['#options'][$language->language] = t($language->name);
      }
    }
  }

  // Add the entity language switcher.
  $this
    ->entityFormLanguageWidget($form, $form_state);
  if ($is_translation && isset($form['actions']['delete'])) {

    // Replace the delete button with the delete translation one.
    if (!$new_translation) {
      $weight = 100;
      foreach (array(
        'delete',
        'submit',
      ) as $key) {
        if (isset($form['actions'][$key]['weight'])) {
          $weight = $form['actions'][$key]['weight'];
          break;
        }
      }
      $form['actions']['delete_translation'] = array(
        '#type' => 'submit',
        '#value' => t('Delete translation'),
        '#weight' => $weight,
        '#submit' => array(
          'entity_translation_entity_form_delete_translation_submit',
        ),
      );
    }

    // Always remove the delete button on translation forms.
    unset($form['actions']['delete']);
  }

  // We need to display the translation tab only when there is at least one
  // translation available or a new one is about to be created.
  if ($new_translation || count($translations->data) > 1) {
    $form['translation'] = array(
      '#type' => 'fieldset',
      '#title' => t('Translation'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => 10,
      '#access' => $access,
      '#multilingual' => TRUE,
    );

    // A new translation is enabled by default.
    $status = $new_translation || $translations->data[$form_langcode]['status'];

    // If there is only one published translation we cannot unpublish it,
    // since there would be no content left to display. The whole entity
    // should be unpublished instead, where possible.
    $enabled = !$status;
    if (!empty($status)) {

      // A new translation is not available in the translation data hence it
      // should count as one more.
      $published = $new_translation;
      foreach ($translations->data as $langcode => $translation) {
        $published += $translation['status'];
      }
      $enabled = $published > 1;
    }
    $description = $enabled ? t('An unpublished translation will not be visible for non-administrators.') : t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
    $form['translation']['status'] = array(
      '#type' => 'checkbox',
      '#title' => t('This translation is published'),
      '#default_value' => $status,
      '#description' => $description,
      '#disabled' => !$enabled,
    );
    $translate = !$new_translation && !empty($translations->data[$form_langcode]['translate']);
    if (!$translate) {
      $form['translation']['retranslate'] = array(
        '#type' => 'checkbox',
        '#title' => t('Flag translations as outdated'),
        '#default_value' => 0,
        '#description' => t('If you made a significant change, which means translations should be updated, you can flag all translations of this post as outdated. This will not change any other property of those posts, like whether they are published or not.'),
      );
    }
    else {
      $form['translation']['translate'] = array(
        '#type' => 'checkbox',
        '#title' => t('This translation needs to be updated'),
        '#default_value' => $translate,
        '#description' => t('When this option is checked, this translation needs to be updated because the source post has changed. Uncheck when the translation is up to date again.'),
        '#disabled' => !$translate,
      );
    }
    $translation_author = $new_translation ? $GLOBALS['user'] : user_load($translations->data[$form_langcode]['uid']);
    $name = isset($translation_author->name) ? $translation_author->name : '';
    $form['translation']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Authored by'),
      '#maxlength' => 60,
      '#autocomplete_path' => 'user/autocomplete',
      '#default_value' => $name,
      '#description' => t('Leave blank for %anonymous.', array(
        '%anonymous' => variable_get('anonymous', t('Anonymous')),
      )),
    );
    $date = $new_translation ? REQUEST_TIME : $translations->data[$form_langcode]['created'];
    $form['translation']['created'] = array(
      '#type' => 'textfield',
      '#title' => t('Authored on'),
      '#maxlength' => 25,
      '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
        '%time' => format_date($date, 'custom', 'Y-m-d H:i:s O'),
        '%timezone' => format_date($date, 'custom', 'O'),
      )),
      '#default_value' => $new_translation ? '' : format_date($date, 'custom', 'Y-m-d H:i:s O'),
    );
  }

  // If Menu translation is available translate the menu strings.
  if (module_exists('entity_translation_i18n_menu')) {
    $this
      ->menuForm($form, $form_state);
  }

  // Process entity form submission.
  $form['#submit'][] = 'entity_translation_entity_form_submit';

  // This allows to intercept deletions. The check is needed because
  // action-specific submit handlers prevent global ones from being called.
  if (!empty($form['actions']['delete']['#submit'])) {
    $form['actions']['delete']['#submit'][] = 'entity_translation_entity_form_submit';
  }
}