diff --git a/taxonomy_menu.module b/taxonomy_menu.module
index 06f295d..5ac6fbe 100644
--- a/taxonomy_menu.module
+++ b/taxonomy_menu.module
@@ -69,6 +69,24 @@ function taxonomy_menu_form_alter(&$form, &$form_state, $form_id) {
'#description' => t('The path will be taxonomy/term/tid if Default has been selected.
The menu path will be passed through drupal_get_path_alias() function so all aliases will be applied.'),
);
+ $form['taxonomy_menu']['expand'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Expand tree by defaut'),
+ '#default_value' => isset($form['vid']['#value']) ? variable_get('taxonomy_menu_expanded_' . $form['vid']['#value'], TRUE) : 0,
+ );
+
+ $form['taxonomy_menu']['hide_empty_terms'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Only show child terms where nodes exist'),
+ '#default_value' => isset($form['vid']['#value']) ? variable_get('taxonomy_menu_hide_empty_terms_' . $form['vid']['#value'], FALSE) : 0,
+ );
+
+ $form['taxonomy_menu']['show_nodes'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Add all linked nodes to each terms'),
+ '#default_value' => isset($form['vid']['#value']) ? variable_get('taxonomy_show_nodes_' . $form['vid']['#value'], FALSE) : 0,
+ );
+
//get taxonomy menu form options
//$form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($form['vid']['#value']);
//
@@ -156,6 +174,15 @@ function taxonomy_menu_vocab_submit($form, &$form_state) {
variable_set($variable_name, $value);
}
+ // Set the default expand option on the menu
+ variable_set('taxonomy_menu_expanded_' . $vid, $form_state['values']['taxonomy_menu']['expand']);
+
+ // Set the default nodes exist option on the menu
+ variable_set('taxonomy_menu_hide_empty_terms_' . $vid, $form_state['values']['taxonomy_menu']['hide_empty_terms']);
+
+ // Add all nodes to linked to each terms
+ variable_set('taxonomy_show_nodes_' . $vid, $form_state['values']['taxonomy_menu']['show_nodes']);
+
// If the menu hasn't changed and the menu is disabled then do not do anything else.
if ($form_state['values']['taxonomy_menu']['options']['rebuild'] ||
$changed_menu ||
@@ -518,7 +545,38 @@ function taxonomy_menu_handler($op, $args = array(), $node = NULL, $item = array
// Update the menu and return the mlid if the remove element is not true
if ($op != 'delete') {
- return _taxonomy_menu_save($item);
+ $mlid = _taxonomy_menu_save($item);
+
+ // Add nodes to menu
+ if (variable_get('taxonomy_show_nodes_' . $item['vid'], FALSE)) {
+ $results = db_query("SELECT ti.nid FROM {taxonomy_index} as ti INNER JOIN {node} as n on n.nid=ti.nid WHERE ti.tid = :tid ORDER by n.title", array(':tid' => $item['tid']));
+ foreach ($results as $key => $result) {
+ $node = node_load($result->nid);
+ $exist = NULL;
+ if ($node) {
+ $exist = db_query("SELECT m.mlid FROM {menu_links} as m WHERE link_path=:path AND plid=:mlid", array (':path' => 'node/'. $node->nid, ':mlid' => $mlid));
+ if ($exist->rowCount() == 0) {
+ $node_item = array(
+ 'link_title' => $node->title,
+ 'menu_name' => $item['menu_name'],
+ 'plid' => $mlid,
+ 'weight' => $key,
+ 'expanded' => 0,
+ 'module' => 'taxonomy_menu',
+ 'link_path' => 'node/'. $node->nid,
+ );
+ menu_link_save($node_item);
+ }
+ }
+ }
+ }
+
+ return $mlid;
+ } else {
+ // Delete nodes to menu
+ if (variable_get('taxonomy_show_nodes_' . $item['vid'], FALSE)) {
+ // TODO
+ }
}
}
@@ -545,7 +603,7 @@ function taxonomy_menu_handler($op, $args = array(), $node = NULL, $item = array
function _taxonomy_menu_save($item) {
$insert = TRUE;
//create the path.
- $path = taxonomy_menu_create_path($item['vid'], $item['tid']);
+ $path = $item['link_path'] ? $item['link_path'] : taxonomy_menu_create_path($item['vid'], $item['tid']);
// get the parent mlid: this is either:
// - the parent tid's mlid
// - the vocab menu item's mlid
@@ -804,15 +862,16 @@ function _taxonomy_menu_create_item($args = array(), $node) {
* @param $vid
* @return boolean
*
+ */
function _taxonomy_menu_children_has_nodes($tid, $vid) {
$children = taxonomy_get_children($tid, $vid);
+ $result = TRUE;
foreach ($children as $tid => $term) {
- if (_taxonomy_menu_term_count($tid) > 0) {
- return FALSE;
- }
+ if ($result) $result = _taxonomy_menu_term_count($tid) == 0;
+ if ($children && $result) $result = _taxonomy_menu_children_has_nodes($tid, $vid);
}
- return TRUE;
-}*/
+ return $result;
+}
/**
* Helper function for insert and update hooks
@@ -823,12 +882,9 @@ 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
- // @TODO Needs Updating
- //$num = _taxonomy_menu_term_count($item['tid']);
+ $num = _taxonomy_menu_term_count($item['tid']);
//if hide menu is selected and the term count is 0 and the term has no children then do not create the menu item
- // @TODO Needs updating
- /*
if ($num == 0 &&
variable_get('taxonomy_menu_hide_empty_terms_' . $item['vid'], FALSE) &&
_taxonomy_menu_children_has_nodes($item['tid'], $item['vid'])) {
@@ -844,7 +900,7 @@ function _taxonomy_menu_item($item) {
$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'], '');