Advertisement
brucewhealton

Drupal Mod Dev. Form API

Mar 14th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. form_fun.module file - this line does not appear in the code, obviously.
  2.  
  3. <?php
  4.  
  5. /*
  6.  * Implements form hook_menu().
  7.  */
  8.  
  9. function form_fun_menu() {
  10.     $items = array();
  11.    
  12.     $items['form_fun/cake'] = array(
  13.         'title' => 'Cake or Pie?',
  14.         'page callback' => 'form_fun_cake_page',
  15.         'access arguments' => array('access content'),
  16.         'file' => 'form_fun.cake.inc',
  17.         'weight' => 1,
  18.     );  
  19.     return $items;
  20. }
  21.  
  22. form_fun.cake.inc file - also just a note for reference.
  23. <?php
  24.  
  25. /*
  26.  * To change this license header, choose License Headers in Project Properties.
  27.  * To change this template file, choose Tools | Templates
  28.  * and open the template in the editor.
  29.  */
  30.  
  31. function form_fun_cake_page() {
  32.     return drupal_get_form('form_fun_cake');
  33. }
  34.  
  35. function form_fun_cake(&$form_state) {
  36.     $form = array();
  37.    
  38.     $form['choice'] = array(
  39.         '#type' => 'select',
  40.         '#title' => t('Cake or pie?'),
  41.         '#description' => t('Would you like cake or pie?'),
  42.         '#options' => array(
  43.             'cake' =>  t('Cake please'),
  44.             'pie' => t('Pie I guess'),
  45.         ),
  46.         '#default' => 'cake',
  47.         '#required' => TRUE,
  48.     );
  49.    
  50.     $form['buttons']['submit'] = array(
  51.         '#type' => 'submit',
  52.         '#value' => t('Submit'),
  53.     );
  54.    
  55.     $form['buttons']['unsure'] = array(
  56.         '#type' => 'submit',
  57.         '#value' => t('Equivocate'),
  58.         '#submit' => array('form_fun_cake_unsure'),
  59.         '#validate' => array(),
  60.        
  61.     );
  62.    
  63.     return $form;
  64. }
  65.  
  66. function form_fun_cake_validate(&$form, &$form_state) {
  67.   if($form_state['values']['choice'] == 'cake') {
  68.       form_set_error('choice', t('We are out of cake.'));
  69.   }
  70. }
  71.  
  72. function form_fun_cake_submit(&$form, &$form) {
  73.     dsm($form_state['values']);
  74.     $form_state['redirect'] = '';
  75. }
  76.  
  77. function form_fun_cake_unsure(&$form, &$form_state) {
  78.     drupal_set_message(t('Make up your mind.'), 'warning' );
  79. }
  80.  
  81. form_fun.info - file
  82.  
  83. name = Form FunApi Demonstration
  84. description = Demonstrate the use of Form API.
  85. core = 7.x
  86. files[] = form_fun.cake.inc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement