You are here

function tablefield_process in TableField 6

Process the tablefield

1 string reference to 'tablefield_process'
tablefield_elements in ./tablefield.module
Implementation of FAPI hook_elements().

File

./tablefield.module, line 277
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_process($element, $edit, $form_state, $form) {
  $delta = $element['#delta'];
  $field = $form['#field_info'][$element['#field_name']];
  if (isset($element['#value']['tablefield'])) {

    // A form was submitted
    $default_value = tablefield_rationalize_table($element['#value']['tablefield']);

    // Catch empty form sumissions for required tablefields
    if ($form_state['submitted'] && $element['#required'] && tablefield_content_is_empty($element['#value'], $field)) {
      form_set_error($element['#field_name'], t('@field is a required field.', array(
        '@field' => $form[$element['#parents'][0]]['#title'],
      )));
    }
  }
  elseif (isset($element['#default_value']['value'])) {

    // Default from database
    $default_value = tablefield_rationalize_table(unserialize($element['#default_value']['value']));
  }
  else {

    // Get the widget default value
    $default_count_cols = $field['widget']['default_value'][0]['tablefield']['count_cols'];
    $default_count_rows = $field['widget']['default_value'][0]['tablefield']['count_rows'];
  }
  $description = $element['#description'] ? $element['#description'] . ' ' : '';
  $description .= t('The first row will appear as the table header.');
  $element['tablefield'] = array(
    '#title' => $field['widget']['label'],
    '#description' => $description,
    '#attributes' => array(
      'id' => 'node-tablefield-' . str_replace('_', '-', $element['#field_name']) . '-' . $delta,
      'class' => 'node-tablefield',
    ),
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#collapsible' => FALSE,
    '#field_name' => $element['#field_name'],
    '#type_name' => $element['#type_name'],
    '#delta' => $element['#delta'],
    '#columns' => $element['#columns'],
  );

  // Give the fieldset the appropriate class if it is required
  if ($element['#required']) {
    $element['tablefield']['#title'] .= ' <span class="form-required" title="' . t('This field is required') . '">*</span>';
  }
  if ($form['#id'] == 'content-field-edit-form') {
    $element['tablefield']['#description'] = t('The first row will appear as the table header. This form defines the table field defaults, but the number of rows/columns and content can be overridden on a per-node basis.');
  }

  // Determine how many rows/columns are saved in the data
  if (!empty($default_value)) {
    $count_rows = count($default_value);
    foreach ($default_value as $row) {
      $temp_count = count($row);
      if ($temp_count > $count_cols) {
        $count_cols = $temp_count;
      }
    }
  }
  else {
    $count_cols = $default_count_cols;
    $count_rows = $default_count_rows;
  }

  // Override the number of rows/columns if the user rebuilds the form
  if (!empty($edit['tablefield']['count_cols'])) {
    $count_cols = $edit['tablefield']['count_cols'];
  }
  if (!empty($edit['tablefield']['count_rows'])) {
    $count_rows = $edit['tablefield']['count_rows'];
  }

  // Allow the user to import a csv file
  $element['tablefield']['import'] = array(
    '#type' => 'fieldset',
    '#tree' => FALSE,
    '#title' => t('Import from CSV'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $element['tablefield']['import']['tablefield_csv_' . $field['field_name'] . '_' . $delta] = array(
    '#title' => 'File upload',
    '#type' => 'file',
  );
  $element['tablefield']['import']['rebuild_' . $field['field_name'] . '_' . $delta] = array(
    '#type' => 'button',
    '#validate' => array(),
    '#limit_validation_errors' => array(
      'title',
    ),
    '#executes_submit_callback' => TRUE,
    '#validate' => array(
      'tablefield_import_csv',
    ),
    '#submit' => array(
      'tablefield_rebuild_form',
      'node_form_build_preview',
    ),
    '#value' => 'Import File to: ' . $field['field_name'] . '-' . $delta,
    '#attributes' => array(
      'class' => array(
        'tablefield-rebuild',
      ),
      'id' => 'tablefield-import-button-' . $field['field_name'] . '-' . $delta,
    ),
  );

  // Render the form table
  $element['tablefield']['a_break'] = array(
    '#type' => 'markup',
    '#value' => '<table>',
  );
  for ($i = 0; $i < $count_rows; $i++) {
    $element['tablefield']['b_break' . $i] = array(
      '#type' => 'markup',
      '#value' => '<tr>',
    );
    for ($ii = 0; $ii < $count_cols; $ii++) {
      $element['tablefield']['cell_' . $i . '_' . $ii] = array(
        '#type' => 'textfield',
        '#size' => 10,
        '#attributes' => array(
          'id' => 'tablefield-' . str_replace('_', '-', $element['#field_name']) . '-' . $delta . '-cell-' . $i . '-' . $ii,
          'class' => 'tablefield-row-' . $i . ' tablefield-col-' . $ii,
        ),
        '#default_value' => empty($field_value) ? $default_value[$i][$ii] : $field_value,
        '#prefix' => '<td>',
        '#suffix' => '</td>',
      );
    }
    $element['tablefield']['d_break' . $i] = array(
      '#type' => 'markup',
      '#value' => '</tr>',
    );
  }
  $element['tablefield']['t_break' . $i] = array(
    '#type' => 'markup',
    '#value' => '</table>',
  );

  // Allow the user to add more rows/columns
  $element['tablefield']['count_cols'] = array(
    '#title' => t('How many Columns'),
    '#type' => 'textfield',
    '#size' => 5,
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
    //'#default_value' => $count_cols,
    '#value' => $count_cols,
  );
  $element['tablefield']['count_rows'] = array(
    '#title' => t('How many Rows'),
    '#type' => 'textfield',
    '#size' => 5,
    '#prefix' => '<div class="clear-block">',
    '#suffix' => '</div>',
    //'#default_value' => $count_rows,
    '#value' => $count_rows,
  );
  $element['tablefield']['rebuild'] = array(
    '#type' => 'button',
    '#value' => t('Rebuild Table'),
    '#attributes' => array(
      'class' => 'tablefield-rebuild',
    ),
  );
  if (!empty($field['cell_processing'])) {
    $filter_key = $element['#columns'][0];
    $format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
    $parents = array_merge($element['#parents'], array(
      $filter_key,
    ));
    $element[$filter_key] = filter_form($format, 1, $parents);
  }
  return $element;
}