function check_markup

Same name in other branches
  1. 7.x modules/filter/filter.module \check_markup()
  2. 8.9.x core/modules/filter/filter.module \check_markup()
  3. 10 core/modules/filter/filter.module \check_markup()
  4. 11.x 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.

See also

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

8 calls to check_markup()
AreaTextTest::testAreaText in core/modules/views/tests/src/Kernel/Handler/AreaTextTest.php
FilterAPITest::testCheckMarkupFilterOrder in core/modules/filter/tests/src/Kernel/FilterAPITest.php
Tests that the filter order is respected.
FilterAPITest::testCheckMarkupFilterSubset in core/modules/filter/tests/src/Kernel/FilterAPITest.php
Tests the ability to apply only a subset of filters.
FilterNoFormatTest::testCheckMarkupNoFormat in core/modules/filter/tests/src/Kernel/FilterNoFormatTest.php
Tests text without format.
FilterSecurityTest::testSkipSecurityFilters in core/modules/filter/tests/src/Functional/FilterSecurityTest.php
Tests that security filters are enforced even when marked to be skipped.

... See full list

File

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

Code

function check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = []) {
    $build = [
        '#type' => 'processed_text',
        '#text' => $text,
        '#format' => $format_id,
        '#filter_types_to_skip' => $filter_types_to_skip,
        '#langcode' => $langcode,
    ];
    return \Drupal::service('renderer')->renderPlain($build);
}

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