function ContentTranslationFormHooks::formFieldConfigEditFormAlter

Implements hook_form_FORM_ID_alter() for 'field_config_edit_form'.

Attributes

#[Hook('form_field_config_edit_form_alter')]

File

core/modules/content_translation/src/Hook/ContentTranslationFormHooks.php, line 77

Class

ContentTranslationFormHooks
Form hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public function formFieldConfigEditFormAlter(array &$form, FormStateInterface $form_state) : void {
  $field = $form_state->getFormObject()
    ->getEntity();
  $bundle_is_translatable = $this->contentTranslationManager
    ->isEnabled($field->getTargetEntityTypeId(), $field->getTargetBundle());
  $form['translatable'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('Users may translate this field'),
    '#default_value' => $field->isTranslatable(),
    '#weight' => -1,
    '#disabled' => !$bundle_is_translatable,
    '#access' => $field->getFieldStorageDefinition()
      ->isTranslatable(),
  ];
  // Provide helpful pointers for administrators.
  if ($this->currentUser
    ->hasPermission('administer content translation') && !$bundle_is_translatable) {
    $toggle_url = Url::fromRoute('language.content_settings_page', [], [
      'query' => $this->redirectDestination
        ->getAsArray(),
    ])
      ->toString();
    $form['translatable']['#description'] = $this->t('To configure translation for this field, <a href=":language-settings-url">enable language support</a> for this type.', [
      ':language-settings-url' => $toggle_url,
    ]);
  }
  if ($field->isTranslatable()) {
    $element = $this->fieldSyncWidget
      ->widget($field);
    if ($element) {
      $form['third_party_settings']['content_translation']['translation_sync'] = $element;
      $form['third_party_settings']['content_translation']['translation_sync']['#weight'] = -10;
    }
  }
}

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