You are here

function _collapse_text_filter_process in Collapse Text 7.2

Implements hook_filter_FILTER_process().

This function uses a pseudo-"parser". Earlier versions used a (very complex) regular expression, but I think this verion will work better and have fewer memory issues.

1 call to _collapse_text_filter_process()
CollapseTextTestCase::processWrapper in ./collapse_text.test
1 string reference to '_collapse_text_filter_process'
collapse_text_filter_info in ./collapse_text.module
Implements hook_filter_info().

File

./collapse_text.module, line 182
collapse_text is an input filter that allows text to be collapsible

Code

function _collapse_text_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {

  // retrieve the options, then look for overrides
  $options = $filter->settings;
  list($text, $options) = _collapse_text_check_options($text, $options);

  // find all of the collapse tags and their location in the string
  $tags = _collapse_text_find_tags($text, $options);

  // determine the level of nesting for each element.
  $levels = _collapse_text_find_levels($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 = _collapse_text_process_recurse_levels($text, 0, strlen($text), $levels, $options);

    // Take the tree, and turn it into FAPI elements, then embed them in a form
    // if requested, see #634666 for more details.
    $render_number =& drupal_static(__FUNCTION__, 1);

    // Used to generate unique ids to prevent an E_NOTICE.
    $holder = array();
    if ($options['form']) {
      $holder = array(
        '#type' => 'form',
        '#theme' => 'collapse_text_form',
        '#id' => 'collapse-text-dynamic-form-number-' . $render_number++,
      );
    }
    else {
      $holder = array(
        '#type' => 'markup',
        '#prefix' => '<div id="' . 'collapse-text-dynamic-div-number-' . $render_number++ . '">',
        '#suffix' => '</div>',
      );
    }
    $holder['collapse_text_internal_text'] = _collapse_text_process_recurse_tree($tree, $options);

    // render the elements back to a string
    $text = drupal_render($holder);
    drupal_add_library('system', 'drupal.collapse');

    // by adding this here, our preprocess function doesn't need to actually
    // recurse through the entire page array. This may be faster...
  }
  return $text;
}