function check_markup

Same name and namespace in other branches
  1. 10 core/modules/filter/filter.module \check_markup()
  2. 9 core/modules/filter/filter.module \check_markup()
  3. 8.9.x core/modules/filter/filter.module \check_markup()
  4. 7.x modules/filter/filter.module \check_markup()
  5. main core/modules/filter/filter.module \check_markup()

Runs all the enabled filters on a piece of text.

Note: Because filters can inject JavaScript or execute PHP code, security is vital here. When a user supplies a text format, you should validate it using $format->access() before accepting/using it. This is normally done in the validation stage of the Form API. You should for example never make a preview of content in a disallowed format.

Note: this function should only be used when filtering text for use elsewhere than on a rendered HTML page. If this is part of an HTML page, then a renderable array with a #type 'processed_text' element should be used instead of this, because that will allow cacheability metadata to be set and bubbled up and attachments to be associated (assets, placeholders, etc.). In other words: if you are presenting the filtered text in an HTML page, the only way this will be presented correctly, is by using the 'processed_text' element.

Parameters

string $text: The text to be filtered.

string|null $format_id: (optional) The machine name of the filter format to be used to filter the text. Defaults to the fallback format. See filter_fallback_format().

string $langcode: (optional) The language code of the text to be filtered, e.g. 'en' for English. This allows filters to be language-aware so language-specific text replacement can be implemented. Defaults to an empty string.

array $filter_types_to_skip: (optional) An array of filter types to skip, or an empty array (default) to skip no filter types. All of the format's filters will be applied, except for filters of the types that are marked to be skipped. FilterInterface::TYPE_HTML_RESTRICTOR is the only type that cannot be skipped.

Return value

\Drupal\Component\Render\MarkupInterface The filtered text.

Deprecated

in drupal:11.4.0 and is removed from drupal:13.0.0. There is no direct replacement. It's recommended to always return a renderable array without flattening as markup to pass the cacheability metadata.

See also

https://www.drupal.org/node/3588040

\Drupal\filter\Plugin\FilterInterface::process()

1 call to check_markup()
CheckMarkupDeprecationTest::testCheckMarkup in core/modules/filter/tests/src/Kernel/CheckMarkupDeprecationTest.php
Test deprecation of check_markup().

File

core/modules/filter/filter.module, line 217

Code

function check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = []) {
  @trigger_error(__FUNCTION__ . "() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no direct replacement. It's recommended to always return a renderable array without flattening as markup to pass the cacheability metadata. See https://www.drupal.org/node/3588040", E_USER_DEPRECATED);
  $build = [
    '#type' => 'processed_text',
    '#text' => $text,
    '#format' => $format_id,
    '#filter_types_to_skip' => $filter_types_to_skip,
    '#langcode' => $langcode,
  ];
  return \Drupal::service('renderer')->renderInIsolation($build);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.