You are here

function cck_table_field_formatter_view in CCK Table Field 8

Same name and namespace in other branches
  1. 7 cck_table.module \cck_table_field_formatter_view()

Implements of hook_field_formatter_view().

File

./cck_table.module, line 82
Defines a field type that outputs data in a table.

Code

function cck_table_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  $formatter = $display['type'];
  switch ($formatter) {
    case 'first_row_header':
      $header_type = TRUE;
      break;
    case 'without_header':
      $header_type = FALSE;
      break;
  }
  foreach ($items as $delta => $item) {
    if (!empty($item['table'])) {
      $header = array();
      $rows = array();
      $lines = explode("\n", $item['table']);
      $lines = array_map('trim', $lines);
      $lines = array_filter($lines, 'strlen');
      foreach ($lines as $line) {
        $cells = explode('|', $line);
        if (empty($header) && count($lines) > 1 && $header_type) {
          $header = $cells;
        }
        else {
          $rows[] = $cells;
        }
      }
      if (count($rows) > 0) {
        $attributes = array();
        if (!empty($field['css_id'])) {
          $attributes['id'] = $instance['widget']['settings']['css_id'] . '-' . $instanc['id'];
        }
        if (!empty($field['css_class'])) {
          $attributes['class'] = $instance['widget']['settings']['css_class'];
        }
      }
      $element[$delta] = array(
        '#markup' => theme('table', array(
          'header' => $header,
          'rows' => $rows,
          'attributes' => $attributes,
        )),
      );
    }
  }
  return $element;
}