You are here

public function LingotekElementInfoAlterForm::submit in Lingotek Translation 3.4.x

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

Form submission handler for element added with lingotek_language_configuration_element_process().

Stores the content translation settings.

Parameters

array $form: The form.

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

See also

lingotek_language_configuration_element_validate()

File

src/Form/LingotekElementInfoAlterForm.php, line 166

Class

LingotekElementInfoAlterForm
Adds the option to use Lingotek to translate content entities

Namespace

Drupal\lingotek\Form

Code

public function submit(array $form, FormStateInterface $form_state) {
  $key = $form_state
    ->get([
    'content_translation_for_lingotek',
    'key',
  ]);
  $context = $form_state
    ->get([
    'language',
    $key,
  ]);
  $enabled_for_lingotek = $form_state
    ->getValue([
    $key,
    'content_translation_for_lingotek',
  ]);
  $enabled = $form_state
    ->getValue([
    $key,
    'content_translation',
  ]);
  $entity_type_id = $context['entity_type'];
  $bundle_id = $context['bundle'];
  $shouldClearCaches = FALSE;
  if ($enabled) {
    if ($enabled_for_lingotek) {
      if (!$this->lingotekConfiguration
        ->isEnabled($entity_type_id, $bundle_id)) {
        $this->lingotekConfiguration
          ->setEnabled($entity_type_id, $bundle_id);
        $entity_type = \Drupal::entityTypeManager()
          ->getDefinition($entity_type_id);
        $content_translation_manager = \Drupal::service('content_translation.manager');
        $storage_definitions = \Drupal::service('entity_field.manager')
          ->getFieldStorageDefinitions($entity_type_id);
        if ($content_translation_manager
          ->isSupported($context['entity_type'])) {
          $fields = \Drupal::service('entity_field.manager')
            ->getFieldDefinitions($entity_type_id, $bundle_id);

          // Find which fields the user previously selected
          foreach ($fields as $field_id => $field_definition) {

            // We allow non-translatable entity_reference_revisions fields through.
            // See https://www.drupal.org/node/2788285
            if (!empty($storage_definitions[$field_id]) && $storage_definitions[$field_id]
              ->getProvider() != 'content_translation' && !in_array($storage_definitions[$field_id]
              ->getName(), [
              $entity_type
                ->getKey('langcode'),
              $entity_type
                ->getKey('default_langcode'),
              'revision_translation_affected',
            ]) && ($field_definition
              ->isTranslatable() || ($field_definition
              ->getType() == 'cohesion_entity_reference_revisions' || $field_definition
              ->getType() == 'entity_reference_revisions' || $field_definition
              ->getType() == 'path')) && !$field_definition
              ->isComputed() && !$field_definition
              ->isReadOnly()) {
              if ($value = $this->lingotekConfiguration
                ->isFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id)) {
                break;
              }
              if ($this->lingotekConfiguration
                ->shouldFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id)) {
                $this->lingotekConfiguration
                  ->setFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id);
              }
            }
            elseif ($field_definition
              ->getType() == 'path' && $field_definition
              ->isComputed()) {
              if ($value = $this->lingotekConfiguration
                ->isFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id)) {
                break;
              }
              elseif ($this->lingotekConfiguration
                ->shouldFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id)) {
                $this->lingotekConfiguration
                  ->setFieldLingotekEnabled($entity_type_id, $bundle_id, $field_id);
              }
            }
          }
        }
      }
      $shouldClearCaches = TRUE;
    }
    else {
      if ($this->lingotekConfiguration
        ->isEnabled($context['entity_type'], $context['bundle'])) {
        $this->lingotekConfiguration
          ->setEnabled($context['entity_type'], $context['bundle'], FALSE);
        $shouldClearCaches = TRUE;
      }
    }
  }
  else {
    $this
      ->messenger()
      ->addError(t("You must Enable Translation to Enable Translation for Lingotek"));
  }
  if ($shouldClearCaches) {
    $this->entityTypeManager
      ->clearCachedDefinitions();
    $this->routeBuilder
      ->setRebuildNeeded();
  }
}