Skip to content
Snippets Groups Projects
Commit be8a77d8 authored by catch's avatar catch
Browse files

Issue #3360445 by Lendude, sboden, smustgrave, quietone: Duplicating a view...

Issue #3360445 by Lendude, sboden, smustgrave, quietone: Duplicating a view does not duplicate the translation
parent cbe1d286
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
namespace Drupal\views_ui; namespace Drupal\views_ui;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
* Form controller for the Views duplicate form. * Form controller for the Views duplicate form.
...@@ -11,6 +14,36 @@ ...@@ -11,6 +14,36 @@
*/ */
class ViewDuplicateForm extends ViewFormBase { 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} * {@inheritdoc}
*/ */
...@@ -68,13 +101,46 @@ protected function actions(array $form, FormStateInterface $form_state) { ...@@ -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. * A reference to a keyed array containing the current state of the form.
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { 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 = $this->entity->createDuplicate();
$this->entity->set('label', $form_state->getValue('label')); $this->entity->set('label', $form_state->getValue('label'));
$this->entity->set('id', $form_state->getValue('id')); $this->entity->set('id', $form_state->getValue('id'));
$this->entity->save(); $this->entity->save();
$this->copyTranslations($original_id);
// Redirect the user to the view admin form. // Redirect the user to the view admin form.
$form_state->setRedirectUrl($this->entity->toUrl('edit-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();
}
}
}
}
} }
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Drupal\Tests\views_ui\Functional; namespace Drupal\Tests\views_ui\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
/** /**
* Tests the UI for view duplicate tool. * Tests the UI for view duplicate tool.
* *
...@@ -9,6 +11,11 @@ ...@@ -9,6 +11,11 @@
*/ */
class DuplicateTest extends UITestBase { class DuplicateTest extends UITestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['config_translation', 'locale', 'language'];
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -27,10 +34,17 @@ protected function setUp($import_test_views = TRUE, $modules = ['views_test_conf ...@@ -27,10 +34,17 @@ protected function setUp($import_test_views = TRUE, $modules = ['views_test_conf
* Checks if duplicated view exists and has correct label. * Checks if duplicated view exists and has correct label.
*/ */
public function testDuplicateView() { public function testDuplicateView() {
$language_manager = $this->container->get('language_manager');
ConfigurableLanguage::createFromLangcode('nl')->save();
// Create random view. // Create random view.
$random_view = $this->randomView(); $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. // Initialize array for duplicated view.
$view = []; $view = [];
...@@ -47,6 +61,9 @@ public function testDuplicateView() { ...@@ -47,6 +61,9 @@ public function testDuplicateView() {
// Assert that the page title is correctly displayed. // Assert that the page title is correctly displayed.
$this->assertSession()->pageTextContains($view['label']); $this->assertSession()->pageTextContains($view['label']);
$copy_translation = $language_manager->getLanguageConfigOverride('nl', 'views.view.' . $view['id']);
$this->assertEquals(['label' => 'NL label'], $copy_translation->get());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment