Problem/Motivation
In the refactoring issue, https://www.drupal.org/project/drupal/issues/3239134, the [0] hatch was added to misc/vertical-tabs.js in line 98: https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/misc/vertica...
If there are multiple forms on a page that uses vertical tabs, this will produce a javascript error because of the hard-coded [0].
The following javascript error occurs
Uncaught TypeError: Cannot set properties of undefined (setting 'value')
at Drupal.verticalTab.focus (vertical-tabs.js?v=9.4.0:98:78)
at vertical-tabs.js?v=9.4.0:65:40
at Array.forEach (<anonymous>)
at Object.attach (vertical-tabs.js?v=9.4.0:25:68)
at drupal.js?v=9.4.0:27:24
at Array.forEach (<anonymous>)
at Drupal.attachBehaviors (drupal.js?v=9.4.0:24:34)
at HTMLDivElement.open (nd_visualshortcodes.js?v=1:588:16)
at t.<computed>.<computed>._trigger (widget.js:706:13)
at t.<computed>.<computed>.open (dialog.js:300:8)
See screenshot:

Steps to reproduce
1. Create a page that has multiple forms using vertical tabs.
2. If you click one of the tabs, you get the javascript error described above.
Proposed resolution
Remove the [0] hatch from misc/vertical-tabs.js like such.
From:
focus() {
this.details
.siblings('.vertical-tabs__pane')
.each(function () {
const tab = $(this).data('verticalTab');
tab.details.hide();
tab.details.removeAttr('open');
tab.item.removeClass('is-selected');
})
.end()
.show()
.siblings(':hidden.vertical-tabs__active-tab')[0].value =
this.details.attr('id');
to
focus() {
this.details
.siblings('.vertical-tabs__pane')
.each(function () {
const tab = $(this).data('verticalTab');
tab.details.hide();
tab.details.removeAttr('open');
tab.item.removeClass('is-selected');
})
.end()
.show()
.siblings(':hidden.vertical-tabs__active-tab')
.get()
.forEach((hidden) => {
hidden.value = this.details.attr('id');
});
Using [0] directly assumes that there is exactly one hidden element matching `:hidden.vertical-tabs__active-tab`.But with the `.siblings(':hidden.vertical-tabs__active-tab')` selects all hidden sibling elements with the class 'vertical-tabs__active-tab'. and .get() method iterates through all the elements matching the condition.
With the latest changes
If there are multiple forms on a page that uses vertical tabs, this will produce a javascript error because of the hard-coded [0].
will be solved.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3291764
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3291764-the-hatch
changes, plain diff MR !8366
- 3291764-the-0-hatch
changes, plain diff MR !2420
Comments
Comment #3
baldwinlouie commentedattaching patch with proposed fix.
Comment #4
ranjith_kumar_k_u commentedComment #5
nod_The fix can not work, this is setting the value property on a jQuery object, not on all the matching DOM objects. I didn't see an issue with the replaced code at the time, would be great to have the steps to reproduce (or better add some test code that can reproduce the issue).
to add some details, the fix probably removes the error but it most likely breaks one of the vertical tabs functionality
Comment #6
rhiss commentedHello, i had this problem too while woking with the visual shortcode module. The patch was missing one line, after adding it woked out fine for me. I created a patch for core 9.4.x.
Comment #8
eugene.britRe-roll for 10.0.x
Comment #9
kevin.pfeifer commentedI also have a project with the drupal/shortcode module and the patch from #5 fixes the problem!
Comment #10
farnoosh commentedA quick solution to remove the error for now on drupal 10.1.0
Comment #11
mkdok commentedPatch #8 from @eugene.brit works perfectly on D10.
Comment #12
podarokBecause of #11
Comment #13
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #14
saranyamariappan commentedI am working on this.
Comment #15
kostyashupenkoFixed linting errors
Comment #16
smustgrave commentedCan we add a test case to cover this bug.
Comment #17
saganakat commentedHello,
Thank you for the contribution.
I've encountered a similar issue with the nd_visualshortcodes module.
Applying the patch from the merge request using Composer:
It worked for me. Thank you very much!
Tested on Drupal 9.5.11
Comment #18
candelas commentedThanks @kostyashupenko : patch #15 works in Drupal 10.2.2
Comment #20
bnjmnmThe patch solution is a bit more complex than it needs to be. Looping through the results called by get() should be sufficient as it will simply not loop through anything if there are no results
can be replaced with
.siblings(':hidden.vertical-tabs__active-tab').get().forEach((hidden) => hidden.value = this.details.attr('id');)The issue summary currently suggests a solution that 1) would not work as jQuery does not have a
.valueproperty 2) suggests reverting back to jquery value settings, which our linter would not allow.Update the suggestion to something that reflects what is actually happening.
As a committer / FEFM I'm fine with no tests on this as long as the scope of changes is just ensuring that
valueis not called on a result that might be empty.Comment #23
gauravvvv commentedUpdated proposed solution in the issue summary
Comment #25
smustgrave commentedAdding back the issue summary tag from #20.
Can it be updated to include the solution and why it's needed. Currently it's showing the diff of the MR essentially
Thanks!
Comment #26
utkarsh_33 commentedComment #27
utkarsh_33 commentedUpdated the Proposed resolution section explaining the solution.
Comment #28
smustgrave commentedThank you @Utkarsh_33 for providing the additional detail.
Believe all feedback has been addressed
Comment #29
nod_Committed and pushed 8c9ff22228 to 11.x and 035142460f to 11.0.x and 10691987c8 to 10.4.x and c55ad7b289 to 10.3.x. Thanks!