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
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
Comment #2
herved commentedComment #3
herved commentedSo here would be the quickfix version.
Comment #4
herved commentedGood 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.
Comment #5
herved commentedLet's just add a check if dropdownParent is not yet set.
Just in case someone wants to have a dropdown inside a form.
Comment #6
briantschuThe patch from #5 fixed this issue for me. Thanks!
Comment #7
tim-dielsThis 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!
Comment #8
heddnThis 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.
Comment #9
ciprian.stavovei commentedThe patch work fine for me too.
+1 on RBTC.
Comment #12
beunerd commentedOn 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).Comment #13
dinazaur commentedThe problem with such solution that Select2 will stop working with Paragraph Classic widget or for instance with Inline Entity Form complex widget.
Comment #14
dinazaur commentedThe 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.
Comment #15
dinazaur commentedComment #16
dinazaur commentedIt 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.
But I'm not sure about the consequences.
Comment #17
dinazaur commentedComment #18
jasonmce commented#17 Fixed the issue I was having. No regression issues found in small positive manual test. Code changes are easy to read and reasonable.
Comment #19
bakopHi, tested patch #17 on a Drupal 9.5.9 and it works.
RTBC +1
Comment #20
jurgenhaasConfirmed, #17 fixes the issue for me too, here on Drupal 10.1
Comment #21
herved commentedSame here on D10.1 with #17, thanks! +1
Comment #22
herved commentedUpdate: 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.
Comment #23
daletrexelOne 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_idisn'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.Comment #24
daletrexelI 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.
Comment #25
daletrexelIs 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.
Comment #26
tame4tex commentedCould 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.
Comment #27
chr.fritschIs this still an issue?
Because for me it seems to work.
Comment #28
chrisla commentedPatch #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.
Comment #29
heddnYes, 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).
Comment #30
nicodh commentedPath in #17 works for me with Layout Paragraphs and select2 field in modal form
Comment #31
manuvelasco commentedMR 8 Working for a dialog modal bootstrap and drupal 10
Comment #32
fantonPatch #17 fixes the issue for me, on Drupal 10.2
Comment #33
mchu-ed commentedWe 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.
Comment #34
joe huggansTested 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.
Comment #35
vmarchukPatch #17 fixed the problem! Drupal 11.3 + Select2 2.0.0