You are here

function tablefield_field in TableField 6

Implementation of hook_field().

File

./tablefield.module, line 89
This module provides a set of fields that can be used to store tabular data with a node. The implementation uses a custom CCK widget.

Code

function tablefield_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'presave':
      foreach ($items as $delta => $table) {
        if (empty($table['value'])) {
          $tablefield = array();
          foreach ($table['tablefield'] as $key => $value) {
            $tablefield[$key] = $value;
          }
          $items[$delta]['value'] = serialize($tablefield);
        }
        elseif (empty($table['tablefield'])) {

          // Batch processing only provides the 'value'
          $items[$delta]['tablefield'] = unserialize($items[$delta]['value']);
        }
      }
      break;
    case 'load':
      foreach ($items as $delta => $table) {
        $items[$delta]['tabledata'] = tablefield_rationalize_table(unserialize($table['value']));
      }
      break;
    case 'sanitize':

      // We need to make the table data available to display
      foreach ($items as $delta => $table) {
        if (!empty($table['tablefield'])) {
          $tabledata = tablefield_rationalize_table($table['tablefield']);
        }
        elseif (!empty($table['value'])) {
          $tabledata = tablefield_rationalize_table(unserialize($table['value']));
        }

        // Multivalue fields will have one row in the db, so make sure that it isn't empty
        if (isset($tabledata)) {

          // Run the table body through input filters
          if (!empty($tabledata)) {
            foreach ($tabledata as $row_key => $row) {
              foreach ($row as $col_key => $cell) {
                if (!empty($field['cell_processing'])) {
                  $tabledata[$row_key][$col_key] = array(
                    'data' => check_markup($cell, $table['format']),
                    'class' => 'row-' . $row_key . ' col-' . $col_key,
                  );
                }
                else {
                  $tabledata[$row_key][$col_key] = array(
                    'data' => check_plain($cell),
                    'class' => 'row-' . $row_key . ' col-' . $col_key,
                  );
                }
              }
            }
          }
          $header = array_shift($tabledata);
          $items[$delta]['value'] = theme('tablefield_view', $header, $tabledata, $field['field_name'], $delta);
        }
      }
      break;
  }
}