Extend Views with handlers
Views builds listing pages as configuration: a query plus display assembled in the admin UI. A Views handler operates on one column of data: a field displays it, a filter narrows the result set by it, a sort orders by it, an argument accepts it as a contextual filter. This guide adds a custom field handler that renders node titles in uppercase; the other handler types follow the same shape.
Make a computed or specially formatted value available in the Views UI, so site builders can add it to any view like a built-in field. If you only need to reformat an existing field’s value for display, a field formatter may be enough; reach for a handler when the logic belongs to Views (querying, filtering, sorting).
Prerequisites
Section titled “Prerequisites”- A custom module, per Scaffold a module.
- The core Views module (installed by the standard profile).
- Plugins, since handlers are plugins, and object-oriented hooks for the registration hook.
Write the field handler
Section titled “Write the field handler”A field handler extends FieldPluginBase, which already adds the column to the query. You override render() to format the value. Put it in src/Plugin/views/field/ and give it an ID with the #[ViewsField] attribute.
<?php
declare(strict_types=1);
namespace Drupal\howto_views\Plugin\views\field;
use Drupal\views\Attribute\ViewsField;use Drupal\views\Plugin\views\field\FieldPluginBase;use Drupal\views\ResultRow;
/** * Renders a node title in uppercase. * * A field handler maps a column into a view and turns its raw value into * output. The base class adds the column to the query; you override * render() to format it. */#[ViewsField('howto_uppercase')]final class UppercaseTitle extends FieldPluginBase {
/** * {@inheritdoc} */ public function render(ResultRow $values): string { return mb_strtoupper((string) $this->getValue($values)); }
}getValue() returns the raw value of the column the handler is bound to; render() returns what appears in the cell.
Register it in Views data
Section titled “Register it in Views data”A handler is invisible until Views data advertises it. Implement hook_views_data_alter() as an object-oriented hook to attach the field to a table, naming the handler’s plugin ID and the real column it reads.
/** * Registers the custom field handler against the node title column. */#[Hook('views_data_alter')]public function viewsDataAlter(array &$data): void { $data['node_field_data']['howto_uppercase_title'] = [ 'title' => $this->t('Title (uppercase)'), 'help' => $this->t('The node title rendered in uppercase.'), 'field' => [ 'id' => 'howto_uppercase', // The real column the handler reads. 'real field' => 'title', ], ];}After a cache rebuild (drush cr), “Title (uppercase)” appears in the field list for any view on content.
Verify
Section titled “Verify”The example module ships a view that uses the handler. After installing it:
ddev drush pm:install howto_viewsddev drush php:eval " \$v = \Drupal\views\Views::getView('howto_uppercase_titles'); \$v->execute(); echo \$v->field['howto_uppercase_title']->render(\$v->result[0]);"prints the first node’s title in uppercase. The module’s kernel test asserts the same end to end.
Variations
Section titled “Variations”Filter, sort, and argument handlers
Section titled “Filter, sort, and argument handlers”Each handler family has its own base class and directory, and registers under its own key in Views data:
| Handler | Base class | Directory | Views-data key |
|---|---|---|---|
| Field | FieldPluginBase | src/Plugin/views/field | field |
| Filter | FilterPluginBase | src/Plugin/views/filter | filter |
| Sort | SortPluginBase | src/Plugin/views/sort | sort |
| Argument | ArgumentPluginBase | src/Plugin/views/argument | argument |
A filter overrides query() to add a WHERE condition; a sort adds ORDER BY; an argument turns a URL segment into a condition.
Related
Section titled “Related”- Extend Views with plugins for styles, rows, and other display-level plugins.
- Plugins for the discovery system handlers use.
- The
Views field handlers topicon api.drupal.org.
Was this page helpful?
Drupal is a registered trademark of Dries Buytaert.