You are here

public function CollapseText::prepare in Collapse Text 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Filter/CollapseText.php \Drupal\collapse_text\Plugin\Filter\CollapseText::prepare()

Prepares the text for processing.

Filters should not use the prepare method for anything other than escaping, because that would short-circuit the control the user has over the order in which filters are applied.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

string The prepared, escaped text.

Overrides FilterBase::prepare

File

src/Plugin/Filter/CollapseText.php, line 48

Class

CollapseText
Provides a filter to display Collapsible text blocks.

Namespace

Drupal\collapse_text\Plugin\Filter

Code

public function prepare($text, $langcode) {

  // Fix any html style (ie, '<>' delimited) tags into our '[]'
  // style delimited tags.
  $text = preg_replace('/(?<!\\\\)     # not preceded by a backslash
        <             # an open bracket
        (             # start capture
          \\/?         # optional backslash
          collapse    # the string collapse
          [^>]*       # everything up to the closing angle bracket; note that you cannot use one inside the tag!
        )             # stop capture
        >             # close bracket
      /ix', '[$1]', $text);
  $text = preg_replace_callback('/(?<!\\\\)     # not preceded by a backslash
        \\[            # open bracket
        collapse      # the string collapse
        [^\\]]*        # everything up to a closing straight bracket; note that you cannot use one inside a tag!
        \\]            # closing bracket
      /ix', [
    $this,
    'filterPrepareRegexCallback',
  ], $text);
  return $text;
}