check_markup

check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = array())

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 a 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 a 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

filter_process_text()

Related topics

Sanitization functions
Functions to sanitize values.

File

core/modules/filter/filter.module, line 294
Framework for handling the filtering of content.

Code

function check_markup($text, $format_id = NULL, $langcode = '', $filter_types_to_skip = array()) {
  $build = array(
    '#type' => 'processed_text',
    '#text' => $text,
    '#format' => $format_id,
    '#filter_types_to_skip' => $filter_types_to_skip,
    '#langcode' => $langcode,
  );
  return \Drupal::service('renderer')->renderPlain($build);
}
doc_Drupal
2016-10-29 08:49:59
Comments
Leave a Comment

Please login to continue.