Use Taxonomy Terms as Webform Options in Drupal 8

Webform has a pretty robust system for managing lists of options. When you create a select box, you can define its options just for that element, or use a predefined list. If you go to Structure, Webforms, Configurations and click on Options, you can see all the predefined options and you can create your own.

If you want to learn how to create your own predefined options check out our tutorial; How to Use Webform Predefined Options in Drupal 8.

One thing to be aware of is that all of these options are stored as config files, which makes perfect sense, it is configuration.

But what if you want editors to manage the options?

Depending on how you deploy Drupal sites if you change an option only on the production site, your change will be overridden the next time you deploy to production because you import all new configuration changes.

To work around this, you could look at using Webform Config Ignore.

Another way of managing options is by using the Taxonomy system. An editor would simply manage all the terms from the Taxonomy page and nothing will be stored in config files.

In this tutorial, you’ll learn how to create a select element which uses a taxonomy vocabulary instead of the standard options.

Getting Started

I’m going to assume you know how to use Webform.

But if this is the first time you’ve heard of the module and want to learn more then check out these two tutorials:

    1. Getting Started with Webform in Drupal 8
    2. Moving Forward with Webform in Drupal 8

Create Taxonomy Vocabulary

The first thing you need to do is create a taxonomy vocabulary.

1. Go to Structure, Taxonomy and click on “Add vocabulary”.

2. Add Software into Name and click on Save.

3. Click on “Add term” and make a bunch of terms.

Now that we have a taxonomy vocabulary, let’s create an element.

Create Term Select Element

Once you’ve created a taxonomy vocabulary, go and edit or create a new webform.

1. Click on “Add element”.

2. Filter by term and click on “Add element” on the Term select row.

3. Enter Software as the name and make sure you select Software from the Vocabulary drop-down.

4. Now if you go to the form you should see a drop-down with the terms as the options.

Other Entity Reference Elements

If you look in the “Entity reference elements” when adding an element, you’ll notice a bunch of other types of elements, i.e., Entity select, Entity radios, etc…

You can use any of the other elements to reference a taxonomy term or any type of entity. The difference between the “Term select” and “Entity select”, is that the “Entity select” gives you more control on how entities are referenced.

For example, you can change the reference method and the sort.

If you want complete control over these options then it’s best to use the “Entity select” instead of “Term select”.

Summary

If you want to give your editors the ability to manage options in webform elements then it’s best to use the Taxonomy system to manage the option, then use an entity reference element to display them.

This way you won’t have to try and ignore configure files which can be tricky to maintain.

Ivan Zugec

About Ivan Zugec

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.