You are here

function collapse_text_process in Collapse Text 6.2

Same name and namespace in other branches
  1. 5 collapse_text.module \collapse_text_process()
  2. 6 collapse_text.module \collapse_text_process()

Implementation of hook_filter($op='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_process()
collapse_text_filter in ./collapse_text.module
Implementation of hook_filter().

File

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

Code

function collapse_text_process($text, $format) {

  // initialize the options, then check to see if an options
  // tag is provided and if so, parse it. Note that only one
  // options tag is supported per invocation.
  $options = array(
    'form' => variable_get("collapse_text_use_form_{$format}", 1),
    'default_title' => variable_get("collapse_text_default_title_{$format}", COLLAPSE_TEXT_DEFAULT_TITLE),
  );
  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, per deviantintegral
    static $render_number = 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);
  }
  return $text;
}