Problem/Motivation

When we have multiple top level host entity paragraph fields, they misbehave.

With two fields, we have two perspective tabs.
But first, the tabs doesn't properly detect if their field has children.
And then, each tab filters also the children of the other and is stealing the active selection of the other too.

Proposed resolution

Properly limit each top level field to only care about its own children.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

miro_dietiker created an issue. See original summary.

toncic’s picture

Assigned: Unassigned » toncic
Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new6.03 KB
new61.56 KB

Fixed problem with JS. Uploading screenshot.

pivica’s picture

Status: Needs review » Needs work
  1. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +      var $parWidgets = $('.field--widget-paragraphs', context);
    

    Indentation is not good in this part until the line 75.

  2. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +      var $parWidgets = $('.field--widget-paragraphs', context);
    ...
    +              /** Activate content tab visually if there is no previously
    

    We are in behavior here so we should use once() in order to avoid reattaching behaviors, something like

    var $parWidgets = $('.field--widget-paragraphs', context).once('paragraphs-bodytabs');

  3. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +          /** Set content fields to visible when tabs are created. After an
    ...
    +              /** Activate content tab visually if there is no previously
    +               * activated tab. */
    

    Not 100% sure but shouldn't we use // comments here and not doc block comments?

    https://www.drupal.org/docs/develop/standards/javascript/javascript-codi...

    > C style comments (/* */) and standard C++ comments (//) are both allowed.

    So you should use /* or // but not /**

  4. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +              $parTabs.find('#content').removeClass('is-active');
    +              $parTabs.find('#behavior').addClass('is-active');
    

    #content and #behavior elements are used on couple of places. We should create vars for them also.

  5. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +              $parTabsWrapper.find('.paragraphs-tabs').show();
    ...
    +              $parTabsWrapper.find('.paragraphs-tabs').hide();
    

    Can we use $parTabs var here?

  6. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +              $mainRegion.removeClass('behavior-active');
    +              $mainRegion.removeClass('content-active');
    +              $mainRegion.removeClass('is-active');
    

    You can shorten this by writing

    $mainRegion.removeClass('behavior-active content-active is-active');

  7. +++ b/js/paragraphs.admin.js
    @@ -9,58 +9,70 @@
    +              $(context).find(el.attr('href')).addClass('is-active');
    

    This does not looks good because it is searching the context, can we be more specific?

miro_dietiker’s picture

@Ivica 6. -- Why is it not good to limit activity to the event context? Usually we need to do exactly that to properly support ajax actions and avoid double bindings.

pivica’s picture

> @Ivica 6. -- Why is it not good to limit activity to the event context? Usually we need to do exactly that to properly support ajax actions and avoid double bindings.

I was just asking can we be more specific and instead of context use $parWidgets or some other var that is that holds that elements so execution is a little bit faster .

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new5.72 KB
new5.5 KB

Addressed #3.

pivica’s picture

Status: Needs review » Needs work

JS looks better, couple of more things:

  1. +++ b/js/paragraphs.admin.js
    @@ -8,70 +8,72 @@
    +        /* Set content fields to visible when tabs are created. After an
    +           action being performed, stay on the same perspective. */
    

    I know drupal doc page says both // and /* are fine, but i've just checked all Drupal JS files in core/misc and all of them use // comment style. Can we please change then all code comments to // syntax.

  2. +++ b/js/paragraphs.admin.js
    @@ -8,70 +8,72 @@
    +          $parWidget.removeClass('content-active');
    +          $parWidget.addClass('behavior-active');
    

    Small thing you could do this in one line

    $parWidget.removeClass('content-active').addClass('behavior-active');

  3. +++ b/js/paragraphs.admin.js
    @@ -8,70 +8,72 @@
    +          var el = jQuery(this);
    

    This is a jquery object so you should use $el instead of el.

    One more thing, this is jQuery object of this so usually you use $this naming for this. Yeah too many 'this' in previous sentence ;)
    Also don't use jQuery(this) but do $(this). So

    var $this = $(this);

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new2.72 KB
new7.54 KB

Addressed comment #7.

pivica’s picture

JS code looks good, didn't tested this so will not put into RTBC.

miro_dietiker’s picture

Status: Needs review » Needs work

Please rebase. This patch contains at least one accidental revert.

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new11.22 KB

Re-basing.

miro_dietiker’s picture

Status: Needs review » Needs work

Almost. :-)

If you have two fields and the first one has paragraphs with behavior plugins, but the second hasn't... it still shows the perspective tabs to both fields, but it should only display the tabs to the first field.

toncic’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new49.51 KB

Tested a bit more with the same steps like you. Here it is screenshot:

We have two fields, one with Grid paragraph type which has behavior form and another one with Text paragraph type which has not behaviors form. Perspective tabs are presented only for first field.

toncic’s picture

StatusFileSize
new5.69 KB

Previous rebase was wrong.

  • miro_dietiker committed ff82c78 on 8.x-1.x authored by toncic
    Issue #2877847 by toncic, miro_dietiker, pivica: Perspective tabs...
miro_dietiker’s picture

Status: Needs review » Fixed

Yeah, tested the new reroll and it seems to work fine. Committed. :-)

miro_dietiker’s picture

Status: Fixed » Needs work

Reverting this commit. It breaks deep nested behavior field visibility. Next time we do it right.

  • miro_dietiker committed 6f1109d on 8.x-1.x
    Revert "Issue #2877847 by toncic, miro_dietiker, pivica: Perspective...
toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new8.08 KB
new7.3 KB

Fixed problem with nested paragraph.

miro_dietiker’s picture

I'll test this a bit on some production environment while working with content.

toncic’s picture

StatusFileSize
new2.52 KB
new7.81 KB

There was a problem whit changing perspectives if we have more than one field e.g. 2 field and on both fields we are on the same tabs.
Fixed that.

miro_dietiker’s picture

Status: Needs review » Needs work
+++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
@@ -772,7 +772,9 @@ class ParagraphsWidget extends WidgetBase {
+      if ($items->getEntity()->getEntityTypeId() != 'node') {

This is not a reliable detection for the host entity. A paragraph field could be attached to any other host entity, for instance a term. And we need to test cover that case.

Other than that, it seems to do a good job while testing.

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new1.45 KB
new8.47 KB

Fixed problem from #22.
After discussion with @Berdir, we don't need test in this issue.

berdir’s picture

Status: Needs review » Needs work

That's not what @Berdir said :)

He said we don't need to test this node vs user stuff because that check was bogus anyway and there is no such code anymore.

We can check the correct adding of the nested class and we if we change which parts are visible vs not, then we can possibly also extend the JS test with that.

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new10.48 KB
new1.83 KB

Added tests.

berdir’s picture

Status: Needs review » Needs work

It's not quite clear to me how this would fail, please provide a test-only patch.

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new2 KB

Test only patch.

berdir’s picture

Status: Needs review » Needs work

Needs work for discussed test improvements.

toncic’s picture

Status: Needs work » Needs review
StatusFileSize
new12.91 KB
new4.74 KB

Improved test but didn't test. Let's see what test bot will say.

berdir’s picture

Worked on the test together with @toncic.

  • miro_dietiker committed 7357156 on 8.x-1.x authored by toncic
    Issue #2877847 by toncic, Berdir, miro_dietiker, pivica: Perspective...
miro_dietiker’s picture

Status: Needs review » Fixed

Commited, while fixing some codestyle things and comments.

  • miro_dietiker committed 51893b8 on 8.x-1.x authored by toncic
    Issue #2877847 by toncic, Berdir, miro_dietiker, pivica: Perspective...

Status: Fixed » Closed (fixed)

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