You are here

public function MetatagViewsTranslationForm::submitForm in Metatag 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

metatag_views/src/Form/MetatagViewsTranslationForm.php, line 229

Class

MetatagViewsTranslationForm
Defines a form for translating meta tags for views.

Namespace

Drupal\metatag_views\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Get the values of metatags.
  $values = $form_state
    ->getValue('metatags');
  $translated_values = array_combine(array_keys($values), array_column($values, 'translation'));
  $config_name = $this->view
    ->getConfigDependencyName();
  $config_path = 'display.' . $this->displayId . '.display_options.display_extenders.metatag_display_extender.metatags';

  // Set configuration values based on form submission and source values.
  $base_config = $this
    ->configFactory()
    ->getEditable($config_name);
  $config_translation = $this->languageManager
    ->getLanguageConfigOverride($this->language
    ->getId(), $config_name);

  // Save the configuration values, if they are different from the source
  // values in the base configuration. Otherwise remove the override.
  $source_values = $this
    ->removeEmptyTags($base_config
    ->get($config_path));
  if ($source_values !== $translated_values) {
    $config_translation
      ->set($config_path, $translated_values);
  }
  else {
    $config_translation
      ->clear($config_path);
  }

  // If no overrides, delete language specific configuration file.
  $saved_config = $config_translation
    ->get();
  if (empty($saved_config)) {
    $config_translation
      ->delete();
  }
  else {
    $config_translation
      ->save();
  }

  // Redirect back to the views list.
  $form_state
    ->setRedirect('metatag_views.metatags.translate_overview', [
    'view_id' => $this->viewId,
    'display_id' => $this->displayId,
  ]);
  $this
    ->messenger()
    ->addMessage($this
    ->t('Successfully updated @language translation.', [
    '@language' => $this->language
      ->getName(),
  ]));
}