Problem/Motivation

In English, in the Views UI, at the top there is a list of links that lets you add Block, Page, etc. displays to your view. It looks like this:
Add display on a view in English

If you look at the HTML though, actually each entry in the list has a link that says "Add Block", "Add page", etc. But something is being done in JavaScript to remove the word Add.

This doesn't work in Hungarian, because the order is reversed -- instead of "Add ____" it says "____ hozzaadaza" (forgive me for butchering the Hungarian). And the Add at the top says Hozzaadas -- capitalized and not the same word. I haven't looked at the JavaScript, but I think it must be somehow looking for the word "Add" at the beginning to be repeated in each link and removing it? Anyway, it doesn't work in at least this one other language. Here is what it looks like:
Add display on a view in Hungarian

(I noticed this because I was making automated screenshots for the User Guide in Hungarian, and it didn't look the same as my English screenshot.)

Proposed resolution

Either:
(a) make the links actually say just "Block", "Page", etc. without the funky JavaScript thing going on
(b) fix the funky JavaScript thing so it will work in other languages
(c) decide this is unimportant and do not worry about it in other languages
(d) don't do it at all even in English

Remaining tasks

Decide what to do, and do it. Or close this as "Won't fix".

User interface changes

Possibly.

API changes

No.

Data model changes

No.

CommentFileSizeAuthor
#40 2807241_40.patch5.45 KBStefdewa
#40 interdiff_38_40.txt564 bytesStefdewa
#38 2807241_38.patch5.46 KBStefdewa
#38 interdiff_32_38.txt651 bytesStefdewa
#37 2807241_37.patch5.6 KBStefdewa
#37 interdiff_32_37.txt979 bytesStefdewa
#32 Blokk_li_has_no_text.png455.59 KBStefdewa
#32 2807241_32.patch5.49 KBStefdewa
#32 interdiff_28_32.txt859 bytesStefdewa
#32 2807241_32_test_only.patch2.83 KBStefdewa
#28 2807241_28.patch5.45 KBStefdewa
#27 2807241_27.patch5.59 KBStefdewa
#27 2807241_27_test_only.patch2.79 KBStefdewa
#25 2807241_25.patch5.51 KBStefdewa
#25 2807241_25_test_only.patch2.72 KBStefdewa
#24 2807241_24.patch5.51 KBStefdewa
#23 2807241_23.patch2.8 KBStefdewa
#15 2807241-15.patch2.78 KBLendude
#7 2807241-views-ui-js-7.patch2.06 KBdroplet
#4 hungarian-add-display-with-patch.png13.3 KBjhodgdon
#4 catalan-add-display-with-patch.png11.77 KBjhodgdon
#4 english-add-display-with-patch.png12.11 KBjhodgdon
#4 2807241-views-ui-js-4.patch1.45 KBjhodgdon
#3 2807241-views-ui-js.patch1.25 KBjhodgdon
#2 views-block_add-block.png18.27 KBjhodgdon
views-block_add-block.png18.5 KBjhodgdon
views-block_add-block.png11.48 KBjhodgdon
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon created an issue. See original summary.

jhodgdon’s picture

FileSize
18.27 KB

And by the way, it doesn't work in Catalan either. In this case, the word "Add" apparently comes first in the strings, unlike Hungarian where it comes last, but it is still not removed from "Afegeix Bloc" etc. See screenshot:
Add display on a view in Catalan

jhodgdon’s picture

Title: Funky code in Views UI to make Add display list doesn't work in Hungarian » Funky code in Views UI to make Add display list doesn't work in non-English languages
Status: Active » Needs review
FileSize
1.25 KB

Here's the code that is doing this. It's in core/modules/views_ui/js/vies-admin.js in class/objectDrupal.behaviors.viewsUiRenderAddViewButton:

      var $addDisplayDropdown = $('<li class="add"><a href="#"><span class="icon add"></span>' + Drupal.t('Add') + '</a><ul class="action-list" style="display:none;"></ul></li>');
      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');

      // 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 () {
        var label = $(this).val();
        if (label.substr(0, 4) === 'Add ') {
          $(this).val(label.substr(4));
        }

So. The comment here is incorrect, because the variable $addDisplayDropdown a few lines up is actually translating the word "Add".

And what really needs to happen here is ... Hm. The label here is actually coming from a call in PHP to t('Add @type') with the @type substituted in from a call to something like t('Block') or t('Page') or t('Attachment') [the defined types of displays you can add]. So the JavaScript needs to call Drupal.t('Add @type') and then figure out where the "@type" appears in the string for the current language. Then it can extract that out.

For instance, if the translation of "Add @type" is something like "Foo @type barz", then it would need to take a substring that chops off the first 4 characters and the last 5, to find the "@type" portion of the label. If @type is at the start, then don't chop off anything at the start; if it's at the end, then don't chop off anything at the end. Pretty easy to calculate and achieve, using the JavaScript substr() method on strings. str.length(4) would take everything starting at character 4 to the end of the string, and str.length(-5) would take everything but the last 5 characters.

Let's see... how about this patch? I'm testing it in English, Hungarian, and Catalan by making User Guide screenshots, and will posts them when the tests finish. Or post a better patch if it doesn't work right. ;)

jhodgdon’s picture

That patch wasn't quite right. Here's a new one that works for English, Hungarian (where "Add" is a suffix), and Catalan (where "Add" is a prefix, but not 3 characters long). And some screenshots.
English:
English add display with patch
Hungarian:
Hungarian add display with patch
Catalan:
Catalan add display with patch

jhodgdon’s picture

Issue tags: +D8MI, +language-ui, +JavaScript

Adding some tags.

Also note that the CSS for this drop-down needs some adjustment probably, but that was present in the "Before" screenshots in the issue summary, and fixing it is probably out of scope for this issue. (The left side of the buttons in the drop-down list is cut off for some reason by the CSS.)

dawehner’s picture

Interesting, nice catch!

droplet’s picture

will it work?

dawehner’s picture

This is just beatiful! Thank you @droplet. Can we add some javascript test to ensure the UI works in the other language as well?

jhodgdon’s picture

Yes, it definitely makes the JS code much less funky! A good thing.

droplet’s picture

Any D8MI member could suggest me an efficient way (programmatically, I guess?) or help to write a patch for "Enable & Import second UI Lang" part.

jhodgdon’s picture

You'll need to have the modules language and locale enabled.
Then in your test you can go to admin/config/regional/language/add and submit the form there with drupalPostForm, using something like

$edit = [
  'predefined_langcode' => 'es',
]

and hitting the button 'Add language'.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Lendude’s picture

Reroll. Still needs those tests.

borisson_’s picture

Status: Needs review » Needs work

Needs work based to add the tests.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
Stefdewa’s picture

Assigned: Unassigned » Stefdewa
Issue tags: -JavaScript +JavaScript, +ddd2022

Starting work on test.

Stefdewa’s picture

Version: 9.3.x-dev » 9.4.x-dev
FileSize
2.8 KB

Rerolled patch for 9.4.x. Now continue with tests.

Stefdewa’s picture

Status: Needs work » Needs review
FileSize
5.51 KB

Added test. It checks for 'Blokk' in the dropdown inside the view in Hungarian. Without the patch this will fail because it will find 'Blokk hozzáadása'.

Stefdewa’s picture

Adding a test-only patch to show a failing test for the current codebase.
Added the complete patch also so testbot checks this patch last.

Stefdewa’s picture

Status: Needs review » Needs work

Setting back to "Needs work", will fix CSpell error tomorrow.

Stefdewa’s picture

Couldn't wait till tomorrow. Added CSpell ignore.

Stefdewa’s picture

Fixed es6 error in JS fix.

The last submitted patch, 27: 2807241_27_test_only.patch, failed testing. View results

dww’s picture

Issue tags: +Bug Smash Initiative

This looks interesting. Will try to review when I have a chance.

Lendude’s picture

Status: Needs review » Needs work

@Stefdewa nice job, great talking to you at DDD! Just some nitpicking:

  1. +++ b/core/modules/views_ui/tests/src/FunctionalJavascript/DisplayTest.php
    @@ -152,4 +158,54 @@ public function testAjaxRebuild() {
    +  /**
    +   * Tests adding a display.
    +   */
    

    Doc block needs something more specific.

  2. +++ b/core/modules/views_ui/tests/src/FunctionalJavascript/DisplayTest.php
    @@ -152,4 +158,54 @@ public function testAjaxRebuild() {
    +    $elements = $page->find('css', '.add ul')->findAll('css', 'input');
    

    I think this can just be one findAll('css', '.add ul input'), no need to chain it

  3. +++ b/core/modules/views_ui/tests/src/FunctionalJavascript/DisplayTest.php
    @@ -152,4 +158,54 @@ public function testAjaxRebuild() {
    +    $this->assertEquals('Blokk', $elements[1]->getAttribute('value'));
    

    Might be better to use getText here so we are sure we are testing what the user is seeing.

Stefdewa’s picture

Added an interdiff between 28 and 32.

  1. Updated the doc block to be more descriptive.
  2. Merged findAll.
  3. Didn't use getText because there isn't any actual text. There is only a value attribute. Added a screenshot to show this.

Status: Needs review » Needs work

The last submitted patch, 32: 2807241_32.patch, failed testing. View results

Stefdewa’s picture

Status: Needs work » Needs review

Setting "Needs review" again. Tests are green.

Lendude’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs JavaScript testing, -Needs tests

@Stefdewa thanks for working on this.

Looks good to me now, we got tests, RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/views_ui/src/ViewEditForm.php
@@ -793,7 +793,10 @@ public function renderDisplayTop(ViewUI $view) {
+          'data-drupal-dropdown-label' => $this->t('@display', ['@display' => $label]),

I think we can do 'data-drupal-dropdown-label' => $label, here. '@display' is not really a translatable string - ie. there's nothing to add. And $label is already a translatable string.

Stefdewa’s picture

Status: Needs work » Needs review
FileSize
979 bytes
5.6 KB

Updated per #36.

Stefdewa’s picture

A new line got in there somewhere. Removed it and created patch again.

pjbaert’s picture

Status: Needs review » Needs work

I found one more nitpick:

@@ -152,4 +158,54 @@ public function testAjaxRebuild() {
+   * Tests display labels in dropdown when adding a display in a multilingual setup.

The test title is too long. I suggest we rewrite this to: "Test if 'add' translations are filtered from multilingual display options."

Stefdewa’s picture

Good catch. Updated comment.

Stefdewa’s picture

Status: Needs work » Needs review
pjbaert’s picture

Assigned: Stefdewa » Unassigned
Status: Needs review » Reviewed & tested by the community

Great work!
Since only a comment was updated (& nothing functional was changed & all test passed on previous patch), I feel it safe to assume this is RTBC again.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 9182f0a and pushed to 10.0.x. Thanks!
Committed 7953bb5 and pushed to 9.4.x. Thanks!

Not porting to 9.3.x because JS change and UI change.

  • alexpott committed 9182f0a on 10.0.x
    Issue #2807241 by Stefdewa, jhodgdon, Lendude, droplet, pjbaert,...

  • alexpott committed 7953bb5 on 9.4.x
    Issue #2807241 by Stefdewa, jhodgdon, Lendude, droplet, pjbaert,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.