You are here

public function CollapseText::processTextItem 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::processTextItem()

Helper function to process a text item.

1 call to CollapseText::processTextItem()
CollapseText::processRecurseTree in src/Plugin/Filter/CollapseText.php
Helper function to take a nested tree and turn it into a string.

File

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

Class

CollapseText
Provides a filter to display Collapsible text blocks.

Namespace

Drupal\collapse_text\Plugin\Filter

Code

public function processTextItem($item, $options) {

  // Remove any leftover [collapse] or [/collapse] tags,
  // such as might be caused by the teaser.
  // Leaving out the closing tag.
  // Note that a backslash before the collapse tag will act as an escape.
  $item = preg_replace('/(?<!\\\\)\\[\\/?collapse[^\\]]*\\]/', '', $item);

  // Remove the first backslash before any collapse tags.
  // This allows collapse tags to be escaped.
  $item = str_replace([
    '\\[collapse',
    '\\[/collapse',
  ], [
    '[collapse',
    '[/collapse',
  ], $item);

  // Clear out some miscellaneous tags that are
  // introduced by visual editors...
  // Close paragraph right at the start.
  $item = preg_replace('/^<\\/p>/', '', $item);

  // Open paragraph right at the end.
  $item = preg_replace('/<p(?:\\s[^>]*)?>$/', '', $item);

  // Clear out cruft introduced by the html line ending filter.
  // These are probably more controversial,
  // since they may actually be intended...
  // At the very start.
  $item = preg_replace('/^<br ?\\/?>/', '', $item);

  // At the very end.
  $item = preg_replace('/<br ?\\/?>$/', '', $item);

  // Only return a value if there is something besides whitespace.
  if (preg_match('/\\S/', $item)) {
    return [
      '#type' => 'markup',
      '#markup' => $item,
      '#prefix' => '<div class="collapse-text-text">',
      '#suffix' => '</div>',
    ];
  }
  else {
    return NULL;
  }
}