0

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?

2
  • 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
1

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.

2
  • 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
  • It's hook_deploy_NAME now for content updates during drush deploy which then calls this hook after config import when the new field already is created and can be populated. – leymannx May 26 at 20:32
0

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.

0

I faced the same issue: created a new boolean field in an existing content and needed to use it as a filter criteria in a View.

My solution involved implementing hook_views_query_alter() to manually add the JOIN and a WHERE clause to address nodes that had not yet received values for my new boolean field.

Important to note that, in order for this solution to work, I didn't add the filter criteria in the View, otherwise Drupal would have added the JOIN and a WHERE clause that would cause no results to be returned.

I used a custom module for this. This is the contents of my_module.module:

<?php

use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;

/**
 * Implements hook_views_query_alter().
 */
function my_module_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
    // Only applies to 'my_view', for two REST Export displays
    if ($view->id() == 'my_view' && in_array($view->getDisplay()->display['id'], ['rest_export_1', 'rest_export_2'])) {
        // Add JOIN to main view query
        // LEFT JOIN node__field_hide_feed ON node_field_data.nid = node__field_hide_feed.entity_id AND node__field_hide_feed.deleted = 0
        $join = Drupal::service('plugin.manager.views.join')->createInstance('standard', [
            'type' => 'LEFT',
            'table' => 'node__field_hide_feed',
            'field' => 'entity_id',
            'left_table' => 'node_field_data',
            'left_field' => 'nid',
            'extra' => [
                [
                    'field' => 'deleted',
                    'value' => 0,
                ],
            ],
        ]);
        $query->addRelationship('node__field_hide_feed', $join, 'node_field_data');

        // Add WHERE clause
        // AND (node__field_hide_feed.field_hide_feed_value IS NULL OR node__field_hide_feed.field_hide_feed_value = 0)
        $query->where[] = [
            'conditions' => [
                [
                    'field' => 'node__field_hide_feed.field_hide_feed_value',
                    'value' => NULL,
                    'operator' => 'IS NULL',
                ],
                [
                    'field' => 'node__field_hide_feed.field_hide_feed_value',
                    'value' => 0,
                    'operator' => '=',
                ],
            ],
            'args' => [],
            'type' => 'OR',
        ];
    }
}

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.