Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.36 KB | None | 0 0
  1. <?php
  2. function ajax_dropdown_search_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  3.  
  4.   if(!empty($form_state['values'])) {
  5.     $form_state['input'] = array_merge($form_state['input'],$form_state['values']);
  6.   }
  7.   if (($form['#id'] == 'views-exposed-form-product-listing-page') || ($form['#id'] == 'views-exposed-form-product-listing-sidebar-page')) {
  8.  
  9.       /**
  10.        * Build brand dropdown.
  11.        */
  12.       $first_options = array('All'=>'Select Your Brand') + ajax_dropdown_search_entity_query_printer_brand_dropdown();
  13.       // Set default to variable.
  14.       $value_dropdown_first = isset($form_state['input']['field_printer_brand_tid']) ? $form_state['input']['field_printer_brand_tid'] : null;
  15.  
  16.       $form['field_printer_brand_tid'] = array(
  17.         '#type' => 'select',
  18.         '#options' => $first_options,
  19.         '#default_value' => $value_dropdown_first,
  20.         '#required' => TRUE,
  21.         '#validated' => TRUE,
  22.         '#title' => 'Brand Name',
  23.         '#ajax' => array(
  24.           'event' => 'change',
  25.           'callback' => 'first_ajax_callback',
  26.           'wrapper' => 'second_dropdown',
  27.          ),
  28.       );
  29.  
  30.       /**
  31.        * Build device type dropdown.
  32.        */
  33.       $second_dropdown = array('All' => 'Select Device Type') + ajax_dropdown_search_get_printer_type_dropdown($value_dropdown_first);
  34.       // Set default to variable.
  35.       $value_dropdown_second = isset($form_state['input']['field_printer_type_tid']) ? $form_state['input']['field_printer_type_tid'] : null;
  36.  
  37.       $form['field_printer_type_tid'] = array(
  38.         '#type' => 'select',
  39.         '#validated' => TRUE,
  40.         '#required' => TRUE,
  41.         '#DANGEROUS_SKIP_CHECK' => TRUE,
  42.         '#title' => 'Device Type',
  43.         '#prefix' => '<div id="second_dropdown">',
  44.         '#suffix' => '</div>',
  45.         '#options' => $second_dropdown,
  46.         '#default_value' => $value_dropdown_second,
  47.         '#ajax' => array(
  48.           'event' => 'change',
  49.           'callback' => 'second_ajax_callback',
  50.           'wrapper' => 'third_dropdown',
  51.         ),
  52.       );
  53.  
  54.       // Build printer model dropdown.
  55.       $form['field_printer_model_value'] = array(
  56.         '#type' => 'select',
  57.         '#validated' => TRUE,
  58.         '#required' => TRUE,
  59.         '#DANGEROUS_SKIP_CHECK' => TRUE,
  60.         '#title' => 'Printer Model',
  61.         '#prefix' => '<div id="third_dropdown">',
  62.         '#suffix' => '</div>',
  63.         '#options' => array('All'=>'Select Printer Model') + ajax_dropdown_search_get_printer_dropdown($value_dropdown_first,$value_dropdown_second),
  64.         '#default_value' => isset($form_state['input']['field_printer_model_value']) ? $form_state['input']['field_printer_model_value'] : null,
  65.       );
  66.   }
  67.  
  68.   return $form;
  69. }
  70.  
  71. /**
  72.  * [first_ajax_callback description]
  73.  * @param  [type] $form       [description]
  74.  * @param  [type] $form_state [description]
  75.  * @return [type]             [description]
  76.  */
  77. function first_ajax_callback(&$form, &$form_state) {
  78.   $commands = array();
  79.   $form['field_printer_model_value']['#options'] = array('All' => 'Select Printer Model');
  80.  
  81.   $commands[] = ajax_command_replace("#second_dropdown", drupal_render($form['field_printer_type_tid']));
  82.   $commands[] = ajax_command_replace("#third_dropdown", drupal_render($form['field_printer_model_value']));
  83.  
  84.   return array('#type' => 'ajax', '#commands' => $commands);
  85. }
  86.  
  87. /**
  88.  * [second_ajax_callback description]
  89.  * @param  [type] $form       [description]
  90.  * @param  [type] $form_state [description]
  91.  * @return [type]             [description]
  92.  */
  93. function second_ajax_callback(&$form, &$form_state) {
  94.   $commands = array();
  95.   $commands[] = ajax_command_replace("#third_dropdown", drupal_render($form['field_printer_model_value']));
  96.  
  97.   return array('#type' => 'ajax', '#commands' => $commands);
  98. }
  99.  
  100. /**
  101.  * Query taxonomy terms printer brands.
  102.  * @return array string taxonomy names
  103.  */
  104. function ajax_dropdown_search_entity_query_printer_brand_dropdown() {
  105.   $taxonomy_query = new EntityFieldQuery;
  106.   $taxonomy_query->entityCondition('entity_type', 'taxonomy_term')
  107.     ->entityCondition('bundle', 'printer_brand')
  108.     ->propertyOrderBy('weight');
  109.   $taxonomy_terms = $taxonomy_query->execute();
  110.   $printer_brand = array();
  111.   foreach ($taxonomy_terms['taxonomy_term'] as $term) {
  112.     $term_load = taxonomy_term_load($term->tid);
  113.     $printer_brand[$term_load->tid] = $term_load->name;
  114.   }
  115.  
  116.   return $printer_brand;
  117. }
  118.  
  119. /**
  120.  * Queries and loads nodes of content type printer model.
  121.  * Filter by brand tid.
  122.  * @param  integer  $brand    taxonomy term id
  123.  * @return mixed    printer   model nodes
  124.  */
  125. function ajax_dropdown_search_printer_model_query_filter_by_brand($brand) {
  126.   $query = new EntityFieldQuery();
  127.   $query->entityCondition('entity_type', 'node')
  128.   ->entityCondition('bundle', 'printer_model')
  129.   ->propertyCondition('status', 1)
  130.   ->fieldCondition('field_printer_brand', 'tid', $brand, '=')
  131.   ->propertyOrderBy('title', 'ASC');
  132.   $result = $query->execute();
  133.  
  134.   if (isset($result['node'])) {
  135.     $array_nid = array_keys($result['node']);
  136.     $nodes = node_load_multiple($array_nid);
  137.   }
  138.  
  139.   return $nodes;
  140. }
  141.  
  142. /**
  143.  * Load the type by the printer type taxonomy term id.
  144.  * @param  integer  $brand  taxonomy term id
  145.  * @return array    string  Device type
  146.  */
  147. function ajax_dropdown_search_get_printer_type_dropdown($brand) {
  148.   // Call array of node load objects of content type printer model.
  149.   $printer_models = ajax_dropdown_search_printer_model_query_filter_by_brand($brand);
  150.   $second_dropdown = array();
  151.   foreach($printer_models as $model) {
  152.     $second_dropdown[$model->field_printer_type['und'][0]['tid']] = taxonomy_term_load($model->field_printer_type['und'][0]['tid'])->field_friendly_name['und'][0]['safe_value'];
  153.   }
  154.  
  155.   return $second_dropdown;
  156. }
  157.  
  158. /**
  159.  * Queries and loads nodes of content type printer model.
  160.  * Filter by brand and type tid.
  161.  * @param  integer  $brand    taxonomy term id
  162.  * @param  integer  $type     taxonomy term id
  163.  * @return mixed    printer   model nodes
  164.  */
  165. function ajax_dropdown_search_printer_model_query_filter_by_brand_and_type($brand, $type) {
  166.   $query = new EntityFieldQuery();
  167.   $query->entityCondition('entity_type', 'node')
  168.   ->entityCondition('bundle', 'printer_model')
  169.   ->propertyCondition('status', 1)
  170.   ->fieldCondition('field_printer_brand', 'tid', $brand, '=')
  171.   ->fieldCondition('field_printer_type', 'tid', $type, '=')
  172.   ->propertyOrderBy('title', 'ASC');
  173.   $result = $query->execute();
  174.  
  175.   if (isset($result['node'])) {
  176.     $array_nid = array_keys($result['node']);
  177.     $nodes = node_load_multiple($array_nid);
  178.   }
  179.  
  180.   return $nodes;
  181. }
  182.  
  183. /**
  184.  * Load printer based on brand and type values.
  185.  * @param  integer  $brand  taxonomy term id
  186.  * @param  integer  $type   taxonomy term id
  187.  * @return array    string  model name
  188.  */
  189. function ajax_dropdown_search_get_printer_dropdown($brand, $type) {
  190.   $printer_models = ajax_dropdown_search_printer_model_query_filter_by_brand_and_type($brand, $type);
  191.   $printers = array();
  192.   foreach($printer_models as $model) {
  193.     if($brand == 'All' || $type == 'All') {
  194.       break;
  195.     }
  196.     elseif($model->field_printer_brand['und'][0]['tid'] == $brand && $model->field_printer_type['und'][0]['tid']) {
  197.       $printers[$model->field_printer_model['und'][0]['safe_value']] = $model->field_printer_model['und'][0]['safe_value'];
  198.     }
  199.   }
  200.  
  201.   return $printers;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement