Drupal Advent Calendar Door 8: Chosen

By james, 8 December, 2022
Advent Calendar door 8 containing the Chosen module

Here we are for day 8 of the Drupal Advent Calendar. Yesterday I hinted that today’s module would make yesterday Address module better. It might have sounded like there was some sort of plan. Believe me, nothing could be further from the truth.

Case in point, trying today’s module in a sandbox, and things weren’t working as expected. I had almost convinced myself that it didn’t work with the Address module. I was almost ready to give up and pick a different module. Then I read the install instructions again, saw the step I had missed, repeated the install including the missing step, and it all worked perfectly. So make sure you read the instructions!

So what is the troublesome module?

Today’s module is Chosen. It is another of those modules that you install and it just makes things better.

Why is it useful? Well, sometimes website forms have selectable lists or drop-downs, and sometimes these lists can get kinda long. Case in point, yesterday’s Address field includes the country list, and it can be kind of fiddly to find a country in a list that long.

Sure you can press a key to take you to the right general area. That works on a computer with a keyboard, but on a phone you’re stuck with just scrolling a long way. 70% of website visitors are now using mobile devices, so this matters.

This is where Chosen comes in. By default, if any list control has more than 20 items, it will add a text box to make it searchable. The country list is a perfect example, but this also applies to US states, Irish counties, and region lists for many other countries.

Country drop-down with 'uni' entered in search box

And it doesn’t only do this for the Country drop-down. Any list control with more than the threshold number of items will have a search box added. This is great, because it means modules with long lists don’t need to add this logic themselves, they can rely on Chosen to do it for them. Without this, modules like Address would probably need to provide this functionality, causing unnecessary duplication, and probably inconsistent solutions.

There are a range of configurable options. In particular you can configure the number of items that the search box will be shown for. You can also configure some specifics about how the search works, for example whether to search in the middle of words, and you can configure it to apply to specific controls on your site. However, for many sites the default options will be sufficient.

The install process is a little fiddly. There are instructions, and if you follow them properly, it’s fairly easy to install. It does require the composer.json file to be edited, which might sound scary to newcomers to Drupal. However, hopefully a future release will simplify the process so it can all be done with a single Composer command.

And that’s day 8. Hope to see you for tomorrow’s door, but I’ve learned not to jinx things by dropping hints!

Advent Calendar with door 8 open, revealing Chosen module




Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.