Advertisement
emcniece

Make custom form submit data to view filters

Mar 28th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.36 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  * This Module provides some custom functions
  5.  *
  6.  */
  7. function ch_custom_functions_form_alter(&$form, &$form_state, $form_id) {
  8.     //dsm($form_id);
  9.     //dsm($form);
  10.     //echo '<pre>'.print_r($form, true).'</pre>';
  11.  
  12. } // hook_form_alter
  13.  
  14. function ch_custom_functions_form(){
  15.     $form = array();
  16.     $form['dob'] = array(
  17.       '#type' => 'date',
  18.       '#title' => "Date de naissance",
  19.       '#after_build' => array("ch_custom_functions_format_dob")
  20.     );
  21.     return $form;
  22. }
  23.  
  24. function ch_custom_functions_format_dob($form_element, &$form_state){
  25.     // We unset the current values
  26.     unset($form_element['year']['#options']);
  27.  
  28.     // Now we set the range we want
  29.     $max_age = date('Y') - 100; // 100 years ago
  30.     $min_age = date('Y') - 7; // 7 years ago
  31.    
  32.     // Now we populate the array
  33.     $form_element['year']['#options'] = array();
  34.     foreach (range($max_age, $min_age) as $year){
  35.         $form_element['year']['#options'][$year] = $year;
  36.     }
  37.    
  38.     // We return our modified element
  39.     return $form_element;
  40. }
  41.  
  42.  
  43.  
  44. /* *************************************************
  45.  
  46.     Front page custom search form
  47.  
  48. ************************************************* */
  49. function ch_custom_functions_homepage_search_form($form, &$form_state) {
  50.    
  51.     // Generate list of ages for age selection
  52.     $ages = array();
  53.     for($i=18;$i<100;$i++){
  54.         $date = date('Y-m-d', strtotime('-'.$i.' years'));
  55.         $ages[$date] = t($i);
  56.     }
  57.    
  58.     $form['iam-seeking'] = array(
  59.         '#type' => 'fieldset',
  60.         '#title' => '',
  61.         '#collapsible' => FALSE,
  62.         '#collapsed' => FALSE,
  63.     );
  64.    
  65.     $form['iam-seeking']['iama'] = array(
  66.         '#type' => 'checkbox',
  67.         '#title' => t('I am a'),
  68.         '#return_value' => 'Male',
  69.     ); 
  70.     $form['iam-seeking']['seeking'] = array(
  71.         '#type' => 'checkbox',
  72.         '#title' => t('seeking a'),
  73.         '#default_value' => 1,
  74.         '#return_value' => 1,
  75.     );
  76.    
  77.     $form['ages'] = array(
  78.         '#type' => 'fieldset',
  79.         '#title' => '',
  80.         '#collapsible' => FALSE,
  81.         '#collapsed' => FALSE,
  82.     );
  83.    
  84.     $form['ages']['age_start'] = array(
  85.        '#type' => 'select',
  86.        '#title' => t('Between'),
  87.        '#options' => $ages,
  88.        '#default_value' => date('Y-m-d', strtotime('-26 years')),
  89.     );
  90.     $form['ages']['age_end'] = array(
  91.        '#type' => 'select',
  92.        '#title' => t('&amp;'),
  93.        '#options' => $ages,
  94.        '#default_value' => date('Y-m-d', strtotime('-35 years')),
  95.     );
  96.    
  97.     $form['near'] = array(
  98.         '#type' => 'textfield',
  99.         '#title' => t('Near'),
  100.         '#default_value' => '',
  101.         '#size' => 20,
  102.         '#maxlength' => 128,
  103.         '#autocomplete_path' => 'example/autocomplete',
  104.     );
  105.    
  106.     // Submit
  107.     $form['actions']['submit'] = array(
  108.         '#type' => 'submit',
  109.         '#value' => 'Find Matches',
  110.         '#submit' => array('ch_custom_functions_form_submit'),
  111.     );
  112.     $form['actions']['submit']['#attributes']['class'][] = 'jeanbtn red active';
  113.     $form['#validate'][] = 'ch_custom_functions_form_validate';
  114.    
  115.     //dsm( field_info_field('field_acct_location') );    
  116.     return $form;
  117. }
  118.  
  119.  
  120. /* ************************************
  121.  
  122.     Adding taxonomy autocomplete callbacks
  123.  
  124. ************************************* */
  125.  
  126. function ch_custom_functions_menu() {  
  127.   $items['example/autocomplete'] = array(
  128.     'page callback' => '_module_name_autocomplete',
  129.     'access arguments' => array('access content'),
  130.     'type' => MENU_CALLBACK
  131.   );
  132.   return $items;
  133. }
  134. function _module_name_autocomplete($string) {
  135.   $matches = array();
  136.   //vocabulary id
  137.     //$term_list = taxonomy_get_vocabularies(NULL);
  138.     //dsm('$term_list structure: '. print_r($term_list, TRUE));
  139.  
  140.   $vid = 3;
  141.  
  142.   $result = db_select('taxonomy_term_data', 't')
  143.     -> fields('t', array('tid', 'name'))
  144.     -> condition('vid', $vid, '=')
  145.     -> condition('name', $string.'%%', 'LIKE')
  146.     -> range(0, 10)
  147.     -> execute();
  148.  
  149.   foreach ($result as $term) {
  150.     $matches[$term -> name] = check_plain($term -> name);
  151.   }
  152.  
  153.     if(!$matches) $matches = array(''=>'No Results Found');
  154.   drupal_json_output($matches);
  155. }
  156.  
  157. /* ************************************
  158.  
  159.     Custom form validation
  160.  
  161. ************************************* */
  162. function ch_custom_functions_form_submit(&$form, &$form_state){
  163.     $form_state['redirect'] = 'search/';
  164. }
  165. function ch_custom_functions_form_validate($form, &$form_state) {
  166.     // Random example, if the title is 'test' throw an error
  167.     if ($form_state['values']['title'] == 'test') {
  168.         form_set_error('title', 'Title cannot be "test"');
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement