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?
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 :)