You are here

function matrix_format_prepare in Matrix field 6.2

Prepare the data to be rendered.

Parameters

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

Return value

array containing the header, first col, and the data

4 calls to matrix_format_prepare()
content-field-field_fieldname.tpl.php in ./content-field-field_fieldname.tpl.php
content-field.tpl.php Default theme implementation to display the value of a field.
matrix_handler_cell::render in ./matrix_handler_cell.inc
matrix_handler_field::render in ./matrix_handler_field.inc
theme_matrix_formatter_default in ./matrix.module
Theme function for 'default' text field formatter.

File

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

Code

function matrix_format_prepare($field_data, $item = NULL, $field_name) {
  $links = array();
  $field_info = content_fields($field_name);
  $rows_elements = $field_info['widget']['type'] == 'table' ? array(
    '',
  ) : (is_array($field_info['rows_elements']) ? $field_info['rows_elements'] : unserialize(str_replace("\r", "", $field_info['rows_elements'])));
  $cols_elements = unserialize(str_replace("\r", "", $field_info['cols_elements']));
  $mode =& $field_info['mode'];
  $empty =& $field_info['empty'];
  $empty_hide =& $field_info['empty_hide'];

  //if there is no data, just return
  if (!is_array($rows_elements) || !is_array($cols_elements) || !is_array($field_data)) {
    return;
  }

  //prepare the data - this will either live in $field_data (defaut formatter) or in $item (.tpl.php file)
  if (isset($item)) {
    $field_data = array();
    ksort($item);
    foreach ($item as $key => $value) {
      if (is_numeric($key)) {
        ksort($value);
        $field_data[] = $value;
      }
    }
  }
  foreach ($rows_elements as $row_id => $e) {
    $rows_header[] =& $e['#title'];
  }
  $header = array(
    '',
  );
  foreach ($cols_elements as $id => $e) {
    $header[] =& $e['#title'];
  }

  //prepare data for calculation fields
  foreach ($field_data as $row_id => $row) {
    foreach ($row as $col_id => $value) {
      $calcdata_cols[$col_id][] =& $value;
      $calcdata_rows[$row_id][] =& $value;
    }
  }

  //replace blank cells with a dash
  ksort($field_data);
  $data = array();
  foreach ($field_data as $row_index => $row) {
    ksort($row);
    foreach ($row as $col_index => $cell_value) {
      $element_type = $mode == 'rows' ? $rows_elements[$row_index]['#type'] : $cols_elements[$col_index]['#type'];
      if ($mode == 'rows') {
        $element_type = $rows_elements[$row_index]['#type'];
        $calc_data = array(
          'calc_method' => $rows_elements[$row_index]['#calc_method'],
          'data' => $calcdata_cols[$col_index],
        );
      }
      else {
        $element_type = $cols_elements[$col_index]['#type'];
        $calc_data = array(
          'calc_method' => $cols_elements[$col_index]['#calc_method'],
          'data' => $calcdata_rows[$row_index],
        );
      }
      $data[$row_index][$col_index] = _matrix_format_cell($element_type, $cell_value, $empty, $calc_data);
      if ($data[$row_index][$col_index] != $empty) {
        $show_row = $row_index;
      }
    }
    $row_label = '<div class="matrix-first-col">' . array_shift($rows_header) . '</div>';
    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] =& $empty;
      }
    }
  }
  $flushed_data[] =& $header;

  //strip out rows at the end of the dataset where there is no data
  if ($empty_hide == 1) {
    foreach ($data as $row_id => $row) {
      if ($row_id <= $show_row) {
        $flushed_data[] = $row;
      }
    }
  }
  else {
    foreach ($data as $row_id => $row) {
      $flushed_data[] = $row;
    }
  }

  //return false if there is no data to display
  if (isset($show_row)) {

    // Hide empty cols and rows
    matrix_compact_table($flushed_data, $empty);
    return $flushed_data;
  }
  else {
    return FALSE;
  }
}