Filter

In Drupal, a text filter is one ordered step of a text format, such as Convert URLs into links, that rewrites stored text as it is rendered.

Learn DrupalDrupal glossary

A filter is a small piece of code that changes user-entered text when Drupal displays it. One filter removes HTML tags you have not approved, another turns URLs into clickable links, another adds captions to images. Filters are plugins that you switch on, order and configure inside a text format.

How filters work in Drupal 11

Core discovers filters as plugins in the Plugin\Filter namespace of a module. A class declares itself with the #[Filter] attribute, which requires an id, a title and a type. Most filters extend FilterBase and implement FilterInterface. Text passes through two steps. First, prepare() runs for every filter; the interface says it should be used for escaping only. Then process() runs and returns a FilterProcessResult, which can also carry assets, cacheability metadata and placeholders.

The type is one of four constants on FilterInterface: TYPE_MARKUP_LANGUAGE, TYPE_HTML_RESTRICTOR, TYPE_TRANSFORM_REVERSIBLE and TYPE_TRANSFORM_IRREVERSIBLE. A text format stores, for each filter, whether it is enabled, its weight and its settings. Filters run from top to bottom in that order.

Filters that ship with core

FilterWhat it does
Limit allowed HTML tags and correct faulty HTMLKeeps only the tags and attributes in its allowed list.
Correct faulty and chopped off HTMLRepairs broken or unclosed HTML.
Convert line breaks into HTMLTurns line breaks into <br> and <p> tags.
Convert URLs into linksMakes web addresses clickable.
Display any HTML as plain textShows tags as visible text instead of running them.
Restrict images to this siteReplaces images hosted elsewhere with a placeholder.
Align images, Caption imagesRead the data-align and data-caption attributes of an image.
Lazy load imagesAsks browsers to lazy load images that have dimensions.
Track images uploaded via a Text EditorKeeps the latest version and the dimensions of uploaded images.
Embed media (Media module)Replaces a <drupal-media> tag with the media item.
Entity links (Drupal 11.3 and later)Points links that carry data-entity-type and data-entity-uuid to the current URL alias.

Example

The Plain text format turns on three filters: display any HTML as plain text, convert URLs into links and convert line breaks. Type <b>Hello</b> https://example.com into a field that uses it and the visitor sees the tag as text, followed by a working link. The Restricted HTML format of the Standard profile instead limits HTML to a list that starts with <a href hreflang> <em> <strong>.

Common mistakes

  • Ignoring the order. The Drupal help gives the case of converting URLs into links before disallowed tags are removed: all links may then be removed.
  • Expecting a filter to fix saved content. It only changes the output, so the original text is still in the database.
  • Mixing it up with a Views filter. In a view, a filter limits the data the view outputs, for example to published content. It has nothing to do with text formats.
  • Letting untrusted roles use a format without the tag limit. The tag-limiting filter helps defend against XSS, but only in formats that enable it. The Full HTML format of the Standard profile does not.

For marketing and content teams

Filters explain surprises on the page: a pasted web address that became a link, an image that gained a caption, or a tag that vanished. Ask your developer which filters the format has before you assume a bug.

Sources

Questions about Filter

A filter does one job on text, such as converting URLs into links. A text format is the saved group of filters, in a set order, together with an optional editor and the roles that may use it. You enable filters inside a text format.

No. The Filter module help says filters do not change the stored content. They define how it is processed and displayed, so you can change a filter later and the output changes without editing any saved text.

Add a class in the src/Plugin/Filter folder of a custom module. Mark it with the #[Filter] attribute, extend FilterBase and implement process(). Then switch the filter on in a text format.

Related terms

All Drupal terms from A to Z