5 * Sample AJAX functionality so people can see some of the CTools AJAX
9 // ---------------------------------------------------------------------------
13 * Implementation of hook_menu()
15 function ctools_ajax_sample_menu() {
16 $items['ctools_ajax_sample'] = array(
17 'title' => 'Chaos Tools AJAX Demo',
18 'page callback' => 'ctools_ajax_sample_page',
19 'access callback' => TRUE,
20 'type' => MENU_NORMAL_ITEM,
22 $items['ctools_ajax_sample/simple_form'] = array(
23 'title' => 'Simple Form',
24 'page callback' => 'ctools_ajax_simple_form',
25 'access callback' => TRUE,
26 'type' => MENU_CALLBACK,
28 $items['ctools_ajax_sample/%ctools_js/hello'] = array(
29 'title' => 'Hello World',
30 'page callback' => 'ctools_ajax_sample_hello',
31 'page arguments' => array(1),
32 'access callback' => TRUE,
33 'type' => MENU_CALLBACK,
35 $items['ctools_ajax_sample/%ctools_js/tablenix/%'] = array(
36 'title' => 'Hello World',
37 'page callback' => 'ctools_ajax_sample_tablenix',
38 'page arguments' => array(1, 3),
39 'access callback' => TRUE,
40 'type' => MENU_CALLBACK,
42 $items['ctools_ajax_sample/%ctools_js/login'] = array(
44 'page callback' => 'ctools_ajax_sample_login',
45 'page arguments' => array(1),
46 'access callback' => TRUE,
47 'type' => MENU_CALLBACK,
49 $items['ctools_ajax_sample/%ctools_js/animal'] = array(
51 'page callback' => 'ctools_ajax_sample_animal',
52 'page arguments' => array(1),
53 'access callback' => TRUE,
54 'type' => MENU_CALLBACK,
56 $items['ctools_ajax_sample/%ctools_js/login/%'] = array(
57 'title' => 'Post-Login Action',
58 'page callback' => 'ctools_ajax_sample_login_success',
59 'page arguments' => array(1, 3),
60 'access callback' => TRUE,
61 'type' => MENU_CALLBACK,
63 $items['ctools_ajax_sample/jumped'] = array(
64 'title' => 'Successful Jumping',
65 'page callback' => 'ctools_ajax_sample_jump_menu_page',
66 'access callback' => TRUE,
67 'type' => MENU_NORMAL_ITEM,
73 function ctools_ajax_simple_form() {
74 ctools_include('content');
75 ctools_include('context');
77 $context = ctools_context_create('node', $node);
78 $context = array('context_node_1' => $context);
79 return ctools_content_render('node_comment_form', 'node_comment_form', ctools_ajax_simple_form_pane(), array(), array(), $context);
82 function ctools_ajax_simple_form_pane() {
83 $configuration = array(
85 'context' => 'context_node_1',
86 'override_title' => 0,
87 'override_title_text' => '',
89 return $configuration;
93 * Implementation of hook_theme()
95 * Render some basic output for this module.
97 function ctools_ajax_sample_theme() {
99 // Sample theme functions.
100 'ctools_ajax_sample_container' => array(
101 'arguments' => array('content' => NULL),
106 // ---------------------------------------------------------------------------
110 * Page callback to display links and render a container for AJAX stuff.
112 function ctools_ajax_sample_page() {
115 // Include the CTools tools that we need.
116 ctools_include('ajax');
117 ctools_include('modal');
119 // Add CTools' javascript to the page.
120 ctools_modal_add_js();
122 // Create our own javascript that will be used to theme a modal.
123 $sample_style = array(
124 'ctools-sample-style' => array(
125 'modalSize' => array(
132 'modalOptions' => array(
134 'background-color' => '#000',
136 'animation' => 'fadeIn',
137 'modalTheme' => 'CToolsSampleModal',
138 'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'), 'alt' => t('Loading...'), 'title' => t('Loading'))),
142 drupal_add_js($sample_style, 'setting');
144 // Since we have our js, css and images in well-known named directories,
145 // CTools makes it easy for us to just use them without worrying about
146 // using drupal_get_path() and all that ugliness.
147 ctools_add_js('ctools-ajax-sample', 'ctools_ajax_sample');
148 ctools_add_css('ctools-ajax-sample', 'ctools_ajax_sample');
150 // Create a list of clickable links.
153 // Only show login links to the anonymous user.
154 if ($user->uid == 0) {
155 $links[] = ctools_modal_text_button(t('Modal Login (default style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'));
157 // The extra class points to the info in ctools-sample-style which we added
158 // to the settings, prefixed with 'ctools-modal'.
159 $links[] = ctools_modal_text_button(t('Modal Login (custom style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'), 'ctools-modal-ctools-sample-style');
162 // Four ways to do our animal picking wizard.
163 $button_form = ctools_ajax_sample_ajax_button_form();
164 $links[] = l(t('Wizard (no modal)'), 'ctools_ajax_sample/nojs/animal');
165 $links[] = ctools_modal_text_button(t('Wizard (default modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'));
166 $links[] = ctools_modal_text_button(t('Wizard (custom modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'), 'ctools-modal-ctools-sample-style');
167 $links[] = drupal_render($button_form);
169 $links[] = ctools_ajax_text_button(t('Hello world!'), "ctools_ajax_sample/nojs/hello", t('Replace text with "hello world"'));
171 $output = theme('item_list', array('items' => $links, 'title' => t('Actions')));
173 // This container will have data AJAXed into it.
174 $output .= theme('ctools_ajax_sample_container', array('content' => '<h1>' . t('Sample Content') . '</h1>'));
176 // Create a table that we can have data removed from via AJAX.
177 $header = array(t('Row'), t('Content'), t('Actions'));
179 for($i = 1; $i < 11; $i++) {
181 'class' => array('ajax-sample-row-'. $i),
185 ctools_ajax_text_button("remove", "ctools_ajax_sample/nojs/tablenix/$i", t('Delete this row')),
190 $output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('ajax-sample-table'))));
192 // Show examples of ctools javascript widgets
193 $output .= '<h2>'. t('CTools Javascript Widgets') .'</h2>';
195 // Create a drop down menu
197 $links[] = array('title' => t('Link 1'), 'href' => $_GET['q']);
198 $links[] = array('title' => t('Link 2'), 'href' => $_GET['q']);
199 $links[] = array('title' => t('Link 3'), 'href' => $_GET['q']);
201 $output .= '<h3>' . t('Drop Down Menu') . '</h3>';
202 $output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links));
204 // Create a collapsible div
205 $handle = t('Click to Collapse');
206 $content = 'Nulla ligula ante, aliquam at adipiscing egestas, varius vel arcu. Etiam laoreet elementum mi vel consequat. Etiam scelerisque lorem vel neque consequat quis bibendum libero congue. Nulla facilisi. Mauris a elit a leo feugiat porta. Phasellus placerat cursus est vitae elementum.';
207 $output .= '<h3>'. t('Collapsible Div') .'</h3>';
208 $output .= theme('ctools_collapsible', array('handle' => $handle, 'content' => $content, 'collapsed' => FALSE));
210 // Create a jump menu
211 ctools_include('jump-menu');
212 $form = drupal_get_form('ctools_ajax_sample_jump_menu_form');
213 $output .= '<h3>'. t('Jump Menu') .'</h3>';
214 $output .= drupal_render($form);
216 return array('markup' => array('#markup' => $output));
220 * Returns a "take it all over" hello world style request.
222 function ctools_ajax_sample_hello($js = NULL) {
223 $output = '<h1>' . t('Hello World') . '</h1>';
225 ctools_include('ajax');
227 $commands[] = ajax_command_html('#ctools-sample', $output);
228 print ajax_render($commands); // this function exits.
237 * Nix a row from a table and restripe.
239 function ctools_ajax_sample_tablenix($js, $row) {
241 // We don't support degrading this from js because we're not
242 // using the server to remember the state of the table.
243 return MENU_ACCESS_DENIED;
245 ctools_include('ajax');
248 $commands[] = ajax_command_remove("tr.ajax-sample-row-$row");
249 $commands[] = ajax_command_restripe("table.ajax-sample-table");
250 print ajax_render($commands);
255 * A modal login callback.
257 function ctools_ajax_sample_login($js = NULL) {
258 // Fall back if $js is not set.
260 return drupal_get_form('user_login');
263 ctools_include('modal');
264 ctools_include('ajax');
266 'title' => t('Login'),
269 $output = ctools_modal_form_wrapper('user_login', $form_state);
270 if (!empty($form_state['executed'])) {
271 // We'll just overwrite the form output if it was successful.
273 $inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account'));
274 $account = ctools_ajax_text_button(t('your account'), 'ctools_ajax_sample/nojs/login/user', t('Go to your account'));
275 $output[] = ctools_modal_command_display(t('Login Success'), '<div class="modal-message">Login successful. You can now choose whether to '. $inplace .', or go to '. $account.'.</div>');
277 print ajax_render($output);
282 * Post-login processor: should we go to the user account or stay in place?
284 function ctools_ajax_sample_login_success($js, $action) {
286 // we should never be here out of ajax context
287 return MENU_NOT_FOUND;
290 ctools_include('ajax');
291 ctools_add_js('ajax-responder');
293 if ($action == 'inplace') {
295 $commands[] = ctools_ajax_command_reload();
299 $commands[] = ctools_ajax_command_redirect('user');
301 print ajax_render($commands);
306 * A modal login callback.
308 function ctools_ajax_sample_animal($js = NULL, $step = NULL) {
310 ctools_include('modal');
311 ctools_include('ajax');
316 'path' => "ctools_ajax_sample/" . ($js ? 'ajax' : 'nojs') . "/animal/%step",
317 'show trail' => TRUE,
319 'show cancel' => TRUE,
320 'show return' => FALSE,
321 'next callback' => 'ctools_ajax_sample_wizard_next',
322 'finish callback' => 'ctools_ajax_sample_wizard_finish',
323 'cancel callback' => 'ctools_ajax_sample_wizard_cancel',
324 // this controls order, as well as form labels
326 'start' => t('Choose animal'),
328 // here we map a step to a form id.
330 // e.g. this for the step at wombat/create
332 'form id' => 'ctools_ajax_sample_start'
337 // We're not using any real storage here, so we're going to set our
338 // object_id to 1. When using wizard forms, id management turns
339 // out to be one of the hardest parts. Editing an object with an id
340 // is easy, but new objects don't usually have ids until somewhere
343 // We skip all this here by just using an id of 1.
348 // We reset the form when $step is NULL because that means they have
349 // for whatever reason started over.
350 ctools_ajax_sample_cache_clear($object_id);
354 // This automatically gets defaults if there wasn't anything saved.
355 $object = ctools_ajax_sample_cache_get($object_id);
357 $animals = ctools_ajax_sample_animals();
359 // Make sure we can't somehow accidentally go to an invalid animal.
360 if (empty($animals[$object->type])) {
361 $object->type = 'unknown';
364 // Now that we have our object, dynamically add the animal's form.
365 if ($object->type == 'unknown') {
366 // If they haven't selected a type, add a form that doesn't exist yet.
367 $form_info['order']['unknown'] = t('Configure animal');
368 $form_info['forms']['unknown'] = array('form id' => 'nothing');
371 // Add the selected animal to the order so that it shows up properly in the trail.
372 $form_info['order'][$object->type] = $animals[$object->type]['config title'];
375 // Make sure all animals forms are represented so that the next stuff can
377 foreach ($animals as $id => $animal) {
378 $form_info['forms'][$id] = array('form id' => $animals[$id]['form']);
383 // Put our object and ID into the form state cache so we can easily find
385 'object_id' => $object_id,
386 'object' => &$object,
389 // Send this all off to our form. This is like drupal_get_form only wizardy.
390 ctools_include('wizard');
391 $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
392 $output = drupal_render($form);
394 if ($output === FALSE || !empty($form_state['complete'])) {
395 // This creates a string based upon the animal and its setting using
396 // function indirection.
397 $animal = $animals[$object->type]['output']($object);
400 // If $output is FALSE, there was no actual form.
402 // If javascript is active, we have to use a render array.
404 if ($output === FALSE || !empty($form_state['complete'])) {
405 // Dismiss the modal.
406 $commands[] = ajax_command_html('#ctools-sample', $animal);
407 $commands[] = ctools_modal_command_dismiss();
409 else if (!empty($form_state['cancel'])) {
410 // If cancelling, return to the activity.
411 $commands[] = ctools_modal_command_dismiss();
414 $commands = ctools_modal_form_render($form_state, $output);
416 print ajax_render($commands);
420 if ($output === FALSE || !empty($form_state['complete'])) {
423 else if (!empty($form_state['cancel'])) {
424 drupal_goto('ctools_ajax_sample');
432 // ---------------------------------------------------------------------------
436 * Theme function for main rendered output.
438 function theme_ctools_ajax_sample_container($vars) {
439 $output = '<div id="ctools-sample">';
440 $output .= $vars['content'];
446 // ---------------------------------------------------------------------------
447 // Stuff needed for our little wizard.
450 * Get a list of our animals and associated forms.
452 * What we're doing is making it easy to add more animals in just one place,
453 * which is often how it will work in the real world. If using CTools, what
454 * you would probably really have, here, is a set of plugins for each animal.
456 function ctools_ajax_sample_animals() {
459 'title' => t('Sheep'),
460 'config title' => t('Configure sheep'),
461 'form' => 'ctools_ajax_sample_configure_sheep',
462 'output' => 'ctools_ajax_sample_show_sheep',
465 'title' => t('Lizard'),
466 'config title' => t('Configure lizard'),
467 'form' => 'ctools_ajax_sample_configure_lizard',
468 'output' => 'ctools_ajax_sample_show_lizard',
471 'title' => t('Raptor'),
472 'config title' => t('Configure raptor'),
473 'form' => 'ctools_ajax_sample_configure_raptor',
474 'output' => 'ctools_ajax_sample_show_raptor',
479 // ---------------------------------------------------------------------------
480 // Wizard caching helpers.
483 * Store our little cache so that we can retain data from form to form.
485 function ctools_ajax_sample_cache_set($id, $object) {
486 ctools_include('object-cache');
487 ctools_object_cache_set('ctools_ajax_sample', $id, $object);
491 * Get the current object from the cache, or default.
493 function ctools_ajax_sample_cache_get($id) {
494 ctools_include('object-cache');
495 $object = ctools_object_cache_get('ctools_ajax_sample', $id);
497 // Create a default object.
498 $object = new stdClass;
499 $object->type = 'unknown';
507 * Clear the wizard cache.
509 function ctools_ajax_sample_cache_clear($id) {
510 ctools_include('object-cache');
511 ctools_object_cache_clear('ctools_ajax_sample', $id);
514 // ---------------------------------------------------------------------------
515 // Wizard in-between helpers; what to do between or after forms.
518 * Handle the 'next' click on the add/edit pane form wizard.
520 * All we need to do is store the updated pane in the cache.
522 function ctools_ajax_sample_wizard_next(&$form_state) {
523 ctools_ajax_sample_cache_set($form_state['object_id'], $form_state['object']);
527 * Handle the 'finish' click on the add/edit pane form wizard.
529 * All we need to do is set a flag so the return can handle adding
532 function ctools_ajax_sample_wizard_finish(&$form_state) {
533 $form_state['complete'] = TRUE;
537 * Handle the 'cancel' click on the add/edit pane form wizard.
539 function ctools_ajax_sample_wizard_cancel(&$form_state) {
540 $form_state['cancel'] = TRUE;
543 // ---------------------------------------------------------------------------
544 // Wizard forms for our simple info collection wizard.
547 * Wizard start form. Choose an animal.
549 function ctools_ajax_sample_start($form, &$form_state) {
550 $form_state['title'] = t('Choose animal');
552 $animals = ctools_ajax_sample_animals();
553 foreach ($animals as $id => $animal) {
554 $options[$id] = $animal['title'];
557 $form['type'] = array(
558 '#title' => t('Choose your animal'),
560 '#options' => $options,
561 '#default_value' => $form_state['object']->type,
569 * They have selected a sheep. Set it.
571 function ctools_ajax_sample_start_submit(&$form, &$form_state) {
572 $form_state['object']->type = $form_state['values']['type'];
573 // Override where to go next based on the animal selected.
574 $form_state['clicked_button']['#next'] = $form_state['values']['type'];
578 * Wizard form to configure your sheep.
580 function ctools_ajax_sample_configure_sheep($form, &$form_state) {
581 $form_state['title'] = t('Configure sheep');
583 $form['name'] = array(
584 '#type' => 'textfield',
585 '#title' => t('Name your sheep'),
586 '#default_value' => $form_state['object']->name,
590 $form['sheep'] = array(
591 '#title' => t('What kind of sheep'),
594 t('Wensleydale') => t('Wensleydale'),
595 t('Merino') => t('Merino'),
596 t('Corriedale') => t('Coriedale'),
598 '#default_value' => !empty($form_state['object']->sheep) ? $form_state['object']->sheep : '',
605 * Submit the sheep and store the values from the form.
607 function ctools_ajax_sample_configure_sheep_submit(&$form, &$form_state) {
608 $form_state['object']->name = $form_state['values']['name'];
609 $form_state['object']->sheep = $form_state['values']['sheep'];
613 * Provide some output for our sheep.
615 function ctools_ajax_sample_show_sheep($object) {
616 return t('You have a @type sheep named "@name".', array(
617 '@type' => $object->sheep,
618 '@name' => $object->name,
623 * Wizard form to configure your lizard.
625 function ctools_ajax_sample_configure_lizard($form, &$form_state) {
626 $form_state['title'] = t('Configure lizard');
628 $form['name'] = array(
629 '#type' => 'textfield',
630 '#title' => t('Name your lizard'),
631 '#default_value' => $form_state['object']->name,
635 $form['lizard'] = array(
636 '#title' => t('Venomous'),
637 '#type' => 'checkbox',
638 '#default_value' => !empty($form_state['object']->lizard),
644 * Submit the lizard and store the values from the form.
646 function ctools_ajax_sample_configure_lizard_submit(&$form, &$form_state) {
647 $form_state['object']->name = $form_state['values']['name'];
648 $form_state['object']->lizard = $form_state['values']['lizard'];
652 * Provide some output for our raptor.
654 function ctools_ajax_sample_show_lizard($object) {
655 return t('You have a @type lizard named "@name".', array(
656 '@type' => empty($object->lizard) ? t('non-venomous') : t('venomous'),
657 '@name' => $object->name,
662 * Wizard form to configure your raptor.
664 function ctools_ajax_sample_configure_raptor($form, &$form_state) {
665 $form_state['title'] = t('Configure raptor');
667 $form['name'] = array(
668 '#type' => 'textfield',
669 '#title' => t('Name your raptor'),
670 '#default_value' => $form_state['object']->name,
674 $form['raptor'] = array(
675 '#title' => t('What kind of raptor'),
678 t('Eagle') => t('Eagle'),
679 t('Hawk') => t('Hawk'),
680 t('Owl') => t('Owl'),
681 t('Buzzard') => t('Buzzard'),
683 '#default_value' => !empty($form_state['object']->raptor) ? $form_state['object']->raptor : '',
687 $form['domesticated'] = array(
688 '#title' => t('Domesticated'),
689 '#type' => 'checkbox',
690 '#default_value' => !empty($form_state['object']->domesticated),
696 * Submit the raptor and store the values from the form.
698 function ctools_ajax_sample_configure_raptor_submit(&$form, &$form_state) {
699 $form_state['object']->name = $form_state['values']['name'];
700 $form_state['object']->raptor = $form_state['values']['raptor'];
701 $form_state['object']->domesticated = $form_state['values']['domesticated'];
705 * Provide some output for our raptor.
707 function ctools_ajax_sample_show_raptor($object) {
708 return t('You have a @type @raptor named "@name".', array(
709 '@type' => empty($object->domesticated) ? t('wild') : t('domesticated'),
710 '@raptor' => $object->raptor,
711 '@name' => $object->name,
716 * Helper function to provide a sample jump menu form
718 function ctools_ajax_sample_jump_menu_form() {
719 $url = url('ctools_ajax_sample/jumped');
720 $form_state = array();
721 $form = ctools_jump_menu(array(), $form_state, array($url => t('Jump!')), array());
726 * Provide a message to the user that the jump menu worked
728 function ctools_ajax_sample_jump_menu_page() {
729 $return_link = l(t('Return to the examples page.'), 'ctools_ajax_sample');
730 $output = t('You successfully jumped! !return_link', array('!return_link' => $return_link));
735 * Provide a form for an example ajax modal button
737 function ctools_ajax_sample_ajax_button_form() {
740 $form['url'] = array(
742 // The name of the class is the #id of $form['ajax_button'] with "-url"
744 '#attributes' => array('class' => array('ctools-ajax-sample-button-url')),
745 '#value' => url('ctools_ajax_sample/nojs/animal'),
748 $form['ajax_button'] = array(
750 '#value' => 'Wizard (button modal)',
751 '#attributes' => array('class' => array('ctools-use-modal')),
752 '#id' => 'ctools-ajax-sample-button',