How do you create a single on/off checkbox field in Drupal?
Simple on/off checkbox fields where you want the user to select or deselect a checkbox are a common requirement. Yet it is not so obvious how to create them in Drupal.
On/off checkbox fields are often used to toggle for things like:
- Accepting terms and conditions
- Accepting privacy notices
- Marking an event as cancelled
- Flagging a node or user
To create a check box, you first need to add a field with field type of Boolean. Set the widget to Single on/off checkbox.
You will then be presented with the field settings. You don't need to change anything here.
On the next screen, select the option to Use field label instead of the "On value" as label. This will ensure that the label for the field is displayed with the field rather than the number 1.
And that is it! You now have a nice on/off checkbox.
UPDATED 6 Jan: Thanks to everyone who pointed out the boolean type and single on/off widget this out in the comments!
If you liked this, you'll love my book, Master Drupal Module Development.
"..the must have drupal developers book"
Feeling stuck with Drupal 8 module dev?
Get the free 7 lesson course that will help you get started today without feeling overwhelmed.
- Create Drupal modules with just a few commands using the Drupal Console
- Create custom pages
- Create custom blocks
- Create admin forms
- Demystify routers and controllers
- Bonus material