You are here

function tablefield_trim in TableField 7.2

Same name and namespace in other branches
  1. 7.3 tablefield.module \tablefield_trim()

Trim trailing empty rows/columns.

Parameters

array $tabledata: The rationalized tablefield.

bool $ignore_head: Whether ignoring header or not.

2 calls to tablefield_trim()
tablefield_field_formatter_view in ./tablefield.module
Implements hook_field_formatter_view().
tablefield_rtrim_cols in ./tablefield.module
Trim trailing empty columns.

File

./tablefield.module, line 1454
Provides a set of fields that can be used to store tabular data with a node.

Code

function tablefield_trim($tabledata, $ignore_head = FALSE) {
  $tabledata = array_reverse($tabledata);

  // For columns the transposed array has the 'weight' one level higher.
  unset($tabledata['weight']);
  foreach ($tabledata as $key => $value) {

    // Removes the weight key for the rows.
    unset($value['weight']);
    $empty = TRUE;
    if (is_array($value)) {
      foreach ($value as $k2 => $v2) {
        if (!empty($v2)) {

          // Stop traversing at the first non empty value.
          if (!$ignore_head || $ignore_head && $k2 !== 'row_0') {
            $empty = FALSE;
          }
        }
      }
    }
    else {
      if (!empty($value)) {

        // Stop traversing at the first non empty value.
        $empty = FALSE;
      }
    }
    if ($empty) {
      unset($tabledata[$key]);
    }
    else {
      break;
    }
  }
  $tabledata = array_reverse($tabledata);
  return $tabledata;
}