You are here

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

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

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

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

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

Class

CollapseText
Provides a filter to display Collapsible text blocks.

Namespace

Drupal\collapse_text\Plugin\Filter

Code

public function process($text, $langcode) {

  // Retrieve the options, then look for overrides.
  $options = $this->settings;
  list($text, $options) = $this
    ->checkOptions($text, $options);

  // Find all of the collapse tags and their location in the string.
  $tags = $this
    ->findTags($text, $options);

  // Determine the level of nesting for each element.
  $levels = $this
    ->findLevels($tags, $options);

  // Process the text if there are any collapse tags...
  if (count($levels)) {

    // Turn the levels and the string into a structured tree.
    $tree = $this
      ->processRecurseLevels($text, 0, strlen($text), $levels, $options);

    // Take the tree, and turn it into FAPI elements, then embed
    // them in a form if requested.
    // Used to generate unique ids to prevent an E_NOTICE.
    static $render_number = 1;
    $holder = [];
    if ($options['form']) {
      $holder = [
        '#type' => 'form',
        '#theme' => 'collapse_text_form',
        '#form_id' => 'collapse-text-dynamic-form-number-' . $render_number,
        '#id' => 'collapse-text-dynamic-form-number-' . $render_number++,
      ];
    }
    else {
      $holder = [
        '#type' => 'markup',
        '#prefix' => '<div id="' . 'collapse-text-dynamic-div-number-' . $render_number++ . '">',
        '#suffix' => '</div>',
      ];
    }
    $holder['collapse_text_internal_text'] = $this
      ->processRecurseTree($tree, $options);

    // Render the elements back to a string.
    $text = drupal_render($holder);
  }
  return new FilterProcessResult($text);
}