Select List Theming in Drupal 8
Styling native form elements has always been a battle against browser inconsistencies (and let's not even mention the nightmares of old Internet Explorer). In this guide, we bypass the limitations of standard CSS appearance: none and implement Select2—a powerful library that turns boring dropdowns into searchable, customizable components. We cover the full Drupal 8 integration process, from registering the library in libraries.yml to writing the custom Drupal.behaviors JavaScript needed to make it robust for mobile and touch devices.
Theming of form items and then performance issues is one of the hard jobs. I am not mentioning IE here because Dear internet Explorer has crosses all limits of giving pain. Now the happy news is that IE is dead and I am sure it will be burning in the deepest hole of hell. The pain, the time waste that IE has done in past years will not be fixed by Microsoft ever. Even if they sell their shares and pay all money to Front-End Developers.
I think I am going far with this now, back on the track. There are many ways to theme form items. Browsers and Operating systems render form items differently. Which is a reason for design inconsistencies across platforms. Depending on the amount of theming you have to do for form items and select boxes particularly.
Starting with the below code in your style sheets will give you jump start and idea what of what else to achieve. Its important to cascade the default appearance.
appearance: none;
-vendor_prefix-appearance: none;What if we have to select multiple options from drop-down or we have a very long drop-down list and (For example country names) and scrolling through is time consuming. What if we have a search inside the select list?
All this is possible with lot of libraries available but here we are focusing on one of the best libraries select2. Select2 gives us a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.
Particularly theming the select drop-down, which isn’t easy. border, drop-down arrows option list cant be themed easily without certain JS. We will unleash select2 for these things. It isn’t recomended to use Select2 for small customizations. If you want maximum out of it then it can be used otherwise there will be some small performance issues.
Its simple to use select2 in any site but with drupal there comes small customizations.
Firstly we will install select2 library and declare in our theme.libraries.yml
js:
libs/select2.min.js: {}
in my case I have put all libraries in libs folder. Now we need to do some little customizations to make it work on our drupal site. Actually it is mainly a class and wrapper issues that select2 isn’t getting applied with just library setup.
in our common JS file, we need to add some customised code. For me it is.
(function ($, Drupal, window, document) {
'use strict';
Drupal.behaviors.SelectCases = {
attach: function (context, settings) {
$(window, context).load(function () {
// Changing our default select lists
$('#content select.form-select').each(function (i) {
var $selectIndex = $('select-' + (i + 1));
$(this)
.select2({
width: 'style',
shouldFocusInput: function (instance) {
// Attempt to detect touch devices
var supportsTouchEvents = (('ontouchstart' in window) ||
(navigator.msMaxTouchPoints > 0));
// Only devices which support touch events should be special cased
if (!supportsTouchEvents) {
return false;
}
return true;
}
});
$('.select2', context)
.once()
.addClass('form-select').addClass('select-id-' + (i + 1));
});
}
};
})(jQuery, Drupal, this);
Now that new markup is generated and you can easily theme it, because it will be skinned in divs rather than .
Go ahead and theme as per your comps.
- The Native Trap: Native select boxes render differently on every OS and browser. Trying to style the
<option>tag is often a dead end. - Why Select2? It solves three major problems:
- Consistency: It looks the same on all platforms.
- UX: Adds search functionality for long lists (like countries).
- Styling: It converts the UI into standard
<div>elements that are easy to style with CSS.
- Drupal Integration: You can't just drop the script in. You must register it in
theme.libraries.ymland attach it viaDrupal.behaviorsto ensure it works with Drupal's caching and AJAX loading. - Mobile Optimization: The provided code snippet includes a crucial
shouldFocusInputcheck to prevent usability issues on touch devices—a detail often overlooked in standard tutorials.