Views exposed taxonomy exposed filter show "Content Taxonomy Tree" or "Taxonomy Term Reference Tree Widget" structure
Well Recently I came across the situation that the View exposed taxonomy field should show like "Content Taxonomy Tree" or "Taxonomy Term Reference Tree Widget" . It took quite sometime to figure out what to do. When i Figured it out i decide to write a little bit about it. Following steps will help you to achieve this.
2) You should have the view with taxonomy field field as exposed.
3) Copy and paste the following code on your custom module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implementing hook_form_alter() | |
* @param $form | |
* @param $form_state | |
* @param $form_id | |
*/ | |
function YOUR_MODULE_NAME_form_alter(&$form, $form_state, $form_id) { | |
if ($form_id == 'views_exposed_form') { | |
$term_reference_tree_path = drupal_get_path('module', 'term_reference_tree'); | |
// You should give , your exposed taxonomy field | |
$form['your-toxonomy-field'] = array( | |
//s'#title' => t('Tree'), | |
'#type' => 'checkbox_tree', | |
'#max_choices' => 999, | |
'#max_depth' => 999, | |
'#field_name' => 'custom_name', | |
// '#leaves_only' => 1, | |
'#start_minimized' => TRUE, | |
'#depth' => 0, | |
'#vocabulary' => taxonomy_vocabulary_load(3), // instead of '3', Your vocabulary term ID | |
'#parent_tid' => 0, | |
'#value_key' => 'tid', | |
'#select_parents' => TRUE, | |
'#attached' => array( | |
'js' => array($term_reference_tree_path . '/term_reference_tree.js'), | |
'css' => array($term_reference_tree_path . '/term_reference_tree.css') | |
), | |
'#attributes' => array('class' => array('field-widget-term-reference-tree')), | |
'#element_validate' => array('_term_reference_tree_widget_validate'), | |
'#value' => array(), | |
'#filter_view' => '', | |
'#token_display' => '' | |
); | |
} | |
} |
4) You should change the
YOUR_MODULE_NAME to your custom module name
your-toxonomy-field to your exposed taxonomy field
'#vocabulary' => taxonomy_vocabulary_load(3), Instead '3' you should keep your vocabulary ID.
Thanks!
ReplyDeleteIs this for d7 or d8? Is there a d8 version - I'd love to get this working on my site. having trouble signing up, but my email is ddowell617@gmail.com
ReplyDeletesorry - also is vocabulary ID the taxonomy vocabulary machine name?
ReplyDelete