3

I wanted to make a page with exposed filter search app in Drupal for a list of people.

I started by creating content type Bio and populated it as following:-

enter image description here

Then, I continued making a view with exposed filters of the fields of content type Bio. With the help of Better Exposed Filters, I am showing both Filters through Checkboxes through AJAX, as below:-

enter image description here

Now, until now, it's working fine. when I select a region or/and any city, the content updates according to that.

What I am hoping to create is a dependency between the region and city. All I want is that when I select any region in the Filter by Region, the lower filter values in the Filter by City automatically adjust and show me only the cities for which the region field in Bio is the selected city.

As an example, If a user selects EMEA, the city exposed filters should only show European cities.

I have been stuck in this for hours now. I have tried various solutions (View Dependent Filtersand Filter Fields) but to no avail.

I'll be therefore very thankful for any help whatsoever.

2

Look at my currently developing site's exposed filter(where 'Бренд' and 'Модель' fields): Link

If you want to achive this kind of functionality then you can achieve it like this. First you need to alter exposed filter function:

function MY_MODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  if ($form['#id'] == 'views-exposed-form-VIEWS_NAME_VIEWS_DISPLAY') {
    if(!empty($form_state['values'])) {
      $form_state['input'] = array_merge($form_state['input'],$form_state['values']);
    }

    // Marks
    $mark_childs = populate_terms_by_vocab('transport_models', TRUE, FALSE);
    $form['field_adv_mark_tid']['#options'] = array('All' => t('--Выберите')) + $mark_childs;
    $form['field_adv_mark_tid']['#ajax'] = array(
      'callback' => 'adv_mark_callback',
      'wrapper' => 'model-wrapper',
      'effect' => 'none',
      'progress' => array(
        'type' => 'throbber',
        'message' => '',
      ),
    );
    // marks end

    // Models begin
    $selectedMark = isset($form_state['input']['field_adv_mark_tid']) ? $form_state['input']['field_adv_mark_tid'] : NULL;

    if($selectedMark != NULL || $selectedMark != FALSE) {
      $model_childs = populate_terms_by_vocab('transport_models', FALSE, $selectedMark);
    }
    elseif($selectedMark == NULL || $selectedMark == FALSE) {
      $model_childs = array();
    }

    $form['field_adv_model_tid']['#options'] = array('All' => t('--Выберите')) + $model_childs;
    $form['field_adv_model_tid']['#prefix'] = '<div id="model-wrapper">';
    $form['field_adv_model_tid']['#suffix'] = '</div>';
    // models end
  }
}

// This is an Ajax callback for $form['field_adv_mark_tid']['#ajax']
function adv_mark_callback($form, $form_state) {
  return $form['field_adv_model_tid'];
}

// This is a helper function which returns terms (suitable for form selectlist, checkboxes or radios..)
function populate_terms_by_vocab($vocabulary_machine_name, $only_parents = TRUE, $parent = FALSE) {
  $vocab = taxonomy_vocabulary_machine_name_load($vocabulary_machine_name);
  $terms = array();
  if($parent == FALSE) {
    $terms = taxonomy_get_tree($vocab->vid, 0, NULL, FALSE);
  }
  elseif($parent == TRUE) {
    $terms = taxonomy_get_tree($vocab->vid, $parent, NULL, FALSE);
  }

  $child_terms = array();
  foreach($terms as $key => $value) {
    if($only_parents && $terms[$key]->parents[0] == 0) {
      $child_terms[$terms[$key]->tid] = $terms[$key]->name;
    }
    elseif($only_parents == FALSE && $terms[$key]->parents[0] != 0) {
      $child_terms[$terms[$key]->tid] = $terms[$key]->name;
    }
  }

  return $child_terms;
}

I thinks the logic is the same.. In my case, I am adding to my entity type the two same term reference fields, and on content creation form I am displaying them as different parent field and child field (with pre-render function). By that, first field is holding the top level terms, the second field is holding child terms. And by views ui I am exposing them separately. And, that piece of code is displaying them as I want..

|improve this answer|||||
  • Thank you xurshid. I appreciate a lot. Where should I place this code? – user219492 Dec 10 '13 at 19:25
  • @user219492 You can create a simple for that (just MY_MODULE.info file and MY_MODULE.module file is needed. Put this code in .module file) or put it in template.php. – xurshid29 Dec 11 '13 at 5:57
0

Simple Hierarchical Select module seems to do what you are trying to achieve.

Simple hierarchical select defines a new form widget for taxonomy fields to select a term by "browsing" through the vocabularies hierarchy. The widget is available in node forms and as Views exposed filter.

  • add a new filter using the field set-up as "Simple hierarchical select" or use "Content: Has taxonomy terms (with depth; Simple hierarchical select)" as a new filter

  • use "Simple hierarchical select" as selection type

  • select "Expose this filter to visitors, to allow them to change it"

|improve this answer|||||
  • Thank you for your answer. But It seems that it's not gonna help in Existing filters or fields within the content type. Any Idea on that? Thanks again for your assistance. – user219492 Dec 10 '13 at 8:42
  • Any idea anyone guys? Simple Hierarchical Select only works with taxonomy terms, but I have fields in the content type Bio which I am using as Exposed Filters. Any help here? – user219492 Dec 10 '13 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.