You are here

function _collapse_text_process_child_item in Collapse Text 7.2

Same name and namespace in other branches
  1. 6.2 collapse_text.module \_collapse_text_process_child_item()

process a child item.

1 call to _collapse_text_process_child_item()
_collapse_text_process_recurse_tree in ./collapse_text.module
Take a nested tree and turn it into a string.

File

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

Code

function _collapse_text_process_child_item($item, $options) {

  // translate the "tag" into a proper tag, and then parse it
  // as an xml tag; this is more robust than the previous method
  $tag = preg_replace(array(
    '/^\\[/',
    '/\\]$/',
  ), array(
    '<',
    '/>',
  ), $item['tag']);

  // turn HTML entities into XML entities
  // Issue #1109792 by eronte
  $tag = _collapse_text_html_to_xml_entities($tag);
  $xmltag = simplexml_load_string($tag);
  $collapsed = $xmltag['collapsed'] == 'collapsed';
  $class = trim($xmltag['class']);

  // Issue #1096070 by Asgardinho: issues with UTF8 text
  $title = htmlspecialchars(trim($xmltag['title']), ENT_QUOTES, 'UTF-8');

  // set up the styles array. We need to include the 'collapsible' and 'collapsed'
  // classes ourself, because this is no longer done by the theme system.
  $classes = array();
  $classes[] = drupal_clean_css_identifier('collapse-text-fieldset');
  $classes[] = 'collapsible';
  if ($collapsed) {
    $classes[] = 'collapsed';
  }

  // change the style item into an array
  // older versions used a custom safe id function, but
  // I've gone to using the core Drupal function.
  foreach (explode(' ', $class) as $c) {
    if (!empty($c)) {
      $classes[] = drupal_clean_css_identifier($c);
    }
  }

  // if a title is not supplied, look in the first child for a header tag
  if (empty($title)) {
    if ($item['value'][0]['type'] == 'text') {
      $h_matches = array();
      if (preg_match('/(<h\\d[^>]*>(.+?)<\\/h\\d>)/smi', $item['value'][0]['value'], $h_matches)) {
        $title = strip_tags($h_matches[2]);
      }

      // if we get the title from the first header tag, we should remove it from
      // the text so that it isn't repeated.
      if (!empty($title)) {
        $occ = 1;

        // this is a hack to only replace the first instance.
        $item['value'][0]['value'] = str_replace($h_matches[0], '', $item['value'][0]['value'], $occ);
      }
    }
  }

  // if still no title, put in the default title
  if (empty($title)) {
    $title = t($options['default_title']);
    $classes[] = drupal_clean_css_identifier('collapse-text-default-title');
  }

  // create a fieldset that can be themed.
  // per #634666, deviantintegral, although not including the form tag, as we allow nesting.
  $fieldset = array(
    '#type' => 'fieldset',
    '#theme' => 'collapse_text_fieldset',
    '#title' => $title,
    '#collapsible' => TRUE,
    // not actually used, but here for correctness
    '#collapsed' => $collapsed,
    // not actually used, but here for correctness
    '#attributes' => array(
      'class' => $classes,
    ),
    'collapse_text_contents' => _collapse_text_process_recurse_tree($item['value'], $options),
  );
  return $fieldset;
}