Hi, i'm using option 'Display number of items' to display number of posts (nodes) per term, but that option doesn't display summary of both the child terms post and parent terms post.

For example, we have vocabulary 'fruits and vegetables' and inside that vocabulary term 'fruits' and term 'vegetables', which are parent terms. If inside parent term 'fruit' we have child term 'apple' and we have 3 nodes per term 'apple' but none for parent term 'fruit', when the parent term is collapsed, it would be appear as if there was nothing posted for term 'fruit', so no one will open that menu.

It would be very helpfull if parent term would sum number of nodes per all child terms including it's own, and display that value, so for example, if there was 3 post per apple, one post per fruit, the term fruit would display next to it number (4).

I don't have experence with building modules so i was trying to solve the issue previously mentioned by adding some code in taxonomy_menu.module, but without success. Below is what i have added to the function _taxonomy_menu_item($item).

/**
 * Helper function for insert and update hooks
 * @param $item
 * @return unknown_type
 */
function _taxonomy_menu_item($item) {
	
  //if tid is 0 then do not chagne any settings
  if ($item['tid'] > 0) {
    //get the number of node attached to this term
    $num = _taxonomy_menu_term_count($item['tid']);

	//what i have added starts here
	if (_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
			$children = taxonomy_get_children($item['tid'], $item['vid']);		
			foreach ($children as $tid => $term) {
    			if (_taxonomy_menu_term_count($tid) > 0) {
					$num += _taxonomy_menu_term_count($tid); 
								
    			}
  			}     		
    	}
//ends here
    //if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
    if ($num == 0 &&
        variable_get('taxonomy_menu_hide_empty_terms_'. $item['vid'], FALSE) &&
        _taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {

        $item['remove'] = TRUE;
        return $item;
    }

    //if display number is selected and $num > 0 then change the title
    if (variable_get('taxonomy_menu_display_num_'. $item['vid'], FALSE)) {
      //if number > 0 and display decendants, then count all of the children
      if (variable_get('taxonomy_menu_display_descendants_'. $item['vid'], FALSE)) {
        $num = taxonomy_term_count_nodes($item['tid']);
      }
      $item['name'] .= " ($num)";
    }
  } elseif ($item['tid'] == 0) {
    //if custom name is provided, use that name
    $custom_name = variable_get('taxonomy_menu_voc_name_'. $item['vid'], '');
    if (!empty($custom_name)) {
      $item['name'] = $custom_name;
    }
  }

  return $item;
}

So does anyone have a solution for the above mentioned?

Comments

2id’s picture

Status: Active » Closed (fixed)

My mistake that option already exists, under the display descendants.