You are here

function tablefield_field_presave in TableField 7.2

Same name and namespace in other branches
  1. 7.3 tablefield.module \tablefield_field_presave()
  2. 7 tablefield.module \tablefield_field_presave()

Implements hook_field_presave().

1 call to tablefield_field_presave()
tablefield_field_prepare_view in ./tablefield.module
Implements hook_field_prepare_view().

File

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

Code

function tablefield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  foreach ($items as $delta => $table) {
    if (empty($table['value'])) {
      $tablefield = array();
      if (!empty($table['tablefield'])) {
        unset($tablefield['tablefield']);

        // Sort by weight.
        uasort($table['tablefield']['tabledata'], 'drupal_sort_weight');

        // Put the data in the desired order before saving.
        $row_counter = $col_counter = 0;
        foreach ($table['tablefield']['tabledata'] as $row) {
          foreach ($row as $key => $cell) {
            if ($key === 'weight') {
              $tablefield['tablefield']['tabledata']['row_' . $row_counter]['weight'] = $cell;
            }
            else {
              $tablefield['tablefield']['tabledata']['row_' . $row_counter]['col_' . $col_counter] = $cell;
            }
            $col_counter++;
          }
          $row_counter++;
          $col_counter = 0;
        }

        // Clear the old table data and repopulate it with the new values.
        unset($table['tablefield']['tabledata']);
        $table['tablefield']['tabledata'] = $tablefield['tablefield']['tabledata'];

        // Add the non-value data back in before we save.
        $tablefield = array_merge($tablefield, $table);
      }
      $items[$delta]['value'] = isset($tablefield['tablefield']) ? serialize($tablefield['tablefield']) : '';
    }
    elseif (empty($table['tablefield'])) {

      // Batch processing only provides the 'value'.
      $items[$delta]['tablefield'] = unserialize($items[$delta]['value']);
    }
  }
}