diff --git a/hierarchical_select.js b/hierarchical_select.js index 059fcc8..4d3ae1f 100644 --- a/hierarchical_select.js +++ b/hierarchical_select.js @@ -471,7 +471,14 @@ Drupal.HierarchicalSelect.update = function(hsid, updateType, settings) { switch (updateType) { case 'update-hierarchical-select': var value = $('#'+ settings.select_id).val(); - var lastUnchanged = parseInt(settings.select_id.replace(/^.*-hierarchical-select-selects-(\d+)$/, "$1")) + 1; + + if (settings.select_id.substring(settings.select_id.length - 3, settings.select_id.length - 1) == '--') { + var lastUnchanged = parseInt(settings.select_id.replace(/^.*-hierarchical-select-selects-(\d+)--.$/, "$1")) + 1; + } + else { + var lastUnchanged = parseInt(settings.select_id.replace(/^.*-hierarchical-select-selects-(\d+)$/, "$1")) + 1; + } + var optionClass = $('#'+ settings.select_id).find('option[value='+ value +']').attr('class'); // Don't do anything (also no callback to the server!) when the selected diff --git a/hierarchical_select.module b/hierarchical_select.module index 731267d..0689e8a 100644 --- a/hierarchical_select.module +++ b/hierarchical_select.module @@ -349,11 +349,11 @@ function _hs_process_determine_hsid($element, &$form_state) { // generate a new one based on the last HSID used (which is // stored in form state storage). if (!isset($element['#value']) || !is_array($element['#value']) || !array_key_exists('hsid', $element['#value'])) { - if (!isset($form_state['storage']['hs']['last_hsid'])) { - $form_state['storage']['hs']['last_hsid'] = -1; + if (!isset($_SESSION['storage']['hs']['last_hsid'])) { + $_SESSION['storage']['hs']['last_hsid'] = -1; } - $form_state['storage']['hs']['last_hsid']++; - $hsid = $form_state['storage']['hs']['last_hsid']; + $_SESSION['storage']['hs']['last_hsid']++; + $hsid = $_SESSION['storage']['hs']['last_hsid']; } else { $hsid = check_plain($element['#value']['hsid']); @@ -1784,6 +1784,13 @@ function _hierarchical_select_hierarchy_generate($config, $selection, $required, $hierarchy->levels[$depth] = array('label_' . $depth => $label) + $hierarchy->levels[$depth] + $children; $hierarchy->levels[$depth] = _hierarchical_select_apply_entity_settings($hierarchy->levels[$depth], $config); + + // Remove the level if it no longer has any children. This is possible + // if _hierarchical_select_apply_entity_settings removes the terms + // because they have no entities. + if (count($hierarchy->levels[$depth]) <= 1) { + unset($hierarchy->levels[$depth]); + } } } } @@ -1856,6 +1863,9 @@ function _hierarchical_select_apply_entity_settings($level, $config) { $special_items['exclusive'] = array_keys(array_filter($config['special_items'], '_hierarchical_select_special_item_exclusive')); $special_items['none'] = array_keys(array_filter($config['special_items'], '_hierarchical_select_special_item_none')); } + else { + $special_items = array(); + } // Only do something when the entity_count or the require_entity (or both) // settings are enabled. diff --git a/modules/hs_taxonomy.module b/modules/hs_taxonomy.module index 2fc3fb4..76fcdf9 100644 --- a/modules/hs_taxonomy.module +++ b/modules/hs_taxonomy.module @@ -625,6 +625,18 @@ function hs_taxonomy_hierarchical_select_children($parent, $params) { } } + // If the term has no nodes associated with it, and it has no child terms, + // remove it. + foreach ($terms as $key => $term) { + $count = hs_taxonomy_hierarchical_select_entity_count($term->tid, array()); + if ($count == 0) { + $children = taxonomy_get_children($term->tid); + if(empty($children)) { + unset($terms[$key]); + } + } + } + return _hs_taxonomy_hierarchical_select_terms_to_options($terms); } @@ -749,22 +761,18 @@ function hs_taxonomy_hierarchical_select_create_item($label, $parent, $params) { } } - /** * Implementation of hook_hierarchical_select_entity_count(). */ -/* -// TODO: port this to D7. function hs_taxonomy_hierarchical_select_entity_count($item, $params) { // Allow restricting entity count by node type. - if ($params['entity_count_for_node_type']) { + if (isset($params['entity_count_for_node_type'])) { return hs_taxonomy_term_count_nodes($item, $params['entity_count_for_node_type']); } else { return hs_taxonomy_term_count_nodes($item); } } -*/ /** * Implementation of hook_hierarchical_select_implementation_info(). @@ -1040,8 +1048,7 @@ function _hs_taxonomy_hierarchical_select_get_tree($vid, $parent = 0, $depth = - } /** - * Drupal core's taxonomy_term_count_nodes() is buggy. See - * http://drupal.org/node/144969#comment-843000. + * Count the number of nodes assigned to a term, or its children terms. */ function hs_taxonomy_term_count_nodes($tid, $type = 0) { static $count; @@ -1055,14 +1062,19 @@ function hs_taxonomy_term_count_nodes($tid, $type = 0) { } if (!isset($count[$type][$tid])) { - if (is_numeric($type)) { - // TODO Please convert this statement to the D7 database API syntax. - $count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.tid IN (%s)"), implode(',', $tids))->fetchField(); - } - else { - // TODO Please convert this statement to the D7 database API syntax. - $count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids))->fetchField(); + $query = db_select('taxonomy_index', 't'); + $query->innerJoin('node', 'n', '(t.nid = n.nid)'); + $query->fields('n', array('nid')); + $query->condition('n.status', 1, '='); + $query->condition('t.tid', implode(',', $tids)); + $query->distinct(TRUE); + + if (!is_numeric($type)) { + $query->condition('n.type', $type, "="); } + + $query->execute(); + $count[$type][$tid] = $query->countQuery()->execute()->fetchField(); } return $count[$type][$tid]; }