function 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 filter_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.

Parameters

$text: The text to be filtered.

$format_id: The format ID of the text to be filtered. If no format is assigned, the fallback format will be used.

$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.

$cache: Boolean whether to cache the filtered output in the {cache_filter} table. The caller may set this to FALSE when the output is already cached elsewhere to avoid duplicate cache lookups and storage.

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. FILTER_TYPE_HTML_RESTRICTOR is the only type that cannot be skipped.

Return value

The filtered text.

Related topics

20 calls to check_markup()
AreaTextTest::testAreaText in drupal/core/modules/views/lib/Drupal/views/Tests/Handler/AreaTextTest.php
BlockTest::testCustomBlock in drupal/core/modules/block/lib/Drupal/block/Tests/BlockTest.php
Test creating custom block, moving it to a specific region and then deleting it.
block_block_view in drupal/core/modules/block/block.module
Implements hook_block_view().
BookTest::checkBookNode in drupal/core/modules/book/lib/Drupal/book/Tests/BookTest.php
Check the outline of sub-pages; previous, up, and next; and printer friendly version.
BookTest::testBookExport in drupal/core/modules/book/lib/Drupal/book/Tests/BookTest.php
Tests book export ("printer-friendly version") functionality.

... See full list

File

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

Code

function check_markup($text, $format_id = NULL, $langcode = '', $cache = FALSE, $filter_types_to_skip = array()) {
  if (!isset($format_id)) {
    $format_id = filter_fallback_format();
  }

  // If the requested text format does not exist, the text cannot be filtered.
  if (!($format = filter_format_load($format_id))) {
    watchdog('filter', 'Missing text format: %format.', array(
      '%format' => $format_id,
    ), WATCHDOG_ALERT);
    return '';
  }

  // Prevent FILTER_TYPE_HTML_RESTRICTOR from being skipped.
  if (in_array(FILTER_TYPE_HTML_RESTRICTOR, $filter_types_to_skip)) {
    $filter_types_to_skip = array_diff($filter_types_to_skip, array(
      FILTER_TYPE_HTML_RESTRICTOR,
    ));
  }

  // When certain filters should be skipped, don't perform caching.
  if ($filter_types_to_skip) {
    $cache = FALSE;
  }

  // Check for a cached version of this piece of text.
  $cache = $cache && !empty($format->cache);
  $cache_id = '';
  if ($cache) {
    $cache_id = $format->format . ':' . $langcode . ':' . hash('sha256', $text);
    if ($cached = cache('filter')
      ->get($cache_id)) {
      return $cached->data;
    }
  }

  // Convert all Windows and Mac newlines to a single newline, so filters only
  // need to deal with one possibility.
  $text = str_replace(array(
    "\r\n",
    "\r",
  ), "\n", $text);

  // Get a complete list of filters, ordered properly.
  $filters = filter_list_format($format->format);
  $filter_info = filter_get_filters();

  // Give filters the chance to escape HTML-like data such as code or formulas.
  foreach ($filters as $name => $filter) {

    // If necessary, skip filters of a certain type.
    if (in_array($filter_info[$name]['type'], $filter_types_to_skip)) {
      continue;
    }
    if ($filter->status && isset($filter_info[$name]['prepare callback'])) {
      $function = $filter_info[$name]['prepare callback'];
      $text = $function($text, $filter, $format, $langcode, $cache, $cache_id);
    }
  }

  // Perform filtering.
  foreach ($filters as $name => $filter) {

    // If necessary, skip filters of a certain type.
    if (in_array($filter_info[$name]['type'], $filter_types_to_skip)) {
      continue;
    }
    if ($filter->status && isset($filter_info[$name]['process callback'])) {
      $function = $filter_info[$name]['process callback'];
      $text = $function($text, $filter, $format, $langcode, $cache, $cache_id);
    }
  }

  // Cache the filtered text. This cache is infinitely valid. It becomes
  // obsolete when $text changes (which leads to a new $cache_id). It is
  // automatically flushed when the text format is updated.
  // @see filter_format_save()
  if ($cache) {
    cache('filter')
      ->set($cache_id, $text, CacheBackendInterface::CACHE_PERMANENT, array(
      'filter_format' => $format->format,
    ));
  }
  return $text;
}