This function is analogous to drupal_get_form() and provides a simple way to place a form in a dialog, without having to write a callback.

Example:

In a normal, non-dialog menu definition, you might do the following:

$item['foo/bar'] = array(
  'title' => 'Foo bar',
  'page callback' => 'drupal_get_form',
  'page arguments' => array('foo_bar_form'),
  ...
);

And now with this function you can create a similar menu item like this:

$item['foo/bar/%ctools_js'] = array(
  'title' => 'Foo bar',
  'page callback' => 'dialog_get_form',
  'page arguments' => array('foo_bar_form', 2),
  ...
);

dialog_get_form will handle all the js/non-js form building, passing on any additional arguments to the form builder. Upon form submission, the ajax response is created in one of the following ways:

  1. If a function exists following the format of [form_id]_dialog_success, it will be called and should return the appropriate ajax commands array. This is very similar to creating a [form_id]_submit function, but only gets called when in an ajax context.
  2. Otherwise, if the submit handler set a redirect path in the form state, then a redirect command is created.
  3. And if all else fails, a simple reload command is issued.

Here is what the user login example looks like now.

function dialog_user_menu() {
  $items['user/login/%ctools_js'] = array(
    'title' => 'Log in',
    'page callback' => 'dialog_get_form',
    'page arguments' => array('user_login', 2),
    'access callback' => 'user_is_anonymous',
    'type' => MENU_CALLBACK,
    'file' => 'user.pages.inc',
    'file path' => drupal_get_path('module', 'user'),
  );
  return $items;
}

Thats it, just a menu declaration, no callback needed! Notice though, that you must take care to include the file where the form builder is located.

CommentFileSizeAuthor
dialog_get_form.patch2.21 KBzroger

Comments

zroger’s picture

Status: Needs review » Fixed

committed to 6.x

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

miraclestyle’s picture

Status: Closed (fixed) » Needs review

Forgive me if I am wrong, as I am not experienced developer, but shouldn't the last function call in "dialog_get_form":

  else {
    return drupal_get_form($form_id);
  }

include the "$args" as well:

  else {
    return drupal_get_form($form_id, $args);
  }

???

Once again, my apologies if this is wrong!

Cheers.
Elvin.

entrigan’s picture

Does using this function then restrict the dialog to only use the defaults? I ask this because normally I put the configuration in the menu callback function, e.g. $output[$i]['options']['resizable'] = FALSE;

EDIT: nm, this function can be called from within the callback function, so everything can be handled per normal.

drewish’s picture

Status: Needs review » Closed (fixed)

ctools_ajax_render() calls exit()