Issue #2807241 by Stefdewa, jhodgdon, Lendude, droplet, pjbaert, alexpott: Funky code in Views UI to make Add display list doesn't work in non-English languages

merge-requests/2080/head
Alex Pott 2022-04-08 16:23:01 +02:00
parent 70d94d4972
commit 7953bb57e0
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
4 changed files with 64 additions and 13 deletions

View File

@ -386,14 +386,9 @@
.end()
.eq(-1)
.addClass('last');
// Remove the 'Add ' prefix from the button labels since they're being
// placed in an 'Add' dropdown. @todo This assumes English, but so does
// $addDisplayDropdown above. Add support for translation.
$displayButtons.each(function () {
const label = this.value;
if (label.substr(0, 4) === 'Add ') {
this.value = label.substr(4);
}
const $this = $(this);
this.value = $this.attr('data-drupal-dropdown-label');
});
$addDisplayDropdown.appendTo($menu);

View File

@ -170,11 +170,8 @@
var $displayButtons = $menu.nextAll('input.add-display').detach();
$displayButtons.appendTo($addDisplayDropdown.find('.action-list')).wrap('<li>').parent().eq(0).addClass('first').end().eq(-1).addClass('last');
$displayButtons.each(function () {
var label = this.value;
if (label.substr(0, 4) === 'Add ') {
this.value = label.substr(4);
}
var $this = $(this);
this.value = $this.attr('data-drupal-dropdown-label');
});
$addDisplayDropdown.appendTo($menu);
$menu.find('li.add > a').on('click', function (event) {

View File

@ -793,7 +793,10 @@ class ViewEditForm extends ViewFormBase {
'#value' => $this->t('Add @display', ['@display' => $label]),
'#limit_validation_errors' => [],
'#submit' => ['::submitDisplayAdd', '::submitDelayDestination'],
'#attributes' => ['class' => ['add-display']],
'#attributes' => [
'class' => ['add-display'],
'data-drupal-dropdown-label' => $label,
],
// Allow JavaScript to remove the 'Add ' prefix from the button label when
// placing the button in an "Add" dropdown menu.
'#process' => array_merge(['views_ui_form_button_was_clicked'], $this->elementInfo->getInfoProperty('submit', '#process', [])),

View File

@ -3,10 +3,14 @@
namespace Drupal\Tests\views_ui\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\locale\SourceString;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewTestData;
use Drupal\Tests\node\Traits\NodeCreationTrait;
// cSpell:ignore Blokk hozzáadása
/**
* Tests the display UI.
*
@ -23,6 +27,8 @@ class DisplayTest extends WebDriverTestBase {
'block',
'contextual',
'node',
'language',
'locale',
'views',
'views_ui',
'views_test_config',
@ -152,4 +158,54 @@ class DisplayTest extends WebDriverTestBase {
$assert_session->pageTextContains('This is text added to the display edit form');
}
/**
* Test if 'add' translations are filtered from multilingual display options.
*/
public function testAddDisplayBlockTranslation() {
// Set up an additional language (Hungarian).
$langcode = 'hu';
ConfigurableLanguage::createFromLangcode($langcode)->save();
$config = $this->config('language.negotiation');
$config->set('url.prefixes', [$langcode => $langcode])->save();
\Drupal::service('kernel')->rebuildContainer();
\Drupal::languageManager()->reset();
// Add Hungarian translations.
$this->addTranslation($langcode, 'Block', 'Blokk');
$this->addTranslation($langcode, 'Add @display', '@display hozzáadása');
$this->drupalGet('hu/admin/structure/views/view/test_display');
$page = $this->getSession()->getPage();
$page->find('css', '#views-display-menu-tabs .add')->click();
// Wait for the animation to complete.
$this->assertSession()->assertWaitOnAjaxRequest();
// Look for the input element, always in second spot.
$elements = $page->findAll('css', '.add ul input');
$this->assertEquals('Blokk', $elements[1]->getAttribute('value'));
}
/**
* Helper function for adding interface text translations.
*/
private function addTranslation($langcode, $source_string, $translation_string) {
$storage = \Drupal::service('locale.storage');
$string = $storage->findString(['source' => $source_string]);
if (is_null($string)) {
$string = new SourceString();
$string
->setString($source_string)
->setStorage($storage)
->save();
}
$storage->createTranslation([
'lid' => $string->getId(),
'language' => $langcode,
'translation' => $translation_string,
])->save();
}
}