diff --git a/core/modules/views_ui/src/ViewDuplicateForm.php b/core/modules/views_ui/src/ViewDuplicateForm.php index db2ec3f314cde9694b4b4fb0ab2b757a4633bd6f..5b39012acaec050874f95a33885fd32b2c79bfa4 100644 --- a/core/modules/views_ui/src/ViewDuplicateForm.php +++ b/core/modules/views_ui/src/ViewDuplicateForm.php @@ -2,7 +2,10 @@ namespace Drupal\views_ui; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Language\LanguageManagerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the Views duplicate form. @@ -11,6 +14,36 @@ */ class ViewDuplicateForm extends ViewFormBase { + /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected LanguageManagerInterface $languageManager; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('module_handler'), + $container->get('language_manager') + ); + } + + /** + * Constructs a ViewDuplicateForm. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler + * Drupal's module handler. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * The language manager. + */ + public function __construct(ModuleHandlerInterface $moduleHandler, LanguageManagerInterface $language_manager) { + $this->setModuleHandler($moduleHandler); + $this->languageManager = $language_manager; + } + /** * {@inheritdoc} */ @@ -68,13 +101,46 @@ protected function actions(array $form, FormStateInterface $form_state) { * A reference to a keyed array containing the current state of the form. */ public function submitForm(array &$form, FormStateInterface $form_state) { + // The original ID gets set to NULL when duplicating, so we need to store it + // here. + $original_id = $this->entity->id(); $this->entity = $this->entity->createDuplicate(); $this->entity->set('label', $form_state->getValue('label')); $this->entity->set('id', $form_state->getValue('id')); $this->entity->save(); + $this->copyTranslations($original_id); // Redirect the user to the view admin form. $form_state->setRedirectUrl($this->entity->toUrl('edit-form')); } + /** + * Copies all translations that existed on the original View. + * + * @param string $original_id + * The original View ID. + */ + 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(); + } + } + } + } + } diff --git a/core/modules/views_ui/tests/src/Functional/DuplicateTest.php b/core/modules/views_ui/tests/src/Functional/DuplicateTest.php index bdddf15b0fc85028d7a875ebed5c913470f3549e..0f4dfdc72c2bdb0dff9ccda5c46f79e0bce5874f 100644 --- a/core/modules/views_ui/tests/src/Functional/DuplicateTest.php +++ b/core/modules/views_ui/tests/src/Functional/DuplicateTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\views_ui\Functional; +use Drupal\language\Entity\ConfigurableLanguage; + /** * Tests the UI for view duplicate tool. * @@ -9,6 +11,11 @@ */ class DuplicateTest extends UITestBase { + /** + * {@inheritdoc} + */ + protected static $modules = ['config_translation', 'locale', 'language']; + /** * {@inheritdoc} */ @@ -27,10 +34,17 @@ protected function setUp($import_test_views = TRUE, $modules = ['views_test_conf * Checks if duplicated view exists and has correct label. */ public function testDuplicateView() { + $language_manager = $this->container->get('language_manager'); + ConfigurableLanguage::createFromLangcode('nl')->save(); // Create random view. $random_view = $this->randomView(); + // Add a translation to the View. + $translation = $language_manager->getLanguageConfigOverride('nl', 'views.view.' . $random_view['id']); + $translation->setData(['label' => 'NL label']); + $translation->save(); + // Initialize array for duplicated view. $view = []; @@ -47,6 +61,9 @@ public function testDuplicateView() { // Assert that the page title is correctly displayed. $this->assertSession()->pageTextContains($view['label']); + + $copy_translation = $language_manager->getLanguageConfigOverride('nl', 'views.view.' . $view['id']); + $this->assertEquals(['label' => 'NL label'], $copy_translation->get()); } }