9

I want to created a custom views field, following Creating a custom Views field in Drupal 8. This custom field counts and returns the number of referencing entities; if the current view row item is referenced by 10 other entities, the field shows 10.

I want to create a custom exposed filter for this field's output, which is a number between 0 and 100. The filter should allow to show all items that are referenced more than 5 times, for example.

So it would be nice if it is possible to get the int filter options (Equal to, Not equal to, Higher than).

That tutorial also shows a link on creating your own filter; content from this link is used in the duplicate answer that is mentioned. In this custom filter they work with existing fields. Where I want the filter to work with the same calculation (database query) I use in the custom field.

What I got so far is in the .views.inc file.

/* @    -- Total score number/percentage */
$data['tests']['score_percentage'] = array(
  'title' => t('Percentage Filter'),
  'filter' => array(
    'title' => t('Percentage Filter'),
    'help' => t('Provides a custom filter for tests to filter by total score percentage'),
    'id' => 'score_percentage'
  ),
);

The tests type is a custom entity type I created with the ECK Entity Types module.

In the src/Plugin/views/filter folder, I have a ScorePercentage.php file with the following code.

namespace Drupal\score_percentage\Plugin\views\filter;

use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\views\ViewExecutable;

/**
 * Filters by given list of node title options.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("score_percentage")
 */
class ScorePercentage extends InOperator {

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->valueTitle = t('Percentage Filter');
  }

  /**
   * Override the query so that no filtering takes place if the user doesn't
   * select any options.
   */
  public function query() {
    if (!empty($this->value)) {
      parent::query();
    }
  }

  /**
   * Skip validation if no options have been chosen so we can use it as a
   * non-filter.
   */
  public function validate() {
    if (!empty($this->value)) {
      parent::validate();
    }
  }
}

Can someone help me creating this exposed filter?

10
  • 1
    Possible duplicate of How to add a custom views filter handler for a specific field?
    – No Sssweat
    Jan 31, 2019 at 2:05
  • 1
    Not a duplicate because they are creating a filter based on a existing field. I would like my filter to be based on a custom views field. The answer in the other thread is based on the article I'm mentioning in my question.
    – Joost
    Jan 31, 2019 at 7:39
  • I'm not sure exactly what you're trying to accomplish here. If you want numeric options then make sure the field is an integer?
    – mradcliffe
    Jan 31, 2019 at 22:04
  • I edited my question hopefully it is more clear what I am looking for.
    – Joost
    Feb 5, 2019 at 9:11
  • 1
    Things you might do is leave public function query() blank, or do a table join in the query function. But if you leave it as is and the field does not exist in the database you will get a "column not found error". Wish I knew more :) Jul 7, 2019 at 14:58

0

Browse other questions tagged or ask your own question.