Fix for Code

  1. <?php  
  2.  
  3. function hookContact_menu()
  4. {      
  5.         $items['contact/js/addInfos'] = array(
  6.                 'page callback' => 'hookContact_addInfos',
  7.                 'type' => MENU_CALLBACK,
  8.                 'access callback' => TRUE,
  9.         );
  10.         return $items;
  11. }
  12.  
  13. /**
  14.  * Implementation of hook_form_alter().
  15.  * @see http://api.drupal.org/api/function/hook_form_alter
  16.  * We add a few custom fields to the default Drupal contact form
  17.  */
  18. function hookContact_form_contact_mail_page_alter(&$form, $form_state) {
  19.         $form['contact_info'] = array(
  20.                 '#tree' => TRUE,
  21.                 '#prefix' => '<div id="wrapper-infos">',
  22.                 '#suffix' => '</div>'
  23.                 '#type' => 'hidden',
  24.         );
  25.  
  26.         $form['choix'] = array(
  27.                 '#type' => 'radios',
  28.                 '#title' => t('Type de contact'),
  29.                 '#default_value' => $form_state['values']['choix'],
  30.                 '#options' => array(
  31.                         'mail' => 'mail',
  32.                         'tel' => 'téléphone',
  33.                 ),     
  34.                 '#ahah' => array(
  35.                         'event'   => 'change',
  36.                         'path'    => 'contact/js/addInfos',
  37.                         'wrapper' => 'wrapper-infos',                  
  38.                         'methode' => 'prepend',        
  39.                 ),
  40.                 '#required' => TRUE,
  41.         );
  42.  
  43.         $form['copy'] = array(
  44.                 '#type' => 'checkbox',
  45.         '#title' => t('Send yourself a copy.'),
  46.     );
  47.        
  48.          
  49.         // reorder the elements in the form to include the elements being inserted
  50.         $order = array('contact_information', 'name', 'entreprise', 'mail', 'choix', 'contact_info', 'subject', 'message', 'copy', 'submit');
  51.         foreach($order as $key => $field) {
  52.                 $form[$field]['#weight'] = $key;
  53.         }
  54. }
  55.  
  56. function hookContact_addInfos()
  57. {      
  58.         //On récupère id unique du formulaire
  59.         $form_build_id = $_POST['form_build_id'];
  60.         $cached_form_state = array();
  61.         //On récupère le formulaire en cache
  62.         $cached_form = form_get_cache($form_build_id, $form_state);
  63.        
  64.         $cached_form['#post'] = $_POST;
  65.         $cached_form_state['post'] = $_POST;
  66.         //$cached_form_state['values'] = $form_state['values'];
  67.  
  68.         $cached_form = form_builder($form['form_id']['#value'], $cached_form, $cached_form_state);
  69.        
  70.         $addForm = hookContact_subForm($_POST['choix']);
  71.        
  72.         $cached_form = array_merge($cached_form, $addForm);
  73.        
  74.         form_set_cache($form_build_id, $cached_form, $cached_form_state);
  75.        
  76.         $debug = "<pre>".print_r($cached_form, true)."</pre>";
  77.        
  78.         drupal_json(array('status' => TRUE, 'data' => $debug . drupal_render($addForm)));
  79. }
  80.  
  81. function hookContact_subForm($choix)
  82. {
  83.         if($choix =='tel') {
  84.                  
  85.                 return array(
  86.                         'heure' => array(
  87.                                 '#name' => 'heure',
  88.                                 '#type' => 'select',
  89.                                 '#options' => array(
  90.                                         '8h-12h' => '8h-12h',
  91.                                         '12h-14h' => '12h-14h',
  92.                                         '14h-17h' => '14h-17h',
  93.                                         '17h-20h' => '17h-20h',
  94.                                 ),     
  95.                         ),
  96.                         'jour' => array(
  97.                                 '#name' => 'jour',
  98.                                 '#type' => 'select',
  99.                                 '#options' => array(
  100.                                         'lundi' => 'Lundi',
  101.                                         'mardi' => 'Mardi',
  102.                                         'mercredi' => 'Mercredi',
  103.                                         'jeudi' => 'Jeudi',
  104.                                         'vendredi' => 'Vendredi',
  105.                                 ),     
  106.                         ),
  107.                         'tel' => array(
  108.                                 '#name' => 'tel',
  109.                                 '#type' => 'textfield',
  110.                                 '#title' => t('Votre numéro de téléphone'),
  111.                         )
  112.                 );
  113.        
  114.         } else if($choix =='mail'){
  115.                 return array();        
  116.         }
  117. }
  118. function hookContact_mail_alter(&$message)
  119. {
  120.         $v = var_export($message, true);
  121.         drupal_set_message("message: " . $v);
  122.         switch($message['id']) {
  123.                 case 'contact_page_mail':
  124.                         if($message['params']['choix'] == 'tel') {
  125.                                 $message['body'][] = 'Moyen de contact : téléphone\n';
  126.                                 $message['body'][] = 'Numéro de téléphone : '. $message['params']['tel'];
  127.                                 $message['body'][] = 'Jour et heure : le '. $message['params']['jour'] .' entre '.$message['params']['heure'] ;
  128.                         } else {
  129.                                 $message['body'][] = 'Moyen de contact : mail ';
  130.                         }
  131.                        
  132.                 break;
  133.         }
  134. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.