0

enter image description hereI created a views to show parent and child term together using autocomplete expose filter .. but its show only one child. I have same child for two different parent.but its show only one child. how to change this programmatically using view query alter. I need such type:

Parent . Parent-1
           locality-1
           locality-2
         parent-2
           locality-2
           locality-4

I need such type of result on autocomplete expose filter.

  • Do you only want to do it with hook_views_query_alter? hook_form_alter would also be an option, I could provide a code example for that. – Stefanos Petrakis Feb 23 '16 at 9:32
  • It would be great help. – jsh Feb 23 '16 at 9:34
  • I have try lot of module .. but still not find a way to achive this using module. So finally I want to fix this via custom code. – jsh Feb 23 '16 at 9:35
  • Please provide me code example. – jsh Feb 23 '16 at 12:53
  • Hey there, I will do, need a little time though. :-) – Stefanos Petrakis Feb 23 '16 at 13:49
-1

Here is a possible answer to this problem. First of all, a litte visual.

enter image description here

To get this working you need the following:

  • Configure your View to display the exposed taxonomy filter as a dropdown. autcomplete will not work because you cannot have multiple entries with the same key in the generated JSON array.
  • Optional: Install and configure the Chosen module [https://www.drupal.org/project/chosen] to get your dropdown to render nicely and offer autocomplete options.
  • Optional: You can finally use some custom code for formatting the presentation of your list, here is a sample minimal module that adds the special arrow that you see in the screenshot.
define('YOUR_CUSTOM_TAXONOMY_AUTOCOMPLETE_PATH', 'mycustompath/1');
define('YOUR_EXPOSED_FILTER_MACHINE_NAME', 'field_tags_tid');

/**
 * Implements hook_form_alter()
 */
function se_form_alter(&$form, &$form_state, $form_id) {
  if( $form_id == 'views_exposed_form' ){
    if(isset($form[YOUR_EXPOSED_FILTER_MACHINE_NAME]) && 
    $form[YOUR_EXPOSED_FILTER_MACHINE_NAME]['#type'] == 'select'){
      $form[YOUR_EXPOSED_FILTER_MACHINE_NAME]['#options'] = _custom_update_options_for_taxonomy($form[YOUR_EXPOSED_FILTER_MACHINE_NAME]['#options']);
    }
  }
}

/**
 * Function that update select options for taxonomy filter
 * 
 */
function _custom_update_options_for_taxonomy($options = array()) {
  $current_parent = '';
  foreach( $options as $i=>$option ) {
    if( is_int($i) && isset($option->option) && is_array($option->option) ) {
      $option_arr = $option->option;
      $tids = array_keys($option_arr);
      $tid = reset($tids);
      // is this a child of the previous parent
      if( strpos($option_arr[$tid], '-') === 0 && $current_parent != '') {
        $option_arr[$tid] = preg_replace('/^-/', ' ⤷ ', $option_arr[$tid]);
        $option->option = $option_arr;
      }
      // this is a parent node
      else {
        $current_parent = $option_arr[$tid];
      }
    }
  }
  return $options;
}

Hope this help some!

| improve this answer | |

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.