You are here

function theme_cck_list_formatter in CCK List 6

Implementation of hook_field_formatter().

1 string reference to 'theme_cck_list_formatter'
cck_list_theme in ./cck_list.module
Implementation of hook_theme().

File

./cck_list.module, line 114
Defines a field type that outputs data in a list.

Code

function theme_cck_list_formatter($element) {
  $field = content_fields($element['#field_name'], $element['#type_name']);
  $value = $element['#item']['value'];
  switch ($element['#formatter']) {
    case 'list_unordered':
      $type = 'ul';
      break;
    case 'list_ordered':
      $type = 'ol';
      break;
  }
  if (!empty($value)) {
    $flatArray = explode("\n", $value);
    $flatArray = array_map('trim', $flatArray);

    //get rid of any blank lines
    $flatArray = array_filter($flatArray, 'strlen');

    //Security risk

    //$flatArray = check_plain($flatArray);

    //for each hypen (-) it will get nested
    $nestedArray = array();
    foreach ($flatArray as $key => $line) {
      $current =& $nestedArray;
      if (preg_match('/^[-]+/', $line, $matches)) {
        $level = drupal_strlen($matches[0]);
        while ($level > 0) {
          $line = drupal_substr($line, 1);
          $last =& $current[count($current) - 1];
          $current =& $last['children'];
          --$level;
        }
      }
      $current[] = array(
        $line,
      );
    }
    if (count($nestedArray) > 0) {
      $attributes = array();
      if (!empty($field['css_id'])) {
        $attributes['id'] = $field['css_id'] . '-' . $element['#node']->nid;
      }
      if (!empty($field['css_class'])) {
        $attributes['class'] = $field['css_class'];
      }
      return theme('item_list', $nestedArray, NULL, $type, $attributes);
    }
  }
}