Thanks stBorchert for the module ! It works great.
I have a taxonomy where the terms have a boolean field called published. I want to show only published terms in the exposed filter.

eg - Consider a hierarchy with published value in parentheses A (1) > B (1) > C (1)
A(1) > D (0) > E(0)
I only want to expose the following list in my filter : A > B > C

I tried to find a hook under shs_term_get_children() or taxonomy_get_tree() to modify the children being returned. Any suggestions ?

Comments

stBorchert’s picture

Version: 7.x-1.5 » 7.x-1.x-dev
Status: Active » Needs review

I've committed a change to shs to allow altering a) the callbacks used to get the data with JSON and b) the list of terms retrieved by shs.

<?php
function hook_shs_term_get_children_alter(&$terms, $alter_options) {}
?>

$alter_options is an associative array containing vid, parent and settings from function shs_term_get_children().

manasiv’s picture

Your change is working just fine. I am still to test this for a site with high number of categories. I will check for any performance issues and reply back.

Thanks !

stBorchert’s picture

Status: Needs review » Fixed

Hi.
I guess you didn't find any further issues with this new functionality so setting this to "fixed" ;)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

manasiv’s picture

Yes, this works just fine for ~1000 category tree :) I was able to implement this hook in the site's custom module and conditionally filter the list of categories.

prash1485’s picture

Issue summary: View changes

This is exactly I was looking for. I am not really good with programming.

Can someone please help me how can I use this hook to hide(alter) empty categories on exposed filter which do not have any nodes associated with them.

<?php
function hook_shs_term_get_children_alter(&$terms, $alter_options) {}
?>

Thanks in davance

manasiv’s picture

Hi prash1485,

You can use this as an example. Following code goes in mymodue.module file.

function mymodule_shs_term_get_children_alter(&$terms, $alter_options) {
$vid = $alter_options['vid'];
$parent = $alter_options['parent'];
$list = $terms[$vid][$parent][0];
$temp = array_keys($list);

//Modify terms
foreach($temp as $tid)
{
$term = taxonomy_term_load($tid);
if (isset($term->field_published[LANGUAGE_NONE])){
$published = $term->field_published[LANGUAGE_NONE][0]['value'];
 if($published == 0)
 {
//Remove unwanted terms from the list
unset($terms[$vid][$parent][0][$tid]);
unset($terms[$vid][$tid]);
} 
} 
}
//Return an updated list of terms
return $terms;
}
Nikolino’s picture

I've the same problem... where I have to put this code? in shs.module? which line?
Thank you!

stBorchert’s picture

You'll have to add the code to a custom module and rename the function according to your module's name ...

nastassia’s picture

To save the next person a couple of minutes, here is a link to a tiny custom module I made.

In my case, all content is always published. So I only needed to check if a taxonomy term has associated content. And the method suggested by manasiv (#7) became:

function mymodule_shs_term_get_children_alter(&$terms, $alter_options) {
	
	$vid = $alter_options['vid'];
	$parent = $alter_options['parent'];
	$list = $terms[$vid][$parent][0]; 
	$temp = array_keys($list); 
	
	//Modify terms
	foreach($temp as $tid) {
		//get taxonomy term object
		$term = taxonomy_term_load($tid); 
		//get nodes attached to a term
		$node_matches = taxonomy_select_nodes($tid);		
		if (sizeOf($node_matches) == 0) {
			unset($terms[$vid][$parent][0][$tid]);
			unset($terms[$vid][$tid]);
		}
	};

	//Return an updated list of terms
	return $terms;
}

Thanks!