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.

Boolean field with Single On/Off widget

You will then be presented with the field settings. You don't need to change anything here.

Single on/off field settings

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.

Use field label

And that is it! You now have a nice on/off checkbox.

Create check box field

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

Find out more


Comments

It is actually really easy to create a single on/off checkbox by using the boolean field which does offer a single on/off checkbox widget.

Why not use the Single on/off checkbox widget for the boolean field? If you tick the Use field label instead of the "On value" as label it lets you edit the field label without changing the values (eg: 1 or 0) stored in the database.

This is not the correct way to create single on/off checkbox. There is a field type for that, and it is definitely not the List (text) type.

You should add a field of type 'Boolean' and as widget 'Single on/off checkbox'.

Euhm, what about a boolean with a single on/off checkbox formatter?

Blair Wadman's picture

Thanks everyone for your comments! Somehow I managed to miss the Boolean field type. I'll update this article accordingly.

Annoyingly, no boolean field is available in Webform. There is a module to provide one, https://www.drupal.org/project/webform_boolean, but it has been left in dev, and the bugs do make it problematic for a production site.

Great. It worked. Thanks :)

Thanks for the notes. I have installed Drupal 8 by following the guide at https://www.rosehosting.com/blog/how-to-install-drupal-with-postgresql-on-a-debian-8-vps and will start building my site step by step. Your notes are welcomed. Thank you.

Add new comment