We have a Drupal 8 site. For one of our features, I added a boolean field called 'Pinned' to a our Blog Post content type.

As I added the filed as a filter criteria to views, I noticed that, no matter what configuration I selected, Is equal to + True, Is equal to + False, Is not equal to + True, Is not equal to + False, my view preview, which had had results, suddenly gave no results. I thought surely one of these combinations must apply to at least one node.

I added the 'Pinned' field to the fields of the view to examine the values, and removed it from the filter criteria. It turns out that the field was technically NULL, from the join-- there was no record in node__field_pinned.field_pinned_value.

To further test, I edited one of our blog posts nodes. When I checked the 'Pinned' checkbox, the value in the view became 1. When I then unchecked the checkbox and saved the node, then value became 0.

Trying to think of ways to auto-populate the field, I made the 'Pinned' field required.

enter image description here

But, all that did was make it necessary to check the checkbox when editing or adding a Blog Post.

enter image description here

This didn't solve my problem, and seemed to make the field redundant anyway. You can't save the node without checking the checkbox; you have to check it.

Edit going off Grapple's answer, I found that if I changed the widget to "Check boxes/radio buttons" on the "Manage form display" page of the content type. This does allow you to set a default value, but it does not auto-populate existing node field values, nor does it auto-populate the form widget for existing nodes.

enter image description here

It only auto-populates the form for new nodes.

enter image description here

It seems like a boolean field with a checkbox widget is meant for situations where the user cannot advance without checking the box, such as accepting terms of service, etc.

As it is, it is difficult to use, because we have hundreds of Blog Posts, and it's pretty labor intensive to edit each one and check, then uncheck the box. We only want a few pinned posts, but we can't have views of unpinned posts being empty.

Is there a way I can add a Boolean field to a content type with existing nodes and have it auto-populate with values?

  • Is checking and unchecking the 'Pinned' field actually necessary, or does just saving the node without any changes set the field value in the database to 0? – gapple Aug 7 '19 at 23:29
  • @gapple in my test, saving the node without having made any changes does not populate the field. – user1359 Aug 8 '19 at 14:54

If the field has a default value configured, you may be able to cause the "Pinned" field to be populated by bulk editing the nodes from the content overview page, and toggling a different value like 'Published', 'Sticky', or 'Promoted to front page'.

For a programmatic solution, implement hook_post_update_NAME to load the necessary entities and update the new field with the required default value.

| improve this answer | |
  • I would note to any readers, gapple's first suggestion, that the value of a new Boolean field can be populated by saving the form, does not work, in my testing. – user1359 Aug 8 '19 at 19:09

Because of the problems with no default value, we decided to re-utilize the 'sticky' field on our content type. A default value of '0' already exists for that field.

If you are in a situation where you can't use that, look at hook_post_update_NAME as gapple answers.

Edit We just learned, the hard way (bug report), that the use of the sticky fields requires the "Administer Content" permission, which is an administrative permission (Drupal lists a warning message next to it.)

In our case, I don't want all of our blog editors to have this permission, so this is not a permanent solution for us.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.