0

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.

2 Answers 2

0
  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

3
  • It was unclear in my initial post but my filter is exposed in fact so it's sadly not so simple
    – Claire D
    Aug 16, 2021 at 15:20
  • So can't you just expose the "Is empty (NULL)" operator? and let the user select it? Or am I not getting something
    – hoanns
    Aug 16, 2021 at 15:59
  • No,that's show a textfield or the list of term which doesn't make sense for the UX.
    – Claire D
    Aug 16, 2021 at 21:07
0

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');
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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