function content_translation_form_alter in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/content_translation/content_translation.module \content_translation_form_alter()
Implements hook_form_alter().
File
- core/
modules/ content_translation/ content_translation.module, line 283 - 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();
}
}
}
}
}