function content_translation_form_alter

content_translation_form_alter(array &$form, FormStateInterface $form_state)

Implements hook_form_alter().

File

core/modules/content_translation/content_translation.module, line 291
Allows entities to be translated into different languages.

Code

function content_translation_form_alter(array &$form, FormStateInterface $form_state) {
  $form_object = $form_state->getFormObject();
  if (!($form_object instanceof ContentEntityFormInterface)) {
    return;
  }
  $entity = $form_object->getEntity();
  $op = $form_object->getOperation();

  // Let the content translation handler alter the content entity edit form.
  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1 && ($op == 'edit' || $op == 'default')) {
    $controller = \Drupal::entityManager()->getHandler($entity->getEntityTypeId(), 'translation');
    $controller->entityFormAlter($form, $form_state, $entity);

    // @todo Move the following lines to the code generating the property form
    //   elements once we have an official #multilingual FAPI key.
    $translations = $entity->getTranslationLanguages();
    $form_langcode = $form_object->getFormLangcode($form_state);

    // Handle fields shared between translations when there is at least one
    // translation available or a new one is being created.
    if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
      $langcode_key = $entity->getEntityType()->getKey('langcode');
      foreach ($entity->getFieldDefinitions() as $field_name => $definition) {
        if (isset($form[$field_name]) && $field_name != $langcode_key) {
          $form[$field_name]['#multilingual'] = $definition->isTranslatable();
        }
      }
    }

  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!content_translation!content_translation.module/function/content_translation_form_alter/8.1.x