Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When installed on a site using bootstrap theme the "other" field is always displayed. It should be displayed only when the "other" value is selected from the drop down.
Themes and modules can alter element markup by adding wrapper elements. This is valid for custom module development too.
<div class="form-item js-form-item form-type-select js-form-type-select form-item-field-favorite-color-0-select-other-list js-form-item-field-favorite-color-0-select-other-list form-group">
<label for="edit-field-favorite-color-0-select-other-list" class="control-label">Favorite Color</label>
<div class="select-wrapper"><select class="form-text form-select form-select-other-list form-select form-control" data-drupal-selector="edit-field-favorite-color-0-select-other-list" id="edit-field-favorite-color-0-select-other-list" name="field_favorite_color[0][select_other_list]"><option value="_none" selected="selected">- None -</option><option value="red">Red</option><option value="green">Green</option><option value="blue">Blue</option><option value="other">Other</option></select></div>
</div>
Proposed resolution
Figure out the input element based not on the immediate parent, but a specific selector instead.
var input_element = $(this).parent().next();
Comment | File | Size | Author |
---|---|---|---|
#6 | interdiff-3126148-4-6.txt | 2.23 KB | mradcliffe |
#6 | 3126148-6.patch | 2.42 KB | mradcliffe |
| |||
#4 | 3126148-4.patch | 1.58 KB | mradcliffe |
| |||
#2 | bootstrap-select-other-broken.png | 4.5 KB | mradcliffe |
Comments
Comment #2
mradcliffeThank you for reporting an issue, @ainsofs. It is much appreciated.
I think the most likely scenario is that there is a template preprocess or alter function that changes the behavior of either input or select elements. I took a spin on simplytest.me, and found this to be the case. There is an additional
div.select-wrapper
element. Currently the javascript in select other relies on core markup, but this probably should not be the case.Comment #3
ainsofs CreditAttribution: ainsofs commentedFYI I tested this with the experimental claro theme in 8.8.5 using simplytest and it has the same issue
Comment #4
mradcliffeThank you for the additional testing. That really helped me be able to test locally.
I think we can change to use
drupal-data-selector
, which should be pretty standard. I haven't tested this with bootstrap theme yet, but I'll try to do so.Comment #5
mradcliffeNo, this approach isn't going to work. Bootstrap changes the data-drupal-selector to the select list and the extra wrapper elements are still going to break it.
Comment #6
mradcliffeI think this should work using data-drupal-selector for both list and text input elements.
Also Drupal 8+ uses jQuery 3 so need to switch to using .on rather than .bind.
Comment #7
ainsofs CreditAttribution: ainsofs commentedthanks #6 worked for me using 8.x-1.4, core 8.7.11 and bootstrap 8.x-3.15
Comment #9
mradcliffeThank you for testing. I've committed this to 8.x-1.x.