Advertising sustains the DA. Ads are hidden for members. Join today

Data filters

Last updated on
23 September 2022

Data filters are plugins for the Typed Data module that may be used within a token to manipulate the token value for display. The syntax is:
{{ variable | filter_name }}
where 'filter_name' refers to the filter plugin ID. More than one filter may be used, with the output of each filter passed as input to the next. Filters may also have arguments; e.g.,
{{ variable | filter1 | filter2('argument1', 'argument') }}

As an example of what we can do with filters, let's see how to use the 'default' filter to provide a default value for a variable in a token. If we wanted to apply the 'default' filter to a node title we would write {{ node.title | default('Default title') }}. Note if the filter takes arguments, like the 'default' filter used in this example, then the arguments must appear within parentheses. If the filter doesn't take arguments, the parentheses aren't used. The 'striptags' filter, for instance, doesn't take arguments, so to apply 'striptags' to the node title we would write {{ node.title | striptags }}

Data filters are plugins for the Typed Data API enhancements module that manipulate the token value for display. Data filters currently provided with that module are:

count
Returns the integer length of a string (this is a multibyte-safe filter!) This filter has no arguments.
default
Provides a default value for use if the token is not set. This filter takes one argument - the value to use.
entity_url
Provides the URL of an entity. This filter has no arguments.
format_date
Formats a timestamp value into a readable date - takes the same arguments as the core Drupal DateFormatter::format() function.
lower
Transforms string value into lower case. This filter has no arguments.
replace
Substitutes the text given in the first argument with the text given in the second argument. This filter requires two arguments.
striptags
Strips SGML/XML tags and replaces adjacent whitespace by one space. This filter has no arguments.
trim
Strips whitespace from the beginning and end of a string variable. This filter has no arguments.
upper
Transforms string value into upper case. This filter has no arguments.

The Rules Essentials module adds two new data filters which operate on string tokens:

link
Operates on 'uri' data and outputs an HTML anchor element. This filter takes one argument - the link text.
raw
Marks a string token as already sanitized, thus exempting it from HTML-encoding. This filter has no arguments.

Other modules may provide their own data filter plugins. Likewise, you may write your own data filter plugins if you need additional functionality. See Writing your own data filter plugin for details about how to do this.

Help improve this page

Page status: No known problems

You can: