form_example.module 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @file
  4. * Examples demonstrating the Drupal Form API.
  5. */
  6. /**
  7. * @defgroup form_example Example: Form API
  8. * @ingroup examples
  9. * @{
  10. * Examples demonstrating the Drupal Form API.
  11. *
  12. * The Form Example module is a part of the Examples for Developers Project
  13. * and provides various Drupal Form API Examples. You can download and
  14. * experiment with this code at the
  15. * @link http://drupal.org/project/examples Examples for Developers project page. @endlink
  16. */
  17. /**
  18. * Implements hook_menu().
  19. *
  20. * Here we set up the URLs (menu entries) for the
  21. * form examples. Note that most of the menu items
  22. * have page callbacks and page arguments set, with
  23. * page arguments set to be functions in external files.
  24. */
  25. function form_example_menu() {
  26. $items = array();
  27. $items['examples/form_example'] = array(
  28. 'title' => 'Form Example',
  29. 'page callback' => 'form_example_intro',
  30. 'access callback' => TRUE,
  31. 'expanded' => TRUE,
  32. );
  33. $items['examples/form_example/tutorial'] = array(
  34. 'title' => 'Form Tutorial',
  35. 'page callback' => 'drupal_get_form',
  36. 'page arguments' => array('form_example_tutorial_1'),
  37. 'access callback' => TRUE,
  38. 'description' => 'A set of ten tutorials',
  39. 'file' => 'form_example_tutorial.inc',
  40. 'type' => MENU_NORMAL_ITEM,
  41. );
  42. $items['examples/form_example/tutorial/1'] = array(
  43. 'title' => '#1',
  44. 'page callback' => 'drupal_get_form',
  45. 'page arguments' => array('form_example_tutorial_1'),
  46. 'access callback' => TRUE,
  47. 'description' => 'Tutorial 1: Simplest form',
  48. 'type' => MENU_DEFAULT_LOCAL_TASK,
  49. 'file' => 'form_example_tutorial.inc',
  50. );
  51. $items['examples/form_example/tutorial/2'] = array(
  52. 'title' => '#2',
  53. 'page callback' => 'drupal_get_form',
  54. 'page arguments' => array('form_example_tutorial_2'),
  55. 'access callback' => TRUE,
  56. 'description' => 'Tutorial 2: Form with a submit button',
  57. 'type' => MENU_LOCAL_TASK,
  58. 'file' => 'form_example_tutorial.inc',
  59. );
  60. $items['examples/form_example/tutorial/3'] = array(
  61. 'title' => '#3',
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('form_example_tutorial_3'),
  64. 'access callback' => TRUE,
  65. 'description' => 'Tutorial 3: Fieldsets',
  66. 'type' => MENU_LOCAL_TASK,
  67. 'file' => 'form_example_tutorial.inc',
  68. );
  69. $items['examples/form_example/tutorial/4'] = array(
  70. 'title' => '#4',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array('form_example_tutorial_4'),
  73. 'access callback' => TRUE,
  74. 'description' => 'Tutorial 4: Required fields',
  75. 'type' => MENU_LOCAL_TASK,
  76. 'file' => 'form_example_tutorial.inc',
  77. );
  78. $items['examples/form_example/tutorial/5'] = array(
  79. 'title' => '#5',
  80. 'page callback' => 'drupal_get_form',
  81. 'page arguments' => array('form_example_tutorial_5'),
  82. 'access callback' => TRUE,
  83. 'description' => 'Tutorial 5: More element attributes',
  84. 'type' => MENU_LOCAL_TASK,
  85. 'file' => 'form_example_tutorial.inc',
  86. );
  87. $items['examples/form_example/tutorial/6'] = array(
  88. 'title' => '#6',
  89. 'page callback' => 'drupal_get_form',
  90. 'page arguments' => array('form_example_tutorial_6'),
  91. 'access callback' => TRUE,
  92. 'description' => 'Tutorial 6: Form with a validate handler',
  93. 'type' => MENU_LOCAL_TASK,
  94. 'file' => 'form_example_tutorial.inc',
  95. );
  96. $items['examples/form_example/tutorial/7'] = array(
  97. 'title' => '#7',
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('form_example_tutorial_7'),
  100. 'access callback' => TRUE,
  101. 'description' => 'Tutorial 7: Form with a submit handler',
  102. 'type' => MENU_LOCAL_TASK,
  103. 'file' => 'form_example_tutorial.inc',
  104. );
  105. $items['examples/form_example/tutorial/8'] = array(
  106. 'title' => '#8',
  107. 'page callback' => 'drupal_get_form',
  108. 'page arguments' => array('form_example_tutorial_8'),
  109. 'access callback' => TRUE,
  110. 'description' => 'Tutorial 8: Basic multistep form',
  111. 'type' => MENU_LOCAL_TASK,
  112. 'file' => 'form_example_tutorial.inc',
  113. );
  114. $items['examples/form_example/tutorial/9'] = array(
  115. 'title' => '#9',
  116. 'page callback' => 'drupal_get_form',
  117. 'page arguments' => array('form_example_tutorial_9'),
  118. 'access callback' => TRUE,
  119. 'description' => 'Tutorial 9: Form with dynamically added new fields',
  120. 'type' => MENU_LOCAL_TASK,
  121. 'file' => 'form_example_tutorial.inc',
  122. 'weight' => 9,
  123. );
  124. $items['examples/form_example/tutorial/10'] = array(
  125. 'title' => '#10',
  126. 'page callback' => 'drupal_get_form',
  127. 'page arguments' => array('form_example_tutorial_10'),
  128. 'access callback' => TRUE,
  129. 'description' => 'Tutorial 10: Form with file upload',
  130. 'type' => MENU_LOCAL_TASK,
  131. 'file' => 'form_example_tutorial.inc',
  132. 'weight' => 10,
  133. );
  134. $items['examples/form_example/tutorial/11'] = array(
  135. 'title' => '#11',
  136. 'page callback' => 'drupal_get_form',
  137. 'page arguments' => array('form_example_tutorial_11'),
  138. 'access callback' => TRUE,
  139. 'description' => 'Tutorial 11: generating a confirmation form',
  140. 'type' => MENU_LOCAL_TASK,
  141. 'file' => 'form_example_tutorial.inc',
  142. 'weight' => 11,
  143. );
  144. $items['examples/form_example/tutorial/11/confirm/%'] = array(
  145. 'title' => 'Name Confirmation',
  146. 'page callback' => 'drupal_get_form',
  147. 'page arguments' => array('form_example_tutorial_11_confirm_name', 5),
  148. 'access callback' => TRUE,
  149. 'description' => 'Confirmation form for tutorial 11. Generated using the confirm_form function',
  150. 'file' => 'form_example_tutorial.inc',
  151. );
  152. $items['examples/form_example/states'] = array(
  153. 'title' => '#states example',
  154. 'page callback' => 'drupal_get_form',
  155. 'page arguments' => array('form_example_states_form'),
  156. 'access callback' => TRUE,
  157. 'description' => 'How to use the #states attribute in FAPI',
  158. 'file' => 'form_example_states.inc',
  159. );
  160. $items['examples/form_example/wizard'] = array(
  161. 'title' => 'Extensible wizard example',
  162. 'page callback' => 'drupal_get_form',
  163. 'page arguments' => array('form_example_wizard'),
  164. 'access callback' => TRUE,
  165. 'description' => 'A general approach to a wizard multistep form.',
  166. 'file' => 'form_example_wizard.inc',
  167. );
  168. $items['examples/form_example/element_example'] = array(
  169. 'title' => 'Element example',
  170. 'page callback' => 'drupal_get_form',
  171. 'page arguments' => array('form_example_element_demo_form'),
  172. 'access callback' => TRUE,
  173. 'file' => 'form_example_elements.inc',
  174. 'weight' => 100,
  175. );
  176. return $items;
  177. }
  178. /**
  179. * Page callback for our general info page.
  180. */
  181. function form_example_intro() {
  182. $markup = t('The form example module provides a tutorial, extensible multistep example, an element example, and a #states example');
  183. return array('#markup' => $markup);
  184. }
  185. /**
  186. * Implements hook_help().
  187. */
  188. function form_example_help($path, $arg) {
  189. switch ($path) {
  190. case 'examples/form_example/tutorial':
  191. // TODO: Update the URL.
  192. $help = t('This form example tutorial for Drupal 7 is the code from the <a href="http://drupal.org/node/262422">Handbook 10-step tutorial</a>');
  193. break;
  194. case 'examples/form_example/element_example':
  195. $help = t('The Element Example shows how modules can provide their own Form API element types. Four different element types are demonstrated.');
  196. break;
  197. }
  198. if (!empty($help)) {
  199. return '<p>' . $help . '</p>';
  200. }
  201. }
  202. /**
  203. * Implements hook_element_info().
  204. *
  205. * To keep the various pieces of the example together in external files,
  206. * this just returns _form_example_elements().
  207. */
  208. function form_example_element_info() {
  209. require_once 'form_example_elements.inc';
  210. return _form_example_element_info();
  211. }
  212. /**
  213. * Implements hook_theme().
  214. *
  215. * The only theme implementation is by the element example. To keep the various
  216. * parts of the example together, this actually returns
  217. * _form_example_element_theme().
  218. */
  219. function form_example_theme($existing, $type, $theme, $path) {
  220. require_once 'form_example_elements.inc';
  221. return _form_example_element_theme($existing, $type, $theme, $path);
  222. }
  223. /**
  224. * @} End of "defgroup form_example".
  225. */