Reputation: 133
Drupal 9 Have content type Article. Content type Article has a field, called "Need Further Review", of boolean type. This field is not required, default value FALSE. The articles has value TRUE, FALSE, NULL for this field. TRUE: select true FALSE: select false NULL: the previous articles still have no value for this field.
Have a view to list articles. A filter criteria is this field "Need Further Review", and is exposed. It has options: Any, TRUE, FALSE. When users select TRUE, it will list articles with TRUE value of this field. When users select FALSE, it usually lists articles with FALSE value of this field. Now want to list articles with FALSE and NULL value of this field.
Drupal 9 view query alter https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_query_alter/9.0.x Use hook_views_query_alter
Upvotes: 1
Views: 1225
Reputation: 1085
At the moment you create an Article, the field gets a value, either TRUE or FALSE (default). So your view only needs to have an exposed filter for these values.
It is only when you do a migration without setting the field, you will have no value. Or when you add this field later, when there are already Articles made. Assuming this is the case, one solution is to set the field on those Articles to FALSE manually through the UI.
When there are too many Articles, and if you are able and willing to do some manual queries in the database, you may add the rows in the database for those Articles with a script, something like this (change to fit your situation, and, of course, make a backup first ;):
<?php
$link = mysqli_connect("localhost", "USERNAME", "PASSW", "DATABASENAME");
if($link) {
// get node id's
$nids = array();
$q_nids = mysqli_query($link, "SELECT `nid`,`type` FROM `node` WHERE `type` = 'article' ORDER BY `nid`");
$n_nids = mysqli_num_rows($q_nids);
if($n_nids!=0) {
while($l_nids = mysqli_fetch_assoc($q_nids)) {
foreach($l_nids as $key => $value) {
// insert missing rows
mysqli_query($link, "
INSERT INTO `TABLENAME`.`field_need_further_review`
(`bundle`,`deleted`,`entity_id`,`revision_id`,`langcode`,`delta`,`field_need_further_review_value`)
VALUES ('".$l_nids['type']."','0','".$l_nids['nid']."','0','nl','0','0');
");
}
}
}
}
Upvotes: 0