Problem/Motivation

I noticed a z-index issue when using the "Insert from Media Library" from within CKEditor.
After adding a new Media document, the fields that use the select2 widget are not working properly because the select2 z-index is lower than the dialog's z-index.

The dialog in this case will have a z-index of 10001.
See ckeditor.js:155, where CKEDITOR.config.baseFloatZIndex has a default value of 10000

$(window).on('dialogcreate', function (e, dialog, $element, settings) {
  $('.ui-dialog--narrow').css('zIndex', CKEDITOR.config.baseFloatZIndex + 1);
});

Steps to reproduce

- Create or update a media type and use a select2 field widget for one of the fields
- Setup the Media Library button with CKEditor
- Click on the "Insert from Media Library" CKEditor button
- Add a document
- > The field that uses select2 is not working properly

Proposed resolution

This issue was also encountered in select2_all module: #3091977: Fix compatibility with jQuery UI modal zindex

As mentioned by Dave there,
1. The quick fix is to simply increase the z-index to CKEDITOR.config.baseFloatZIndex + 2 = 10002.
2. The proper way to fix this would be to use dropdownParent.

Remaining tasks

?

User interface changes

?

API changes

?

Data model changes

?

Issue fork select2-3211796

Command icon 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:

Comments

herved created an issue. See original summary.

herved’s picture

Issue summary: View changes
herved’s picture

StatusFileSize
new1.48 KB

So here would be the quickfix version.

herved’s picture

Status: Active » Needs review
StatusFileSize
new672 bytes

Good news, it seems passing the form ID to dropdownParent works. I don't know if it's that important to pass the modal to it instead of the form. I'm guessing that would be a bit harder to do.
So here is a proposal for the proper fix.

herved’s picture

StatusFileSize
new748 bytes

Let's just add a check if dropdownParent is not yet set.
Just in case someone wants to have a dropdown inside a form.

briantschu’s picture

The patch from #5 fixed this issue for me. Thanks!

tim-diels’s picture

Status: Needs review » Reviewed & tested by the community

This not only happens in CKEditor but also when you embed a form in a modal dialog. The provided patch in #5 fixed the error for me. Marking as RTBC!

heddn’s picture

Title: Issue with z-index when opening dialog from CKEditor » Issue with z-index when opening in modal dialog

This solved the same issue for me, but in a layout builder dialog. Meaning, this isn't specific to ckeditor dialogs. Also, #3258855: Autocomplete results are not visible when using select2 widget in a dialog seems a duplicate to here. Will close it as such.

+1 on RTBC.

ciprian.stavovei’s picture

The patch work fine for me too.
+1 on RBTC.

hswong3i made their first commit to this issue’s fork.

beunerd’s picture

On a multivalue field referencing a paragraph (e.g., multiple images or otherwise) the select2 widget cannot attach to complete_form['#id'] because the id gets a unique string appended to it each time another instance of the paragraph is added. So, for example, you might have a form "node-article-form", but when another paragraph is added you'll get something like "node-article-form--o142r5QGpA8". This patch, instead, gets the form id that appears in the markup (i.e., without that unique string).

dinazaur’s picture

Status: Reviewed & tested by the community » Needs work

The problem with such solution that Select2 will stop working with Paragraph Classic widget or for instance with Inline Entity Form complex widget.

dinazaur’s picture

The solution could be to add the ability to set dropdownParent inside the widget itself. So the form id will be taken from the appropriate subform if this is the case.

dinazaur’s picture

Status: Needs work » Needs review
dinazaur’s picture

StatusFileSize
new3.54 KB

It was not as easy as I thought. I should've to implement special handling for regular forms and for widget that is inside ief/paragraph forms.

Another solution could be to check if current request is trigger by an ajax request and only, in that case, add dropdownParent inside Select2 element.

    if (
      // The most reliable way to check if request was triggered by an ajax request.
      \Drupal::request()->isXmlHttpRequest()
      && empty($element['#select2']['dropdownParent'])
      && !empty($complete_form['#id'])
    ) {
      $element['#select2']['dropdownParent'] = '#' . $complete_form['#id'];
    }

But I'm not sure about the consequences.

dinazaur’s picture

jasonmce’s picture

Status: Needs review » Reviewed & tested by the community

#17 Fixed the issue I was having. No regression issues found in small positive manual test. Code changes are easy to read and reasonable.

bakop’s picture

Hi, tested patch #17 on a Drupal 9.5.9 and it works.
RTBC +1

jurgenhaas’s picture

Confirmed, #17 fixes the issue for me too, here on Drupal 10.1

herved’s picture

Same here on D10.1 with #17, thanks! +1

herved’s picture

+++ b/src/Element/Select2.php
@@ -209,7 +210,11 @@ class Select2 extends Select {
+      $element['#prefix'] = '<div id="' . $wrapper_id . '">';
+      $element['#suffix'] = '</div>';

Update: I haven't really checked in depth but why do we need a wrapper div here?
This would need a change record or similar as it can break existing styling in some cases.

daletrexel’s picture

One more success with #17, Drupal 10.1.8, PHP 8.2. Thanks for the work on this!

@herved the patch does not work without the #prefix/#suffix wrapper because the ID is key for the dropdownParent property to have a unique ID to work with/reference. If $wrapper_id isn't added as an ID here, then you'd need to add the ID to some other element wrapper around the field, but within the modal.

daletrexel’s picture

I should add that I have just found some cases where this patch does break styling of some Select2 elements on the site I applied it to, so a change record/notice is definitely important if you stick with the current solution. If you can place the ID used by dropdownParent in some other pre-existing wrapper, that would have less impact.

daletrexel’s picture

Is there a way we can determine when the wrapper is actually needed in the Select2 class, vs. simply setting the dropdownParent to the form ID (without adding any wrapper), as was the approach at the start?

After further review of the impacts throughout our site, I've found that the wrapper is not actually needed for any of our Select2 fields, but when present it DOES cause significant styling issues in some cases.

I think it would be well worth figuring out how to be more selective about the inclusion of the wrapper, to minimize impact to users of this module. I'd take a stab at it myself, but I don't have any use cases that actually need it, so it's hard for me to know where to begin and how to test.

tame4tex’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new871 bytes

Could the issue be more easily solved in JS by determining if the element is in a modal and then getting the parent form object to set as the `dropdownParent `?

The attached patch is working for me in paragraphs, modals and regular forms without the need for a new wrapper element.

chr.fritsch’s picture

Status: Needs review » Postponed (maintainer needs more info)
StatusFileSize
new421.92 KB

Is this still an issue?
Because for me it seems to work.

image

chrisla’s picture

Patch #25 did not work for me using Layout Paragraphs where a field within a Paragraph has Select 2 enabled for form display.

Patch from #17 works in this scenario for me.

heddn’s picture

Status: Postponed (maintainer needs more info) » Needs review

Yes, it is still an issue. Drupal 10.3, Select2 4.0.13, drupal/select 1.15.0. Using MR 8, not the patch (which seem to be just static copies of the MR).

nicodh’s picture

Path in #17 works for me with Layout Paragraphs and select2 field in modal form

manuvelasco’s picture

MR 8 Working for a dialog modal bootstrap and drupal 10

fanton’s picture

Patch #17 fixes the issue for me, on Drupal 10.2

mchu-ed’s picture

We are having a content type having a field that is using the Layout Paragraph widget, and the select boxes are not working as stated in this issue.
Applying the patch #17 fixes the issue in our environment , Drupal v10.5.6, Layout Paragraph v2.1.1and Select 2 v2.0.0.
The select boxes are now working as intended.

joe huggans’s picture

Tested in Drupal 11.2 after applying this patch for jQuery 4 support - https://www.drupal.org/project/select2/issues/3509122

Was able to replicate the issue, and the patch at #17 fixes the issue.

vmarchuk’s picture

Patch #17 fixed the problem! Drupal 11.3 + Select2 2.0.0