0
  1. I've added drupal_add_tabledrag() on one of my custom forms, it enables the users to drag to reorder the elements' weights, which then gets submitted via ajax to a callback function that saves the weights.

    It's all working and the weights get saved, but after the save, the asterisks that indicate the weights have been changed, and also the message "Changes made in this table will not be saved until the form is submitted" won't go away.

    What's the best way to let the dragtable know that the weights have been saved?

  2. What's the best way to display status messages from an ajax callback function? I'd like to say "Weights saved successfully" after the callback function gets called. Currently I'm using drupal_set_message and return true from the callback function. This works as theme_system_messages() get prepended to the responses (see #ajax['callback']), but I'd like to know if there's a better way to achieve this.

4

I managed to solve this finally. My answer to both questions was Ajax Commands.

To anyone looking for the solution, here's what I did:

Question 1:

Instead of messing around with Dragtable, it's easier to just re-create the form and pass it back so it replaces the original form. I had to rebuild the form in my ajax callback function, then return it with an ajax command. To rebuild the form, you needed to create a new form_state variable and use drupal_rebuild_form function like so:

function my_ajax_form_callback($form, &$form_state) {
  $new_state = array();
  $new_state['build_info'] = $form_state['build_info'];
  $new_state['rebuild'] = TRUE;
  $new_state['values'] = array();
  $new_state += form_state_defaults();

  $new_form_array = drupal_rebuild_form('my_ajax_form_id', $new_state);
  $new_form = drupal_render($new_form_array);

  $commands = array();
  $commands[] = ajax_command_html('#form-container-element-id', $new_form);

  return array('#type' => 'ajax', '#commands' => $commands);
}

The original form button that submits to this callback also should have the form-container-element-id in the wrapper element of the #ajax array (it should always be a CSS ID so it shouldn't have the # to indicate it's an ID):

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Submit',
  '#ajax' => array(
    'callback' => 'my_ajax_form_callback',
    'wrapper' => 'form-container-element-id',
    'method' => 'html',
    'event' => 'click',
  ),
);

Here's the API documentation link for ajax commands: Ajax framework commands (Drupal 7)

Question 2:

The best way to display status messages after an ajax callback is to use theme_status_messages() function. The snippet below also goes in the my_ajax_form_callback function before returning from the function:

drupal_set_message('This is a status message', 'status', FALSE);
$messages = theme_status_messages(array('display' => 'status'));
$commands[] = ajax_command_html('#status-message-container', $messages);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.