Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function ajax_dropdown_search_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
- if(!empty($form_state['values'])) {
- $form_state['input'] = array_merge($form_state['input'],$form_state['values']);
- }
- if (($form['#id'] == 'views-exposed-form-product-listing-page') || ($form['#id'] == 'views-exposed-form-product-listing-sidebar-page')) {
- /**
- * Build brand dropdown.
- */
- $first_options = array('All'=>'Select Your Brand') + ajax_dropdown_search_entity_query_printer_brand_dropdown();
- // Set default to variable.
- $value_dropdown_first = isset($form_state['input']['field_printer_brand_tid']) ? $form_state['input']['field_printer_brand_tid'] : null;
- $form['field_printer_brand_tid'] = array(
- '#type' => 'select',
- '#options' => $first_options,
- '#default_value' => $value_dropdown_first,
- '#required' => TRUE,
- '#validated' => TRUE,
- '#title' => 'Brand Name',
- '#ajax' => array(
- 'event' => 'change',
- 'callback' => 'first_ajax_callback',
- 'wrapper' => 'second_dropdown',
- ),
- );
- /**
- * Build device type dropdown.
- */
- $second_dropdown = array('All' => 'Select Device Type') + ajax_dropdown_search_get_printer_type_dropdown($value_dropdown_first);
- // Set default to variable.
- $value_dropdown_second = isset($form_state['input']['field_printer_type_tid']) ? $form_state['input']['field_printer_type_tid'] : null;
- $form['field_printer_type_tid'] = array(
- '#type' => 'select',
- '#validated' => TRUE,
- '#required' => TRUE,
- '#DANGEROUS_SKIP_CHECK' => TRUE,
- '#title' => 'Device Type',
- '#prefix' => '<div id="second_dropdown">',
- '#suffix' => '</div>',
- '#options' => $second_dropdown,
- '#default_value' => $value_dropdown_second,
- '#ajax' => array(
- 'event' => 'change',
- 'callback' => 'second_ajax_callback',
- 'wrapper' => 'third_dropdown',
- ),
- );
- // Build printer model dropdown.
- $form['field_printer_model_value'] = array(
- '#type' => 'select',
- '#validated' => TRUE,
- '#required' => TRUE,
- '#DANGEROUS_SKIP_CHECK' => TRUE,
- '#title' => 'Printer Model',
- '#prefix' => '<div id="third_dropdown">',
- '#suffix' => '</div>',
- '#options' => array('All'=>'Select Printer Model') + ajax_dropdown_search_get_printer_dropdown($value_dropdown_first,$value_dropdown_second),
- '#default_value' => isset($form_state['input']['field_printer_model_value']) ? $form_state['input']['field_printer_model_value'] : null,
- );
- }
- return $form;
- }
- /**
- * [first_ajax_callback description]
- * @param [type] $form [description]
- * @param [type] $form_state [description]
- * @return [type] [description]
- */
- function first_ajax_callback(&$form, &$form_state) {
- $commands = array();
- $form['field_printer_model_value']['#options'] = array('All' => 'Select Printer Model');
- $commands[] = ajax_command_replace("#second_dropdown", drupal_render($form['field_printer_type_tid']));
- $commands[] = ajax_command_replace("#third_dropdown", drupal_render($form['field_printer_model_value']));
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- /**
- * [second_ajax_callback description]
- * @param [type] $form [description]
- * @param [type] $form_state [description]
- * @return [type] [description]
- */
- function second_ajax_callback(&$form, &$form_state) {
- $commands = array();
- $commands[] = ajax_command_replace("#third_dropdown", drupal_render($form['field_printer_model_value']));
- return array('#type' => 'ajax', '#commands' => $commands);
- }
- /**
- * Query taxonomy terms printer brands.
- * @return array string taxonomy names
- */
- function ajax_dropdown_search_entity_query_printer_brand_dropdown() {
- $taxonomy_query = new EntityFieldQuery;
- $taxonomy_query->entityCondition('entity_type', 'taxonomy_term')
- ->entityCondition('bundle', 'printer_brand')
- ->propertyOrderBy('weight');
- $taxonomy_terms = $taxonomy_query->execute();
- $printer_brand = array();
- foreach ($taxonomy_terms['taxonomy_term'] as $term) {
- $term_load = taxonomy_term_load($term->tid);
- $printer_brand[$term_load->tid] = $term_load->name;
- }
- return $printer_brand;
- }
- /**
- * Queries and loads nodes of content type printer model.
- * Filter by brand tid.
- * @param integer $brand taxonomy term id
- * @return mixed printer model nodes
- */
- function ajax_dropdown_search_printer_model_query_filter_by_brand($brand) {
- $query = new EntityFieldQuery();
- $query->entityCondition('entity_type', 'node')
- ->entityCondition('bundle', 'printer_model')
- ->propertyCondition('status', 1)
- ->fieldCondition('field_printer_brand', 'tid', $brand, '=')
- ->propertyOrderBy('title', 'ASC');
- $result = $query->execute();
- if (isset($result['node'])) {
- $array_nid = array_keys($result['node']);
- $nodes = node_load_multiple($array_nid);
- }
- return $nodes;
- }
- /**
- * Load the type by the printer type taxonomy term id.
- * @param integer $brand taxonomy term id
- * @return array string Device type
- */
- function ajax_dropdown_search_get_printer_type_dropdown($brand) {
- // Call array of node load objects of content type printer model.
- $printer_models = ajax_dropdown_search_printer_model_query_filter_by_brand($brand);
- $second_dropdown = array();
- foreach($printer_models as $model) {
- $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'];
- }
- return $second_dropdown;
- }
- /**
- * Queries and loads nodes of content type printer model.
- * Filter by brand and type tid.
- * @param integer $brand taxonomy term id
- * @param integer $type taxonomy term id
- * @return mixed printer model nodes
- */
- function ajax_dropdown_search_printer_model_query_filter_by_brand_and_type($brand, $type) {
- $query = new EntityFieldQuery();
- $query->entityCondition('entity_type', 'node')
- ->entityCondition('bundle', 'printer_model')
- ->propertyCondition('status', 1)
- ->fieldCondition('field_printer_brand', 'tid', $brand, '=')
- ->fieldCondition('field_printer_type', 'tid', $type, '=')
- ->propertyOrderBy('title', 'ASC');
- $result = $query->execute();
- if (isset($result['node'])) {
- $array_nid = array_keys($result['node']);
- $nodes = node_load_multiple($array_nid);
- }
- return $nodes;
- }
- /**
- * Load printer based on brand and type values.
- * @param integer $brand taxonomy term id
- * @param integer $type taxonomy term id
- * @return array string model name
- */
- function ajax_dropdown_search_get_printer_dropdown($brand, $type) {
- $printer_models = ajax_dropdown_search_printer_model_query_filter_by_brand_and_type($brand, $type);
- $printers = array();
- foreach($printer_models as $model) {
- if($brand == 'All' || $type == 'All') {
- break;
- }
- elseif($model->field_printer_brand['und'][0]['tid'] == $brand && $model->field_printer_type['und'][0]['tid']) {
- $printers[$model->field_printer_model['und'][0]['safe_value']] = $model->field_printer_model['und'][0]['safe_value'];
- }
- }
- return $printers;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement