function ViewDuplicateForm::copyTranslations

Same name and namespace in other branches
  1. 11.x core/modules/views_ui/src/ViewDuplicateForm.php \Drupal\views_ui\ViewDuplicateForm::copyTranslations()

Copies all translations that existed on the original View.

Parameters

string $original_id: The original View ID.

1 call to ViewDuplicateForm::copyTranslations()
ViewDuplicateForm::submitForm in core/modules/views_ui/src/ViewDuplicateForm.php
Form submission handler for the 'clone' action.

File

core/modules/views_ui/src/ViewDuplicateForm.php, line 123

Class

ViewDuplicateForm
Form controller for the Views duplicate form.

Namespace

Drupal\views_ui

Code

private function copyTranslations(string $original_id) : void {
  if (!$this->moduleHandler
    ->moduleExists('config_translation')) {
    return;
  }
  $current_langcode = $this->languageManager
    ->getConfigOverrideLanguage()
    ->getId();
  $languages = $this->languageManager
    ->getLanguages();
  $original_name = 'views.view.' . $original_id;
  $duplicate_name = 'views.view.' . $this->entity
    ->id();
  foreach ($languages as $language) {
    $langcode = $language->getId();
    if ($langcode !== $current_langcode) {
      $original_translation = $this->languageManager
        ->getLanguageConfigOverride($langcode, $original_name)
        ->get();
      if ($original_translation) {
        $duplicate_translation = $this->languageManager
          ->getLanguageConfigOverride($langcode, $duplicate_name);
        $duplicate_translation->setData($original_translation);
        $duplicate_translation->save();
      }
    }
  }
}

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