Skip to content

TextFormat

Helper service for modifying text formats.


  • Full name: \Drupal\helper\TextFormat

Methods

addTag

Add a tag to the HTML restrictions for specific formats, or all.

public addTag(\Drupal\filter\FilterFormatInterface $format, string $tag, array $attributes = []): void

Parameters:

Parameter Type Description
$format \Drupal\filter\FilterFormatInterface
The filter format.
$tag string
The tag name to add, without enclosing braces. Example: span.
$attributes array
(Optional) An array of attributes to add to the tag.

removeTag

Remove a tag and all its attributes from the HTML restrictions.

public removeTag(\Drupal\filter\FilterFormatInterface $format, string $tag): void

Parameters:

Parameter Type Description
$format \Drupal\filter\FilterFormatInterface
The filter format.
$tag string
The tag name to remove, without enclosing braces. Example: span.

removeTagAttributes

Remove a tag's attributes from the HTML restrictions for formats.

public removeTagAttributes(\Drupal\filter\FilterFormatInterface $format, string $tag, array $attributes): void

Parameters:

Parameter Type Description
$format \Drupal\filter\FilterFormatInterface
The filter format.
$tag string
The tag name from which to remove attributes, without enclosing braces.
$attributes array
An array of attributes to remove from the tag.

buildTagRegex

Build a regex to extract a tag from the HTML restrictions string.

protected buildTagRegex(string $tag): string

Parameters:

Parameter Type Description
$tag string
The tag name to build the regex for, without enclosing braces.

buildReplacement

Build a replacement string for a tag and its attributes.

protected buildReplacement(string $tag, array $attributes = []): string

Parameters:

Parameter Type Description
$tag string
The tag name without enclosing braces. Example: span.
$attributes array
(Optional) An array of attributes to add to the tag.

isTagAllowed

Checks if a tag is allowed.

public isTagAllowed(\Drupal\filter\FilterFormatInterface $format, string $tag): bool

Parameters:

Parameter Type Description
$format \Drupal\filter\FilterFormatInterface
The text format.
$tag string
The HTML tag name.

Return Value:

TRUE if the tag is allowed by the text format, or FALSE otherwise.


isAttributeAllowed

Checks if a tag attribute is allowed.

public isAttributeAllowed(\Drupal\filter\FilterFormatInterface $format, string $tag, string $attribute): bool|null

Parameters:

Parameter Type Description
$format \Drupal\filter\FilterFormatInterface
The text format.
$tag string
The HTML tag name or '*' to check this global allowed attributes.
$attribute string
The optional attribute to check for.

Return Value:

A boolean if the tag is allowed or not. A NULL result is possible when checking the global allowed attributes and there is no match found.