9.14.1. Extending custom Drupal Views filter handler
In the previous article we had a look how to integrate Views modules with custom database for Did this help? module:
https://www.drupal.org/project/did_this_help
In this articles we will override Views filter for Choice (yes/no) field:
Now it's usual text field created by "string" handler, which allows to add search key. But we have only two options Yes/No, so it would be better to have dropbox instead of textfield. Let's create a file /did_this_help/src/Plugin/views/filter/DidThisHelp.php:
<?php namespace Drupal\did_this_help\Plugin\views\filter; use Drupal\views\Plugin\views\filter\InOperator; /** * Filters by given list of yes/no options. * * @ingroup views_filter_handlers * * @ViewsFilter("did_this_help") */ class DidThisHelp extends InOperator { }
We will inherit DidThisHelp class from parent InOperator class, which provides filter with select dropbox, radio buttons or checkboxes depends on Views UI settings.
In method getValueOptions() we will add list of available options:
/** * {@inheritdoc} */ public function getValueOptions() { if (isset($this->valueOptions)) { return $this->valueOptions; } $this->valueOptions = [ 'yes' => $this->t('Yes'), 'no' => $this->t('No'), ]; return $this->valueOptions; }
Property valueOptions has been defined in parent class InOperator, so we need to define only array of options.
In results we got that filter:
If we enabled exposed filter, then we see this dropbox:
You can override any Filter class from this list of classes provided by Views and other modules:
Name | Location | Description |
---|---|---|
Access | core/ |
Filter by node_access records. |
BooleanOperator | core/ |
Simple filter to handle matching of boolean values |
BooleanOperatorString | core/ |
Simple filter to handle matching of boolean values. |
Broken | core/ |
A special handler to take the place of missing or broken handlers. |
Bundle | core/ |
Filter class which allows filtering by entity bundles. |
Current | core/ |
Filter handler for the current user. |
Date | core/ |
Date/time views filter. |
Date | core/ |
Filter to handle dates stored as a timestamp. |
Equality | core/ |
Simple filter to handle equal to / not equal to filters |
FilterBooleanOperatorDefaultTest | core/ |
Filter to test queryOpBoolean() with default operator. |
FilterPluginBase | core/ |
Base class for Views filters handler plugins. |
GroupByNumeric | core/ |
Simple filter to handle greater than/less than filters |
HistoryUserTimestamp | core/ |
Filter for new content. |
InOperator | core/ |
Simple filter to handle matching of multiple options selectable via checkboxes |
LanguageFilter | core/ |
Provides filtering by language. |
LatestRevision | core/ |
Filter to show only the latest revision of an entity. |
ListField | core/ |
Filter handler which uses list-fields as options. |
ManyToOne | core/ |
Complex filter to handle filtering for many to one relationships, such as terms (many terms per node) or roles (many roles per user). |
ModerationStateFilter | core/ |
Provides a filter for the moderation state of an entity. |
Name | core/ |
Filter handler for usernames. |
NodeComment | core/ |
Filter based on comment node status. |
NumericFilter | core/ |
Simple filter to handle greater than/less than filters |
Permissions | core/ |
Filter handler for user roles. |
Roles | core/ |
Filter handler for user roles. |
Search | core/ |
Filter handler for search keywords. |
Standard | core/ |
Default implementation of the base filter plugin. |
StatisticsLastUpdated | core/ |
Filter handler for the newer of last comment / node updated. |
Status | core/ |
Filter by file status. |
Status | core/ |
Filter by published status. |
StringFilter | core/ |
Basic textfield filter to handle string filtering commands including equality, like, not like, etc. |
TaxonomyIndexTid | core/ |
Filter by term id. |
TaxonomyIndexTidDepth | core/ |
Filter handler for taxonomy terms with depth. |
UidRevision | core/ |
Filter handler to check for revisions a certain user has created. |
UserUid | core/ |
Filter handler to accept a user id to check for nodes that user posted or commented on. |
UserUid | core/ |
UID filter to check for nodes that a user posted or commented on. |
ViewsFilter | core/ |
Defines a Plugin annotation object for views filter handlers. |