ctools_ajax_sample.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. <?php
  2. /**
  3. * @file
  4. * Sample AJAX functionality so people can see some of the CTools AJAX
  5. * features in use.
  6. */
  7. // ---------------------------------------------------------------------------
  8. // Drupal hooks.
  9. /**
  10. * Implementation of hook_menu()
  11. */
  12. function ctools_ajax_sample_menu() {
  13. $items['ctools_ajax_sample'] = array(
  14. 'title' => 'Chaos Tools AJAX Demo',
  15. 'page callback' => 'ctools_ajax_sample_page',
  16. 'access callback' => TRUE,
  17. 'type' => MENU_NORMAL_ITEM,
  18. );
  19. $items['ctools_ajax_sample/simple_form'] = array(
  20. 'title' => 'Simple Form',
  21. 'page callback' => 'ctools_ajax_simple_form',
  22. 'access callback' => TRUE,
  23. 'type' => MENU_CALLBACK,
  24. );
  25. $items['ctools_ajax_sample/%ctools_js/hello'] = array(
  26. 'title' => 'Hello World',
  27. 'page callback' => 'ctools_ajax_sample_hello',
  28. 'page arguments' => array(1),
  29. 'access callback' => TRUE,
  30. 'type' => MENU_CALLBACK,
  31. );
  32. $items['ctools_ajax_sample/%ctools_js/tablenix/%'] = array(
  33. 'title' => 'Hello World',
  34. 'page callback' => 'ctools_ajax_sample_tablenix',
  35. 'page arguments' => array(1, 3),
  36. 'access callback' => TRUE,
  37. 'type' => MENU_CALLBACK,
  38. );
  39. $items['ctools_ajax_sample/%ctools_js/login'] = array(
  40. 'title' => 'Login',
  41. 'page callback' => 'ctools_ajax_sample_login',
  42. 'page arguments' => array(1),
  43. 'access callback' => TRUE,
  44. 'type' => MENU_CALLBACK,
  45. );
  46. $items['ctools_ajax_sample/%ctools_js/animal'] = array(
  47. 'title' => 'Animal',
  48. 'page callback' => 'ctools_ajax_sample_animal',
  49. 'page arguments' => array(1),
  50. 'access callback' => TRUE,
  51. 'type' => MENU_CALLBACK,
  52. );
  53. $items['ctools_ajax_sample/%ctools_js/login/%'] = array(
  54. 'title' => 'Post-Login Action',
  55. 'page callback' => 'ctools_ajax_sample_login_success',
  56. 'page arguments' => array(1, 3),
  57. 'access callback' => TRUE,
  58. 'type' => MENU_CALLBACK,
  59. );
  60. $items['ctools_ajax_sample/jumped'] = array(
  61. 'title' => 'Successful Jumping',
  62. 'page callback' => 'ctools_ajax_sample_jump_menu_page',
  63. 'access callback' => TRUE,
  64. 'type' => MENU_NORMAL_ITEM,
  65. );
  66. return $items;
  67. }
  68. function ctools_ajax_simple_form() {
  69. ctools_include('content');
  70. ctools_include('context');
  71. $node = node_load(1);
  72. $context = ctools_context_create('node', $node);
  73. $context = array('context_node_1' => $context);
  74. return ctools_content_render('node_comment_form', 'node_comment_form', ctools_ajax_simple_form_pane(), array(), array(), $context);
  75. }
  76. function ctools_ajax_simple_form_pane() {
  77. $configuration = array(
  78. 'anon_links' => 0,
  79. 'context' => 'context_node_1',
  80. 'override_title' => 0,
  81. 'override_title_text' => '',
  82. );
  83. return $configuration;
  84. }
  85. /**
  86. * Implementation of hook_theme()
  87. *
  88. * Render some basic output for this module.
  89. */
  90. function ctools_ajax_sample_theme() {
  91. return array(
  92. // Sample theme functions.
  93. 'ctools_ajax_sample_container' => array(
  94. 'arguments' => array('content' => NULL),
  95. ),
  96. );
  97. }
  98. // ---------------------------------------------------------------------------
  99. // Page callbacks.
  100. /**
  101. * Page callback to display links and render a container for AJAX stuff.
  102. */
  103. function ctools_ajax_sample_page() {
  104. global $user;
  105. // Include the CTools tools that we need.
  106. ctools_include('ajax');
  107. ctools_include('modal');
  108. // Add CTools' javascript to the page.
  109. ctools_modal_add_js();
  110. // Create our own javascript that will be used to theme a modal.
  111. $sample_style = array(
  112. 'ctools-sample-style' => array(
  113. 'modalSize' => array(
  114. 'type' => 'fixed',
  115. 'width' => 500,
  116. 'height' => 300,
  117. 'addWidth' => 20,
  118. 'addHeight' => 15,
  119. ),
  120. 'modalOptions' => array(
  121. 'opacity' => .5,
  122. 'background-color' => '#000',
  123. ),
  124. 'animation' => 'fadeIn',
  125. 'modalTheme' => 'CToolsSampleModal',
  126. 'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'), 'alt' => t('Loading...'), 'title' => t('Loading'))),
  127. ),
  128. );
  129. drupal_add_js($sample_style, 'setting');
  130. // Since we have our js, css and images in well-known named directories,
  131. // CTools makes it easy for us to just use them without worrying about
  132. // using drupal_get_path() and all that ugliness.
  133. ctools_add_js('ctools-ajax-sample', 'ctools_ajax_sample');
  134. ctools_add_css('ctools-ajax-sample', 'ctools_ajax_sample');
  135. // Create a list of clickable links.
  136. $links = array();
  137. // Only show login links to the anonymous user.
  138. if ($user->uid == 0) {
  139. $links[] = ctools_modal_text_button(t('Modal Login (default style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'));
  140. // The extra class points to the info in ctools-sample-style which we added
  141. // to the settings, prefixed with 'ctools-modal'.
  142. $links[] = ctools_modal_text_button(t('Modal Login (custom style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'), 'ctools-modal-ctools-sample-style');
  143. }
  144. // Four ways to do our animal picking wizard.
  145. $button_form = ctools_ajax_sample_ajax_button_form();
  146. $links[] = l(t('Wizard (no modal)'), 'ctools_ajax_sample/nojs/animal');
  147. $links[] = ctools_modal_text_button(t('Wizard (default modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'));
  148. $links[] = ctools_modal_text_button(t('Wizard (custom modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'), 'ctools-modal-ctools-sample-style');
  149. $links[] = drupal_render($button_form);
  150. $links[] = ctools_ajax_text_button(t('Hello world!'), "ctools_ajax_sample/nojs/hello", t('Replace text with "hello world"'));
  151. $output = theme('item_list', array('items' => $links, 'title' => t('Actions')));
  152. // This container will have data AJAXed into it.
  153. $output .= theme('ctools_ajax_sample_container', array('content' => '<h1>' . t('Sample Content') . '</h1>'));
  154. // Create a table that we can have data removed from via AJAX.
  155. $header = array(t('Row'), t('Content'), t('Actions'));
  156. $rows = array();
  157. for ($i = 1; $i < 11; $i++) {
  158. $rows[] = array(
  159. 'class' => array('ajax-sample-row-' . $i),
  160. 'data' => array(
  161. $i,
  162. md5($i),
  163. ctools_ajax_text_button("remove", "ctools_ajax_sample/nojs/tablenix/$i", t('Delete this row')),
  164. ),
  165. );
  166. }
  167. $output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('ajax-sample-table'))));
  168. // Show examples of ctools javascript widgets.
  169. $output .= '<h2>' . t('CTools Javascript Widgets') . '</h2>';
  170. // Create a drop down menu.
  171. $links = array();
  172. $links[] = array('title' => t('Link 1'), 'href' => $_GET['q']);
  173. $links[] = array('title' => t('Link 2'), 'href' => $_GET['q']);
  174. $links[] = array('title' => t('Link 3'), 'href' => $_GET['q']);
  175. $output .= '<h3>' . t('Drop Down Menu') . '</h3>';
  176. $output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links));
  177. // Create a collapsible div.
  178. $handle = t('Click to Collapse');
  179. $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.';
  180. $output .= '<h3>' . t('Collapsible Div') . '</h3>';
  181. $output .= theme('ctools_collapsible', array('handle' => $handle, 'content' => $content, 'collapsed' => FALSE));
  182. // Create a jump menu.
  183. ctools_include('jump-menu');
  184. $form = drupal_get_form('ctools_ajax_sample_jump_menu_form');
  185. $output .= '<h3>' . t('Jump Menu') . '</h3>';
  186. $output .= drupal_render($form);
  187. return array('markup' => array('#markup' => $output));
  188. }
  189. /**
  190. * Returns a "take it all over" hello world style request.
  191. */
  192. function ctools_ajax_sample_hello($js = NULL) {
  193. $output = '<h1>' . t('Hello World') . '</h1>';
  194. if ($js) {
  195. ctools_include('ajax');
  196. $commands = array();
  197. $commands[] = ajax_command_html('#ctools-sample', $output);
  198. // This function exits.
  199. print ajax_render($commands);
  200. exit;
  201. }
  202. else {
  203. return $output;
  204. }
  205. }
  206. /**
  207. * Nix a row from a table and restripe.
  208. */
  209. function ctools_ajax_sample_tablenix($js, $row) {
  210. if (!$js) {
  211. // We don't support degrading this from js because we're not
  212. // using the server to remember the state of the table.
  213. return MENU_ACCESS_DENIED;
  214. }
  215. ctools_include('ajax');
  216. $commands = array();
  217. $commands[] = ajax_command_remove("tr.ajax-sample-row-$row");
  218. $commands[] = ajax_command_restripe("table.ajax-sample-table");
  219. print ajax_render($commands);
  220. exit;
  221. }
  222. /**
  223. * A modal login callback.
  224. */
  225. function ctools_ajax_sample_login($js = NULL) {
  226. // Fall back if $js is not set.
  227. if (!$js) {
  228. return drupal_get_form('user_login');
  229. }
  230. ctools_include('modal');
  231. ctools_include('ajax');
  232. $form_state = array(
  233. 'title' => t('Login'),
  234. 'ajax' => TRUE,
  235. );
  236. $output = ctools_modal_form_wrapper('user_login', $form_state);
  237. if (!empty($form_state['executed'])) {
  238. // We'll just overwrite the form output if it was successful.
  239. $output = array();
  240. $inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account'));
  241. $account = ctools_ajax_text_button(t('your account'), 'ctools_ajax_sample/nojs/login/user', t('Go to your account'));
  242. $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>');
  243. }
  244. print ajax_render($output);
  245. exit;
  246. }
  247. /**
  248. * Post-login processor: should we go to the user account or stay in place?
  249. */
  250. function ctools_ajax_sample_login_success($js, $action) {
  251. if (!$js) {
  252. // We should never be here out of ajax context.
  253. return MENU_NOT_FOUND;
  254. }
  255. ctools_include('ajax');
  256. ctools_add_js('ajax-responder');
  257. $commands = array();
  258. if ($action == 'inplace') {
  259. // Stay here.
  260. $commands[] = ctools_ajax_command_reload();
  261. }
  262. else {
  263. // Bounce bounce.
  264. $commands[] = ctools_ajax_command_redirect('user');
  265. }
  266. print ajax_render($commands);
  267. exit;
  268. }
  269. /**
  270. * A modal login callback.
  271. */
  272. function ctools_ajax_sample_animal($js = NULL, $step = NULL) {
  273. if ($js) {
  274. ctools_include('modal');
  275. ctools_include('ajax');
  276. }
  277. $form_info = array(
  278. 'id' => 'animals',
  279. 'path' => "ctools_ajax_sample/" . ($js ? 'ajax' : 'nojs') . "/animal/%step",
  280. 'show trail' => TRUE,
  281. 'show back' => TRUE,
  282. 'show cancel' => TRUE,
  283. 'show return' => FALSE,
  284. 'next callback' => 'ctools_ajax_sample_wizard_next',
  285. 'finish callback' => 'ctools_ajax_sample_wizard_finish',
  286. 'cancel callback' => 'ctools_ajax_sample_wizard_cancel',
  287. // This controls order, as well as form labels.
  288. 'order' => array(
  289. 'start' => t('Choose animal'),
  290. ),
  291. // Here we map a step to a form id.
  292. 'forms' => array(
  293. // e.g. this for the step at wombat/create.
  294. 'start' => array(
  295. 'form id' => 'ctools_ajax_sample_start',
  296. ),
  297. ),
  298. );
  299. // We're not using any real storage here, so we're going to set our
  300. // object_id to 1. When using wizard forms, id management turns
  301. // out to be one of the hardest parts. Editing an object with an id
  302. // is easy, but new objects don't usually have ids until somewhere
  303. // in creation.
  304. //
  305. // We skip all this here by just using an id of 1.
  306. $object_id = 1;
  307. if (empty($step)) {
  308. // We reset the form when $step is NULL because that means they have
  309. // for whatever reason started over.
  310. ctools_ajax_sample_cache_clear($object_id);
  311. $step = 'start';
  312. }
  313. // This automatically gets defaults if there wasn't anything saved.
  314. $object = ctools_ajax_sample_cache_get($object_id);
  315. $animals = ctools_ajax_sample_animals();
  316. // Make sure we can't somehow accidentally go to an invalid animal.
  317. if (empty($animals[$object->type])) {
  318. $object->type = 'unknown';
  319. }
  320. // Now that we have our object, dynamically add the animal's form.
  321. if ($object->type == 'unknown') {
  322. // If they haven't selected a type, add a form that doesn't exist yet.
  323. $form_info['order']['unknown'] = t('Configure animal');
  324. $form_info['forms']['unknown'] = array('form id' => 'nothing');
  325. }
  326. else {
  327. // Add the selected animal to the order so that it shows up properly in the trail.
  328. $form_info['order'][$object->type] = $animals[$object->type]['config title'];
  329. }
  330. // Make sure all animals forms are represented so that the next stuff can
  331. // work correctly:
  332. foreach ($animals as $id => $animal) {
  333. $form_info['forms'][$id] = array('form id' => $animals[$id]['form']);
  334. }
  335. $form_state = array(
  336. 'ajax' => $js,
  337. // Put our object and ID into the form state cache so we can easily find
  338. // it.
  339. 'object_id' => $object_id,
  340. 'object' => &$object,
  341. );
  342. // Send this all off to our form. This is like drupal_get_form only wizardy.
  343. ctools_include('wizard');
  344. $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
  345. $output = drupal_render($form);
  346. if ($output === FALSE || !empty($form_state['complete'])) {
  347. // This creates a string based upon the animal and its setting using
  348. // function indirection.
  349. $animal = $animals[$object->type]['output']($object);
  350. }
  351. // If $output is FALSE, there was no actual form.
  352. if ($js) {
  353. // If javascript is active, we have to use a render array.
  354. $commands = array();
  355. if ($output === FALSE || !empty($form_state['complete'])) {
  356. // Dismiss the modal.
  357. $commands[] = ajax_command_html('#ctools-sample', $animal);
  358. $commands[] = ctools_modal_command_dismiss();
  359. }
  360. elseif (!empty($form_state['cancel'])) {
  361. // If cancelling, return to the activity.
  362. $commands[] = ctools_modal_command_dismiss();
  363. }
  364. else {
  365. $commands = ctools_modal_form_render($form_state, $output);
  366. }
  367. print ajax_render($commands);
  368. exit;
  369. }
  370. else {
  371. if ($output === FALSE || !empty($form_state['complete'])) {
  372. return $animal;
  373. }
  374. elseif (!empty($form_state['cancel'])) {
  375. drupal_goto('ctools_ajax_sample');
  376. }
  377. else {
  378. return $output;
  379. }
  380. }
  381. }
  382. // ---------------------------------------------------------------------------
  383. // Themes.
  384. /**
  385. * Theme function for main rendered output.
  386. */
  387. function theme_ctools_ajax_sample_container($vars) {
  388. $output = '<div id="ctools-sample">';
  389. $output .= $vars['content'];
  390. $output .= '</div>';
  391. return $output;
  392. }
  393. // ---------------------------------------------------------------------------
  394. // Stuff needed for our little wizard.
  395. /**
  396. * Get a list of our animals and associated forms.
  397. *
  398. * What we're doing is making it easy to add more animals in just one place,
  399. * which is often how it will work in the real world. If using CTools, what
  400. * you would probably really have, here, is a set of plugins for each animal.
  401. */
  402. function ctools_ajax_sample_animals() {
  403. return array(
  404. 'sheep' => array(
  405. 'title' => t('Sheep'),
  406. 'config title' => t('Configure sheep'),
  407. 'form' => 'ctools_ajax_sample_configure_sheep',
  408. 'output' => 'ctools_ajax_sample_show_sheep',
  409. ),
  410. 'lizard' => array(
  411. 'title' => t('Lizard'),
  412. 'config title' => t('Configure lizard'),
  413. 'form' => 'ctools_ajax_sample_configure_lizard',
  414. 'output' => 'ctools_ajax_sample_show_lizard',
  415. ),
  416. 'raptor' => array(
  417. 'title' => t('Raptor'),
  418. 'config title' => t('Configure raptor'),
  419. 'form' => 'ctools_ajax_sample_configure_raptor',
  420. 'output' => 'ctools_ajax_sample_show_raptor',
  421. ),
  422. );
  423. }
  424. // ---------------------------------------------------------------------------
  425. // Wizard caching helpers.
  426. /**
  427. * Store our little cache so that we can retain data from form to form.
  428. */
  429. function ctools_ajax_sample_cache_set($id, $object) {
  430. ctools_include('object-cache');
  431. ctools_object_cache_set('ctools_ajax_sample', $id, $object);
  432. }
  433. /**
  434. * Get the current object from the cache, or default.
  435. */
  436. function ctools_ajax_sample_cache_get($id) {
  437. ctools_include('object-cache');
  438. $object = ctools_object_cache_get('ctools_ajax_sample', $id);
  439. if (!$object) {
  440. // Create a default object.
  441. $object = new stdClass();
  442. $object->type = 'unknown';
  443. $object->name = '';
  444. }
  445. return $object;
  446. }
  447. /**
  448. * Clear the wizard cache.
  449. */
  450. function ctools_ajax_sample_cache_clear($id) {
  451. ctools_include('object-cache');
  452. ctools_object_cache_clear('ctools_ajax_sample', $id);
  453. }
  454. // ---------------------------------------------------------------------------
  455. // Wizard in-between helpers; what to do between or after forms.
  456. /**
  457. * Handle the 'next' click on the add/edit pane form wizard.
  458. *
  459. * All we need to do is store the updated pane in the cache.
  460. */
  461. function ctools_ajax_sample_wizard_next(&$form_state) {
  462. ctools_ajax_sample_cache_set($form_state['object_id'], $form_state['object']);
  463. }
  464. /**
  465. * Handle the 'finish' click on the add/edit pane form wizard.
  466. *
  467. * All we need to do is set a flag so the return can handle adding
  468. * the pane.
  469. */
  470. function ctools_ajax_sample_wizard_finish(&$form_state) {
  471. $form_state['complete'] = TRUE;
  472. }
  473. /**
  474. * Handle the 'cancel' click on the add/edit pane form wizard.
  475. */
  476. function ctools_ajax_sample_wizard_cancel(&$form_state) {
  477. $form_state['cancel'] = TRUE;
  478. }
  479. // ---------------------------------------------------------------------------
  480. // Wizard forms for our simple info collection wizard.
  481. /**
  482. * Wizard start form. Choose an animal.
  483. */
  484. function ctools_ajax_sample_start($form, &$form_state) {
  485. $form_state['title'] = t('Choose animal');
  486. $animals = ctools_ajax_sample_animals();
  487. foreach ($animals as $id => $animal) {
  488. $options[$id] = $animal['title'];
  489. }
  490. $form['type'] = array(
  491. '#title' => t('Choose your animal'),
  492. '#type' => 'radios',
  493. '#options' => $options,
  494. '#default_value' => $form_state['object']->type,
  495. '#required' => TRUE,
  496. );
  497. return $form;
  498. }
  499. /**
  500. * They have selected a sheep. Set it.
  501. */
  502. function ctools_ajax_sample_start_submit(&$form, &$form_state) {
  503. $form_state['object']->type = $form_state['values']['type'];
  504. // Override where to go next based on the animal selected.
  505. $form_state['clicked_button']['#next'] = $form_state['values']['type'];
  506. }
  507. /**
  508. * Wizard form to configure your sheep.
  509. */
  510. function ctools_ajax_sample_configure_sheep($form, &$form_state) {
  511. $form_state['title'] = t('Configure sheep');
  512. $form['name'] = array(
  513. '#type' => 'textfield',
  514. '#title' => t('Name your sheep'),
  515. '#default_value' => $form_state['object']->name,
  516. '#required' => TRUE,
  517. );
  518. $form['sheep'] = array(
  519. '#title' => t('What kind of sheep'),
  520. '#type' => 'radios',
  521. '#options' => array(
  522. t('Wensleydale') => t('Wensleydale'),
  523. t('Merino') => t('Merino'),
  524. t('Corriedale') => t('Coriedale'),
  525. ),
  526. '#default_value' => !empty($form_state['object']->sheep) ? $form_state['object']->sheep : '',
  527. '#required' => TRUE,
  528. );
  529. return $form;
  530. }
  531. /**
  532. * Submit the sheep and store the values from the form.
  533. */
  534. function ctools_ajax_sample_configure_sheep_submit(&$form, &$form_state) {
  535. $form_state['object']->name = $form_state['values']['name'];
  536. $form_state['object']->sheep = $form_state['values']['sheep'];
  537. }
  538. /**
  539. * Provide some output for our sheep.
  540. */
  541. function ctools_ajax_sample_show_sheep($object) {
  542. return t('You have a @type sheep named "@name".', array(
  543. '@type' => $object->sheep,
  544. '@name' => $object->name,
  545. ));
  546. }
  547. /**
  548. * Wizard form to configure your lizard.
  549. */
  550. function ctools_ajax_sample_configure_lizard($form, &$form_state) {
  551. $form_state['title'] = t('Configure lizard');
  552. $form['name'] = array(
  553. '#type' => 'textfield',
  554. '#title' => t('Name your lizard'),
  555. '#default_value' => $form_state['object']->name,
  556. '#required' => TRUE,
  557. );
  558. $form['lizard'] = array(
  559. '#title' => t('Venomous'),
  560. '#type' => 'checkbox',
  561. '#default_value' => !empty($form_state['object']->lizard),
  562. );
  563. return $form;
  564. }
  565. /**
  566. * Submit the lizard and store the values from the form.
  567. */
  568. function ctools_ajax_sample_configure_lizard_submit(&$form, &$form_state) {
  569. $form_state['object']->name = $form_state['values']['name'];
  570. $form_state['object']->lizard = $form_state['values']['lizard'];
  571. }
  572. /**
  573. * Provide some output for our raptor.
  574. */
  575. function ctools_ajax_sample_show_lizard($object) {
  576. return t('You have a @type lizard named "@name".', array(
  577. '@type' => empty($object->lizard) ? t('non-venomous') : t('venomous'),
  578. '@name' => $object->name,
  579. ));
  580. }
  581. /**
  582. * Wizard form to configure your raptor.
  583. */
  584. function ctools_ajax_sample_configure_raptor($form, &$form_state) {
  585. $form_state['title'] = t('Configure raptor');
  586. $form['name'] = array(
  587. '#type' => 'textfield',
  588. '#title' => t('Name your raptor'),
  589. '#default_value' => $form_state['object']->name,
  590. '#required' => TRUE,
  591. );
  592. $form['raptor'] = array(
  593. '#title' => t('What kind of raptor'),
  594. '#type' => 'radios',
  595. '#options' => array(
  596. t('Eagle') => t('Eagle'),
  597. t('Hawk') => t('Hawk'),
  598. t('Owl') => t('Owl'),
  599. t('Buzzard') => t('Buzzard'),
  600. ),
  601. '#default_value' => !empty($form_state['object']->raptor) ? $form_state['object']->raptor : '',
  602. '#required' => TRUE,
  603. );
  604. $form['domesticated'] = array(
  605. '#title' => t('Domesticated'),
  606. '#type' => 'checkbox',
  607. '#default_value' => !empty($form_state['object']->domesticated),
  608. );
  609. return $form;
  610. }
  611. /**
  612. * Submit the raptor and store the values from the form.
  613. */
  614. function ctools_ajax_sample_configure_raptor_submit(&$form, &$form_state) {
  615. $form_state['object']->name = $form_state['values']['name'];
  616. $form_state['object']->raptor = $form_state['values']['raptor'];
  617. $form_state['object']->domesticated = $form_state['values']['domesticated'];
  618. }
  619. /**
  620. * Provide some output for our raptor.
  621. */
  622. function ctools_ajax_sample_show_raptor($object) {
  623. return t('You have a @type @raptor named "@name".', array(
  624. '@type' => empty($object->domesticated) ? t('wild') : t('domesticated'),
  625. '@raptor' => $object->raptor,
  626. '@name' => $object->name,
  627. ));
  628. }
  629. /**
  630. * Helper function to provide a sample jump menu form.
  631. */
  632. function ctools_ajax_sample_jump_menu_form() {
  633. $url = url('ctools_ajax_sample/jumped');
  634. $form_state = array();
  635. $form = ctools_jump_menu(array(), $form_state, array($url => t('Jump!')), array());
  636. return $form;
  637. }
  638. /**
  639. * Provide a message to the user that the jump menu worked.
  640. */
  641. function ctools_ajax_sample_jump_menu_page() {
  642. $return_link = l(t('Return to the examples page.'), 'ctools_ajax_sample');
  643. $output = t('You successfully jumped! !return_link', array('!return_link' => $return_link));
  644. return $output;
  645. }
  646. /**
  647. * Provide a form for an example ajax modal button.
  648. */
  649. function ctools_ajax_sample_ajax_button_form() {
  650. $form = array();
  651. $form['url'] = array(
  652. '#type' => 'hidden',
  653. // The name of the class is the #id of $form['ajax_button'] with "-url"
  654. // suffix.
  655. '#attributes' => array('class' => array('ctools-ajax-sample-button-url')),
  656. '#value' => url('ctools_ajax_sample/nojs/animal'),
  657. );
  658. $form['ajax_button'] = array(
  659. '#type' => 'button',
  660. '#value' => 'Wizard (button modal)',
  661. '#attributes' => array('class' => array('ctools-use-modal')),
  662. '#id' => 'ctools-ajax-sample-button',
  663. );
  664. return $form;
  665. }