Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * This Module provides some custom functions
- *
- */
- function ch_custom_functions_form_alter(&$form, &$form_state, $form_id) {
- //dsm($form_id);
- //dsm($form);
- //echo '<pre>'.print_r($form, true).'</pre>';
- } // hook_form_alter
- function ch_custom_functions_form(){
- $form = array();
- $form['dob'] = array(
- '#type' => 'date',
- '#title' => "Date de naissance",
- '#after_build' => array("ch_custom_functions_format_dob")
- );
- return $form;
- }
- function ch_custom_functions_format_dob($form_element, &$form_state){
- // We unset the current values
- unset($form_element['year']['#options']);
- // Now we set the range we want
- $max_age = date('Y') - 100; // 100 years ago
- $min_age = date('Y') - 7; // 7 years ago
- // Now we populate the array
- $form_element['year']['#options'] = array();
- foreach (range($max_age, $min_age) as $year){
- $form_element['year']['#options'][$year] = $year;
- }
- // We return our modified element
- return $form_element;
- }
- /* *************************************************
- Front page custom search form
- ************************************************* */
- function ch_custom_functions_homepage_search_form($form, &$form_state) {
- // Generate list of ages for age selection
- $ages = array();
- for($i=18;$i<100;$i++){
- $date = date('Y-m-d', strtotime('-'.$i.' years'));
- $ages[$date] = t($i);
- }
- $form['iam-seeking'] = array(
- '#type' => 'fieldset',
- '#title' => '',
- '#collapsible' => FALSE,
- '#collapsed' => FALSE,
- );
- $form['iam-seeking']['iama'] = array(
- '#type' => 'checkbox',
- '#title' => t('I am a'),
- '#return_value' => 'Male',
- );
- $form['iam-seeking']['seeking'] = array(
- '#type' => 'checkbox',
- '#title' => t('seeking a'),
- '#default_value' => 1,
- '#return_value' => 1,
- );
- $form['ages'] = array(
- '#type' => 'fieldset',
- '#title' => '',
- '#collapsible' => FALSE,
- '#collapsed' => FALSE,
- );
- $form['ages']['age_start'] = array(
- '#type' => 'select',
- '#title' => t('Between'),
- '#options' => $ages,
- '#default_value' => date('Y-m-d', strtotime('-26 years')),
- );
- $form['ages']['age_end'] = array(
- '#type' => 'select',
- '#title' => t('&'),
- '#options' => $ages,
- '#default_value' => date('Y-m-d', strtotime('-35 years')),
- );
- $form['near'] = array(
- '#type' => 'textfield',
- '#title' => t('Near'),
- '#default_value' => '',
- '#size' => 20,
- '#maxlength' => 128,
- '#autocomplete_path' => 'example/autocomplete',
- );
- // Submit
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Find Matches',
- '#submit' => array('ch_custom_functions_form_submit'),
- );
- $form['actions']['submit']['#attributes']['class'][] = 'jeanbtn red active';
- $form['#validate'][] = 'ch_custom_functions_form_validate';
- //dsm( field_info_field('field_acct_location') );
- return $form;
- }
- /* ************************************
- Adding taxonomy autocomplete callbacks
- ************************************* */
- function ch_custom_functions_menu() {
- $items['example/autocomplete'] = array(
- 'page callback' => '_module_name_autocomplete',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK
- );
- return $items;
- }
- function _module_name_autocomplete($string) {
- $matches = array();
- //vocabulary id
- //$term_list = taxonomy_get_vocabularies(NULL);
- //dsm('$term_list structure: '. print_r($term_list, TRUE));
- $vid = 3;
- $result = db_select('taxonomy_term_data', 't')
- -> fields('t', array('tid', 'name'))
- -> condition('vid', $vid, '=')
- -> condition('name', $string.'%%', 'LIKE')
- -> range(0, 10)
- -> execute();
- foreach ($result as $term) {
- $matches[$term -> name] = check_plain($term -> name);
- }
- if(!$matches) $matches = array(''=>'No Results Found');
- drupal_json_output($matches);
- }
- /* ************************************
- Custom form validation
- ************************************* */
- function ch_custom_functions_form_submit(&$form, &$form_state){
- $form_state['redirect'] = 'search/';
- }
- function ch_custom_functions_form_validate($form, &$form_state) {
- // Random example, if the title is 'test' throw an error
- if ($form_state['values']['title'] == 'test') {
- form_set_error('title', 'Title cannot be "test"');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement