Fix for Set Default Menu for node with no menu

  1. /**
  2.  * The all powerful preprocess function, so handy.
  3.  * Set the new item here to be sure its ready for rendering.
  4.  */
  5. function mysite_preprocess_node(&$variables) {
  6.   $node = $variables['node'];
  7.  
  8.   // set default menu if needed.
  9.   _set_default_menu($node->nid);
  10. }
  11.  
  12. /**
  13.  * For most items some menu is set. But for those that do not
  14.  * we need to set one.  
  15.  */
  16. function _set_default_menu($nid){
  17.   if (!node_has_menu("node/$nid")) {
  18.     $menu = _find_default_menu($nid);
  19.   }
  20. }
  21.  
  22. // does node have menu
  23. function node_has_menu($path) {
  24.   $sql = "SELECT 'TRUE' FROM drupal_menu_links ";
  25.   $sql .= "WHERE menu_name IN ('primary-links') AND ";
  26.   $sql .= "link_path = '%s'";
  27.   $result = db_result(db_query($sql, $path));
  28.   return $result;
  29. }
  30.  
  31. /**
  32.  * Here the default menu is found.  You could do find your default menu several different ways,
  33.  * like using a taxonomy already set on the node or any other field set on the node, or even a default
  34.  * variable.
  35.  */
  36. function _find_default_menu($nid) {
  37.   $sql = "SELECT link_path FROM drupal_menu_links WHERE plid = 0 AND ";
  38.   $sql .= "menu_name IN ('primary-links') AND link_title = '%s'";
  39.   $link = db_result(db_query($sql, 'Some Menu'));
  40.  
  41.   $router_item = menu_get_item($link);
  42.   menu_set_item("node/$nid", $router_item);
  43. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.