You are here

public function LingotekFieldConfigEditForm::form in Lingotek Translation 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  2. 4.0.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  3. 3.1.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  4. 3.2.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  5. 3.3.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  6. 3.4.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  7. 3.5.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  8. 3.6.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  9. 3.7.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()
  10. 3.8.x src/Form/LingotekFieldConfigEditForm.php \Drupal\lingotek\Form\LingotekFieldConfigEditForm::form()

Adds "Use Lingotek to translate this field" to each field for fields with properties, "Use Lingotek to translate the ___ element" is added

Parameters

array $form: The form definition array for the language content settings.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/Form/LingotekFieldConfigEditForm.php, line 47

Class

LingotekFieldConfigEditForm
Allow to configure fields translation with Lingotek in the proper field forms

Namespace

Drupal\lingotek\Form

Code

public function form(array &$form, FormStateInterface $form_state) {
  $field = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_id = $field
    ->getTargetEntityTypeId();
  $bundle_id = $field
    ->getTargetBundle();
  $field_id = $field
    ->getName();

  // Add the option to translate the field with Lingotek
  if (!$form['translatable']['#disabled']) {
    $form['translatable_for_lingotek'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Lingotek to translate this field'),
      '#default_value' => $this->lingotekConfig
        ->isFieldLingotekEnabled($entity_id, $bundle_id, $field_id),
      '#weight' => -1,
      '#states' => [
        'visible' => [
          ':input[name="translatable"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['actions']['submit']['#submit'][] = [
      $this,
      'submitForm',
    ];
  }
  if (isset($form['third_party_settings']['content_translation'])) {
    if (isset($form['third_party_settings']['content_translation']['translation_sync'])) {
      $content_translation_options = $form['third_party_settings']['content_translation']['translation_sync']['#options'];
      $properties_checkbox_choice = $this->lingotekConfig
        ->getFieldPropertiesLingotekEnabled($entity_id, $bundle_id, $field_id);
      $form['translatable_for_lingotek_properties'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('Lingotek Translation'),
        '#weight' => 15,
        '#states' => [
          'visible' => [
            ':input[name="translatable_for_lingotek"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      foreach ($content_translation_options as $content_translation_option_key => $content_translation_option) {
        $form['translatable_for_lingotek_properties_' . $content_translation_option_key] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Use Lingotek to translate the %content_translation_option element', [
            '%content_translation_option' => $content_translation_option,
          ]),
          '#default_value' => $properties_checkbox_choice ? $properties_checkbox_choice[$content_translation_option_key] : FALSE,
          '#weight' => 15,
          '#states' => [
            'visible' => [
              ':input[name="translatable_for_lingotek"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
        ];
        if ($properties_checkbox_choice && $properties_checkbox_choice[$content_translation_option_key]) {
          $form['translatable_for_lingotek_properties_' . $content_translation_option_key]['#default_value'] = 1;
        }
      }
      if (!$properties_checkbox_choice) {
        $properties_checkbox_choice = [];
      }
      $field
        ->setThirdPartySetting('lingotek', 'translation_sync', $properties_checkbox_choice);
    }
  }
}