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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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();
        }
      }
    }
 
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.