You are here

function colorbox_node_ajax_submit in Colorbox Node 7.3

Handles all the ajax forms and runs them through this central function to either return the form with error messages, or to close the colorbox and populate our messages div container.

_state

Parameters

$form:

Return value

array

Throws

Exception

1 string reference to 'colorbox_node_ajax_submit'
colorbox_node_form_ajax_support in ./colorbox_node.module
_state _id

File

./colorbox_node.module, line 182
Creates a menu callback with support for displaying a node inside of a colorbox.

Code

function colorbox_node_ajax_submit($form, &$form_state) {
  if (!form_get_errors()) {

    // Set our commands for the return to just display status messages.
    $commands = array();

    // Replace the content area with the provided destination
    if (isset($form['#cbox_replace'])) {
      return colorbox_node_output($form['#cbox_replace']);
    }

    // Close the colorbox modal.
    // This is the default behavior.
    $commands[] = ajax_command_invoke('#cboxClose', 'click');

    // Reload the main window if required.
    if (isset($form['#cbox_reload'])) {

      // @TODO: What happens to our messages if the page is reloaded?
      $commands[] = array(
        'command' => 'colorboxNodeReload',
      );
    }
    else {

      // Redirect the user to the passed in destination.
      // @TODO: Add support for dynamic redirects based on newly created content
      if (isset($form['#cbox_redirect'])) {
        $url = url($form['#cbox_redirect']);
        $commands[] = array(
          'command' => 'colorboxNodeRedirect',
          'data' => $url,
        );
      }
      else {
        if (isset($form_state['redirect']) && !empty($form_state['redirect'])) {

          // Lets see if we can find the natural redirect from the form.
          // @TODO: I think the redirect key can also be an array.  Add checks for that.
          if (is_array($form_state['redirect']) && isset($form_state['redirect'][0])) {
            $path = $form_state['redirect'][0];
            $options = array();
            if (isset($form_state['redirect'][1])) {
              foreach ($form_state['redirect'][1] as $key => $value) {
                $options[$key] = $value;
              }
            }
            $data = url($path, $options);
          }
          else {
            $data = url($form_state['redirect']);
          }
          $commands[] = array(
            'command' => 'colorboxNodeRedirect',
            'data' => $data,
          );
        }
        else {

          // Set message.
          // @TODO: How not to make this theme specific?
          // @TODO: Maybe a variable on the admin settings page?
          $commands[] = ajax_command_before('#main-content', theme('status_messages'));
        }
      }
    }
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
  return $form;
}