Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughHow to Use Webform Predefined Options in Drupal 8
Webform allows you to create powerful forms in Drupal without writing any custom code. One feature I want to show you today is predefined options.
If this is the first time you’ve heard of the module and want to learn more check out our two part series on using Webform.
Predefined options ease the creation of forms by offering common lists such as days, months, time zones, titles, etc…
For example, if you want to add a select list where users choose a country, instead of manually entering in all countries yourself, use the predefined one that comes with the module.
Webform comes with around 30 predefined lists which can be added to radio buttons, checkboxes, select list and menus. You can also create your own.
If you have a website that will use the same set of options on multiple forms, look at creating a predefined options list to save time.
In this tutorial, you’ll learn how to create and use predefined options.
Getting Started
This is part of the Webform module, so I’m going to assume you have it installed.
If you’ve never installed it check out our “Getting Started with Webform in Drupal 8” tutorial.
Use a Predefined Option
Let’s now look at using one of the predefined options in a select element. Let’s assume you need to create a select element with days as the options, Monday, Tuesday, etc…
1. From the Webforms page, go to the Build page of any form by clicking on Build.
2. Then click on “Add element”, search for “select” and click on “Add element” on the Select row.
3. Enter in a title for the element, you could call it Day.
4. From the Options drop-down, select Days and then Save.
5. If you view the form, you should see a drop-down called Day with days as the options.
You just saved yourself the effort of manually filling out the days.
Manage Predefined Options
To manage all the predefined options go to Structure, Webforms, Configuration and then click on Options.
From this page, you can view all the options create custom options and modify existing ones.
Let’s now modify the Days options so that Monday is the first day.
1. Click on Edit on the Days row.
2. Reorder the options so that Sunday is at the bottom then click on Save.
3. Now if you view the form, Monday should be the first option.
Take note, if you’re using the same predefined option on multiple elements and change the options (like we just did), then the change will appear on all elements.
Create Predefined Options
Creating your own predefined options is very easy.
1. While on the Options page, click on “Add options”.
2. Give your options a label and some values, then click on Save at the bottom of the page.
3. Select your predefined options from the Options drop-down on a select or checkbox element and you’re done.
Summary
The editors and marketers who use Webform to create custom forms will love this functionality. Out-of-the-box it comes with a bunch of common options such as days, months, “time zones”, “country codes” and more. On top of that you can create your own custom options which’ll save you a lot of time in the long run.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web