function matrix_field_formatter_view in Matrix field 8.2
Same name and namespace in other branches
- 7.2 matrix.module \matrix_field_formatter_view()
Implements hook_field_formatter_view().
File
- ./
matrix.module, line 717 - Contains matrix.module.
Code
function matrix_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $raw_items, $display) {
$element = array();
if (!$raw_items) {
//If there are no items to show (eg. no content saved for this field on this node) then hide the field
return;
}
switch ($display['type']) {
case 'matrix_default':
if (count($raw_items[0]) == 1) {
//if a single value is passed in from views
$element[0] = array(
'#markup' => field_filter_xss($raw_items[0]['value']),
);
}
//transpose the $items array into a [$row][$col] = value arangement
foreach ($raw_items as $delta => $item) {
$items[$item['row']][$item['col']][] = matrix_cell_value($item, $field);
}
//work out how many rows and columns to show, which is the number of rows with at least some data in the row.
// this count algorithm assumes the keys are in order, so make sure they are.
ksort($items);
foreach ($items as $row_id => $row) {
foreach ($row as $col_id => $value) {
if ($value != '') {
$rows_count = $row_id;
// if this row has a value, we don't need to keep looking at the rest
// of the columns.
break;
}
}
}
if ($field['settings']['cols_count'] > 0) {
$cols_count = $field['settings']['cols_count'];
}
else {
foreach ($items as $row_id => $row) {
foreach ($row as $col_id => $value) {
if (!empty($value)) {
$max_cols[$col_id] = $col_id;
}
}
}
$cols_count = max($max_cols);
}
//populate the data part of the table rows
$rows = array();
for ($row = 1; $row <= $rows_count; $row++) {
for ($col = 1; $col <= $cols_count; $col++) {
$value = array(
'',
);
if (isset($items[$row][$col])) {
$value = $items[$row][$col];
}
$rows[$row][$col] = $value;
}
}
//collapse any multi-valued results into a list
foreach ($rows as $row_id => $row) {
foreach ($row as $col_id => $values) {
if (count($values) > 1) {
$rows[$row_id][$col_id] = array(
'data' => _theme('item_list', array(
'items' => $values,
)),
'class' => 'matrix-col-' . $col_id . ' matrix-row-' . $row_id . ' matrix-cell-' . $row_id . '-' . $col_id,
);
}
else {
$rows[$row_id][$col_id] = array(
'data' => $values[0],
'class' => 'matrix-col-' . $col_id . ' matrix-row-' . $row_id . ' matrix-cell-' . $row_id . '-' . $col_id,
);
}
}
}
if ($field['type'] == 'matrix_text' && $field['settings']['spreadsheet_style'] == TRUE) {
for ($col = 1; $col <= $cols_count; $col++) {
$headers[$col] = matrix_make_letter($col);
}
array_unshift($headers, '');
//cell 0,0
for ($row = 1; $row <= $rows_count; $row++) {
$cell = array(
'data' => $row,
'class' => 'matrix-col-header',
);
array_unshift($rows[$row], $cell);
}
}
elseif ($field['type'] == 'matrix_custom') {
$headers = array();
$headers = array_fill(1, $cols_count, ' ');
//initialize the headers with spaces (in case headers have not title set)
$settings = unserialize($field['settings']['settings']);
if (isset($settings['cols'])) {
foreach ($settings['cols'] as $col_id => $col) {
$headers[$col_id] = $col['title'];
}
}
$have_row_labels = FALSE;
if (!empty($settings['rows'])) {
foreach ($settings['rows'] as $row_id => $row) {
if (!empty($row['title'])) {
$have_row_labels = TRUE;
}
$cell = array(
'data' => $row['title'],
'class' => 'matrix-col-header',
);
if (isset($rows[$row_id])) {
array_unshift($rows[$row_id], $cell);
}
}
}
if ($have_row_labels) {
//had left lables so translate the headers one to the right
array_unshift($headers, '');
//cell 0,0
}
}
else {
$headers = NULL;
}
$element[0] = array(
'#theme' => 'matrix_output',
'#header' => $headers,
'#rows' => $rows,
);
break;
}
return $element;
}