public function EntityTranslationController::entityFormAlter

Implements EntityTranslationControllerInterface::entityFormAlter().

Overrides EntityTranslationControllerInterface::entityFormAlter

3 calls to EntityTranslationController::entityFormAlter()
NodeTranslationController::entityFormAlter in drupal/core/modules/node/lib/Drupal/node/NodeTranslationController.php
Overrides EntityTranslationController::entityFormAlter().
ProfileTranslationController::entityFormAlter in drupal/core/modules/user/lib/Drupal/user/ProfileTranslationController.php
Overrides EntityTranslationController::entityFormAlter().
TermTranslationController::entityFormAlter in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/TermTranslationController.php
Overrides EntityTranslationController::entityFormAlter().
3 methods override EntityTranslationController::entityFormAlter()
NodeTranslationController::entityFormAlter in drupal/core/modules/node/lib/Drupal/node/NodeTranslationController.php
Overrides EntityTranslationController::entityFormAlter().
ProfileTranslationController::entityFormAlter in drupal/core/modules/user/lib/Drupal/user/ProfileTranslationController.php
Overrides EntityTranslationController::entityFormAlter().
TermTranslationController::entityFormAlter in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/TermTranslationController.php
Overrides EntityTranslationController::entityFormAlter().

File

drupal/core/modules/translation_entity/lib/Drupal/translation_entity/EntityTranslationController.php, line 117
Definition of Drupal\translation_entity\EntityTranslationController.

Class

EntityTranslationController
Base class for entity translation controllers.

Namespace

Drupal\translation_entity

Code

public function entityFormAlter(array &$form, array &$form_state, EntityInterface $entity) {
  $form_controller = translation_entity_form_controller($form_state);
  $form_langcode = $form_controller
    ->getFormLangcode($form_state);
  $entity_langcode = $entity
    ->language()->langcode;
  $source_langcode = $this
    ->getSourceLangcode($form_state);
  $new_translation = !empty($source_langcode);
  $translations = $entity
    ->getTranslationLanguages();
  if ($new_translation) {

    // Make sure a new translation does not appear as existing yet.
    unset($translations[$form_langcode]);
  }
  $is_translation = !$form_controller
    ->isDefaultFormLangcode($form_state);
  $has_translations = count($translations) > 1;

  // Adjust page title to specify the current language being edited, if we
  // have at least one translation.
  $languages = language_list();
  if (isset($languages[$form_langcode]) && ($has_translations || $new_translation)) {
    $title = $this
      ->entityFormTitle($entity);

    // When editing the original values display just the entity label.
    if ($form_langcode != $entity
      ->language()->langcode) {
      $t_args = array(
        '%language' => $languages[$form_langcode]->name,
        '%title' => $entity
          ->label(),
      );
      $title = empty($source_langcode) ? $title . ' [' . t('%language translation', $t_args) . ']' : t('Create %language translation of %title', $t_args);
    }
    drupal_set_title($title, PASS_THROUGH);
  }

  // Display source language selector only if we are creating a new
  // translation and there are at least two translations available.
  if ($has_translations && $new_translation) {
    $form['source_langcode'] = array(
      '#type' => 'details',
      '#title' => t('Source language: @language', array(
        '@language' => $languages[$source_langcode]->name,
      )),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => -100,
      '#multilingual' => TRUE,
      'source' => array(
        '#type' => 'select',
        '#default_value' => $source_langcode,
        '#options' => array(),
      ),
      'submit' => array(
        '#type' => 'submit',
        '#value' => t('Change'),
        '#submit' => array(
          array(
            $this,
            'entityFormSourceChange',
          ),
        ),
      ),
    );
    foreach (language_list(LANGUAGE_CONFIGURABLE) as $language) {
      if (isset($translations[$language->langcode])) {
        $form['source_langcode']['source']['#options'][$language->langcode] = $language->name;
      }
    }
  }

  // Disable languages for existing translations, so it is not possible to
  // switch this node to some language which is already in the translation
  // set.
  $language_widget = isset($form['langcode']) && $form['langcode']['#type'] == 'language_select';
  if ($language_widget && $has_translations) {
    $form['langcode']['#options'] = array();
    foreach (language_list(LANGUAGE_CONFIGURABLE) as $language) {
      if (empty($translations[$language->langcode]) || $language->langcode == $entity_langcode) {
        $form['langcode']['#options'][$language->langcode] = $language->name;
      }
    }
  }
  if ($is_translation) {
    if ($language_widget) {
      $form['langcode']['#access'] = FALSE;
    }

    // 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(
          array(
            $this,
            'entityFormDeleteTranslation',
          ),
        ),
      );
    }

    // 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 || $has_translations) {
    $form['translation'] = array(
      '#type' => 'details',
      '#title' => t('Translation'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
      '#weight' => 10,
      '#access' => $this
        ->getTranslationAccess($entity, $form_langcode),
      '#multilingual' => TRUE,
    );
    $translate = !$new_translation && $entity->retranslate[$form_langcode];
    if (!$translate) {
      $form['translation']['retranslate'] = array(
        '#type' => 'checkbox',
        '#title' => t('Flag other translations as outdated'),
        '#default_value' => FALSE,
        '#description' => t('If you made a significant change, which means the other translations should be updated, you can flag all translations of this content as outdated. This will not change any other property of them, 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. Uncheck when the translation is up to date again.'),
      );
    }
    if ($language_widget) {
      $form_langcode['#multilingual'] = TRUE;
    }
    $form['#process'][] = array(
      $this,
      'entityFormSharedElements',
    );
  }

  // Process the submitted values before they are stored.
  $form['#entity_builders'][] = array(
    $this,
    'entityFormEntityBuild',
  );

  // Handle entity deletion.
  if (isset($form['actions']['delete'])) {
    $form['actions']['delete']['#submit'][] = array(
      $this,
      'entityFormDelete',
    );
  }
}