function tablefield_rebuild_form_ajax in TableField 7
Same name and namespace in other branches
- 7.3 tablefield.module \tablefield_rebuild_form_ajax()
- 7.2 tablefield.module \tablefield_rebuild_form_ajax()
AJAX callback to rebuild the number of rows/columns. The basic idea is to descend down the list of #parent elements of the clicked_button in order to locate the tablefield inside of the $form array. That is the element that we need to return.
_state
Parameters
array $form:
1 string reference to 'tablefield_rebuild_form_ajax'
- tablefield_field_widget_form in ./tablefield.module 
- Implements hook_widget_form().
File
- ./tablefield.module, line 527 
- 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_rebuild_form_ajax($form, $form_state) {
  $rebuild = $form;
  $parents = $form_state['clicked_button']['#parents'];
  if ($form['#id'] == 'field-ui-field-edit-form') {
    $rebuild = $form['instance']['default_value_widget'][$parents[0]];
  }
  else {
    foreach ($parents as $parent) {
      // Locate the delta of the field - 0 deltas need to break after
      // descending to the 'rebuild' element, but deltas greater than
      // 0 need to break /before/ adding the 'rebuild' element.
      if (is_int($parent)) {
        $delta = $parent;
      }
      $tmp = $rebuild;
      if ($parent == 'rebuild' || $parent == 'import') {
        $rebuild = $delta == 0 ? $tmp[$parent] : $tmp;
        //$rebuild = $tmp[$parent];
        break;
      }
      $rebuild = $tmp[$parent];
    }
  }
  // We don't want to re-send the format/_weight options.
  unset($rebuild['format']);
  unset($rebuild['_weight']);
  // We need to avoid sending headers or the multipart form
  // will make it fail. So, we need to explicitly define the
  // whole response to ajax_deliver().
  return array(
    '#type' => 'ajax',
    '#header' => FALSE,
    '#commands' => array(
      ajax_command_insert(NULL, drupal_render($rebuild)),
      ajax_command_prepend(NULL, theme('status_messages')),
    ),
  );
}