function ContentTranslationFormLanguageHooks::formLanguageContentSettingsFormAlter

Implements hook_form_FORM_ID_alter() for language_content_settings_form().

Attributes

#[Hook('form_language_content_settings_form_alter')]

File

core/modules/content_translation/src/Hook/ContentTranslationFormLanguageHooks.php, line 45

Class

ContentTranslationFormLanguageHooks
Form hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public function formLanguageContentSettingsFormAlter(array &$form, FormStateInterface $form_state) : void {
  // Inject into the content language settings the translation settings if the
  // user has the required permission.
  if (!$this->currentUser
    ->hasPermission('administer content translation')) {
    return;
  }
  $default = $form['entity_types']['#default_value'];
  foreach ($default as $entity_type_id => $enabled) {
    $default[$entity_type_id] = $enabled || $this->contentTranslationManager
      ->isEnabled($entity_type_id) ? $entity_type_id : FALSE;
  }
  $form['entity_types']['#default_value'] = $default;
  $form['#attached']['library'][] = 'content_translation/drupal.content_translation.admin';
  foreach ($form['#labels'] as $entity_type_id => $label) {
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $storage_definitions = $entity_type instanceof ContentEntityTypeInterface ? $this->entityFieldManager
      ->getFieldStorageDefinitions($entity_type_id) : [];
    $entity_type_translatable = $this->contentTranslationManager
      ->isSupported($entity_type_id);
    foreach ($this->entityTypeBundleInfo
      ->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
      // Here we do not want the widget to be altered and hold also the
      // "Enable translation" checkbox, which would be redundant. Hence we
      // add this key to be able to skip alterations. Alter the title and
      // display the message about UI integration.
      $form['settings'][$entity_type_id][$bundle]['settings']['language']['#content_translation_skip_alter'] = TRUE;
      if (!$entity_type_translatable) {
        $form['settings'][$entity_type_id]['#title'] = $this->t('@label (Translation is not supported).', [
          '@label' => $entity_type->getLabel(),
        ]);
        continue;
      }
      // Displayed the "shared fields widgets" toggle.
      if ($this->contentTranslationManager instanceof BundleTranslationSettingsInterface) {
        $settings = $this->contentTranslationManager
          ->getBundleTranslationSettings($entity_type_id, $bundle);
        $force_hidden = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $bundle);
        $form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['untranslatable_fields_hide'] = [
          '#type' => 'checkbox',
          '#title' => $this->t('Hide non translatable fields on translation forms'),
          '#default_value' => $force_hidden || !empty($settings['untranslatable_fields_hide']),
          '#disabled' => $force_hidden,
          '#description' => $force_hidden ? $this->t('Moderated content requires non-translatable fields to be edited in the original language form.') : '',
          '#states' => [
            'visible' => [
              ':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ];
      }
      $fields = $this->entityFieldManager
        ->getFieldDefinitions($entity_type_id, $bundle);
      if ($fields) {
        foreach ($fields as $field_name => $definition) {
          if ($definition->isComputed() || !empty($storage_definitions[$field_name]) && $this->isFieldTranslatabilityConfigurable($entity_type, $storage_definitions[$field_name])) {
            $form['settings'][$entity_type_id][$bundle]['fields'][$field_name] = [
              '#label' => $definition->getLabel(),
              '#type' => 'checkbox',
              '#default_value' => $definition->isTranslatable(),
            ];
            // Display the column translatability configuration widget.
            $column_element = $this->fieldSyncWidget
              ->widget($definition, "settings[{$entity_type_id}][{$bundle}][columns][{$field_name}]");
            if ($column_element) {
              $form['settings'][$entity_type_id][$bundle]['columns'][$field_name] = $column_element;
            }
          }
        }
        if (!empty($form['settings'][$entity_type_id][$bundle]['fields'])) {
          // Only show the checkbox to enable translation if the bundles in
          // the entity might have fields and if there are fields to
          // translate.
          $form['settings'][$entity_type_id][$bundle]['translatable'] = [
            '#type' => 'checkbox',
            '#default_value' => $this->contentTranslationManager
              ->isEnabled($entity_type_id, $bundle),
          ];
        }
      }
    }
  }
  $form['#validate'][] = [
    static::class,
    'languageContentSettingsValidate',
  ];
  $form['#submit'][] = [
    static::class,
    'languageContentSettingsSubmit',
  ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.