Score:0

Create a view filter to find node which have no value in a specific field

ru flag

I'm trying to create an exposed filter for view which allow to find node which have a specific field with no value. It's a field which lists taxonomy terms.

This is not possible with view. When I select to exposed the operator "IS NULL", the filter which is return by view make no sense as you can see on the following images

enter image description here

The filter after I choose this option:

enter image description here

In another issue on stackexchange, someone suggests to use better exposed filter and some people approve. But maybe because the issue was for Drupal 7 or for another type of field, this option is not available with the module for my field.

I create a custom filter which display a checkbox in filter which modify the query to display contents which have this field empty if check.

With EntityQuery, I know it's possible to make a query which return entities with a field empty with the function notExist on the field.

But in the case of the query for view, I have no idea how to indicate I want content which have no connection on this field. I guess I can have this with a subselect in the query but I have no idea how to construct it.

Score:0
fr flag
  1. Go to the view
  2. Add filter for "Tags (field_tags)"
  3. Select "Is empty (NULL)" operator

Now only nodes with no tags will show

ru flag
It was unclear in my initial post but my filter is exposed in fact so it's sadly not so simple
hoanns avatar
fr flag
So can't you just expose the "Is empty (NULL)" operator? and let the user select it? Or am I not getting something
ru flag
No,that's show a textfield or the list of term which doesn't make sense for the UX.
Score:0
ru flag

So, I finally found out how to do it with a left join and verify if it is null or not depending on the value of the checkbox (if checked, should be filled; if not, should be empty). Here is my filter:

<?php

namespace Drupal\my_module\Plugin\views\filter;

use Drupal\Core\Database\Query\Condition;
use Drupal\views\Plugin\views\filter\BooleanOperator;
use Drupal\views\Views;

/**
 * Filters to detect if a content has taxonomy term informed in field_tags.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("has_tags_filter")
 */
class HasTagFilter extends BooleanOperator {

  /**
   * Helper function that builds the query.
   */
  public function query() {
    $configuration = [
      'table' => 'node__field_tags',
      'field' => 'entity_id',
      'left_table' => 'node_field_data',
      'left_field' => 'nid',
      'operator' => '=',
    ];
    $join = Views::pluginManager('join')->createInstance('standard', $configuration);
    $this->query->addRelationship('node__field_tags', $join, 'node_field_data');

    if ($this->value == 1) {
      $this->query->addWhere('AND', 'node__field_tags.field_tags_target_id', '', 'IS NOT NULL');
    } else {
      $this->query->addWhere('AND', 'node__field_tags.field_tags_target_id', '', 'IS NULL');
    }
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.