You are here

function theme_matrix_formatter_default in Matrix field 6

Same name and namespace in other branches
  1. 6.2 matrix.module \theme_matrix_formatter_default()

Theme function for 'default' text field formatter. This gets called for each row, but we only want to render all the data at once so we use a staticly cached variable to ignore subsquent calls.

Parameters

$element The whole $node object, but containing specific information relating to the delta of this element.:

Return value

HTML.

File

./matrix.module, line 284
Defines simple matrix field types.

Code

function theme_matrix_formatter_default($element) {
  $field_info = $element['#node']->{$element}['#field_name'];
  if (!is_array($field_info['cols_header']) || !is_array($field_info['data'])) {
    return;
  }
  $header = $field_info['cols_header'];

  // Translatable headers
  foreach ($header as $i => $h) {
    $header[$i] = t($h);
  }
  array_unshift($header, '');
  static $rendered;

  //since we are rendering the whole thing in one go, we don't want to rerender for each fow
  $nid = $element['#node']->nid;
  if ($rendered[$nid][$element['#field_name']] != TRUE) {
    $rendered[$nid][$element['#field_name']] = TRUE;

    //replace blank cells with a dash
    ksort($field_info['data']);
    foreach ($field_info['data'] as $row_index => $row) {
      ksort($row);
      foreach ($row as $cell_index => $cell_value) {
        if ($cell_value == '') {
          $data[$row_index][$cell_index] = '<span class="matrix-row-' . $row_index . ' matrix-column-' . $cell_index . ' matrix-cell-' . $row_index . '-' . $cell_index . '">' . '-' . '</span>';
        }
        else {
          $data[$row_index][$cell_index] = '<span class="matrix-row-' . $row_index . ' matrix-column-' . $cell_index . ' matrix-cell-' . $row_index . '-' . $cell_index . '">' . $cell_value . '</span>';
          $show_row = $row_index;
          $has_data = TRUE;

          //this is used to hide the whole display if no data is entered what so ever.
        }
      }
      $row_label = '<b>' . array_shift($field_info['rows_header']) . '</b>';
      array_unshift($data[$row_index], $row_label);
    }

    //add blank cells if the number of rows/columns is different to the number of headers

    //this can happen when columns/rows are added to an existing content type
    $row_count = count($data[0]);
    if ($row_count < count($header)) {
      for ($i = $row_count; $i < count($header); $i++) {
        for ($j = 0; $j < count($data); $j++) {
          $data[$j][$i] = '<span class="matrix-row-' . $j . ' matrix-column-' . $i . ' matrix-cell-' . $j . '-' . $i . '">' . '-' . '</span>';
        }
      }
    }

    //strip out rows at the end of the dataset where there is no data
    foreach ($data as $row_id => $row) {
      if ($row_id <= $show_row) {
        $flushed_data[] = $row;
      }
    }
    if ($has_data) {
      return theme('table', $header, $flushed_data);
    }
    else {
      return '';
    }
  }
}