You are here

function tablefield_form_alter in TableField 7.3

Implements hook_form_alter().

Avoid empty tables on multivalue fields with default header values.

File

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

Code

function tablefield_form_alter(&$form, &$form_state, $form_id) {
  if (empty($form_state['field'])) {
    return;
  }
  foreach (element_children($form_state['field']) as $field_name) {
    foreach ($form_state['field'][$field_name] as $lang => $value) {
      if (isset($value['instance']) && $value['instance']['widget']['type'] === 'tablefield' && $value['field']['cardinality'] != 1) {
        $key_exists = FALSE;
        $max_delta = $form[$field_name][$lang]['#max_delta'];
        $parents = array_merge($value['array_parents'], array(
          $field_name,
          $lang,
        ));
        $element =& drupal_array_get_nested_value($form, $parents, $key_exists);
        if ($key_exists && isset($element[$max_delta])) {
          unset($element[$max_delta]);
        }
      }
    }
  }
}