You are here

public function CollapseText::checkOptions in Collapse Text 2.0.x

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

Helper function to see if there is an options tag available.

If so, remove it from the text and set the options.

1 call to CollapseText::checkOptions()
CollapseText::process in src/Plugin/Filter/CollapseText.php
Performs the filter processing.

File

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

Class

CollapseText
Provides a filter to display Collapsible text blocks.

Namespace

Drupal\collapse_text\Plugin\Filter

Code

public function checkOptions($text, $options) {
  $matches = [];
  $regex_text = '
      (?<!\\\\)     # not proceeded by a backslash
      \\[            # opening bracket
      collapse      # the word collapse
      \\s+           # white space
      options       # the word options
      [^\\]]*        # everything until the closing bracket
      \\]            # a closing bracket
    ';
  if (preg_match('/' . $regex_text . '/smx', $text, $matches)) {
    $opt_tag = $matches[0];

    // Remove the "collapse" from the front of the tag,
    // baking in an "options" tag.
    $opt_tag = preg_replace('/^\\[collapse /', '[', $opt_tag);

    // Change to angle brackets, so it can be parsed as XML.
    $opt_tag = preg_replace([
      '/^\\[/',
      '/\\]$/',
    ], [
      '<',
      '/>',
    ], $opt_tag);

    // Turn HTML entities into XML entities.
    $opt_tag = $this
      ->htmlToXmlEntities($opt_tag);
    $opt_tag = simplexml_load_string($opt_tag);

    // Form options are either 'form="form"' or 'form="noform"'.
    if ($opt_tag['form'] == 'form') {
      $options['form'] = 1;
    }
    elseif ($opt_tag['form'] == 'noform') {
      $options['form'] = 0;
    }
    if ($opt_tag['default_title']) {

      // Issue #1096070 by Asgardinho: issues with UTF8 text.
      $options['default_title'] = htmlspecialchars(trim($opt_tag['default_title']), ENT_QUOTES, 'UTF-8');
    }

    // Remove the options tag, including any miscellaneous <p>, </p>,
    // or <br> tags around it.
    $text = preg_replace('/(?:<\\/?p>|<br\\s*\\/?>)*' . $regex_text . '(?:<\\/?p>|<br\\s*\\/?>)*/smx', '', $text);
  }
  return [
    $text,
    $options,
  ];
}