You are here

function _nodehierarchy_create_menu in Node Hierarchy 6

Same name and namespace in other branches
  1. 5 nodehierarchy.module \_nodehierarchy_create_menu()

Create a menu item if the user selects one.

1 call to _nodehierarchy_create_menu()
nodehierarchy_nodehierarchyapi in ./nodehierarchy.module
Implementation of hook_nodehierarchyapi(). Responds to own api calls.

File

./nodehierarchy.module, line 1006

Code

function _nodehierarchy_create_menu(&$node) {

  // If the menu is to be (re)created set the values to that derived by the hierarchy.
  if (function_exists('menu_link_save') && variable_get('nodehierarchy_menus', TRUE) && (user_access('administer menu') || variable_get('nodehierarchy_menu_noadmin', FALSE)) && @$node->nodehierarchy_create_menu) {
    $item = $node->menu;
    if ($parent_menu = _nodehierarchy_get_menu($node->parent)) {
      $item['plid'] = $parent_menu['mlid'];
    }
    else {
      $item['parent'] = variable_get('nodehierarchy_menus_default', 'primary-links:0');
      list($item['menu_name'], $item['plid']) = explode(":", $item['parent']);
    }
    $item['link_title'] = trim($node->title);
    $item['link_path'] = "node/{$node->nid}";

    // Editable menu weight range used to start at -10.
    $item['weight'] = $node->order_by - 11;
    if (!menu_link_save($item)) {
      drupal_set_message(t('There was an error saving the menu link.'), 'error');
    }

    // Prevent the menu.module saving the menu itself in case
    // the module is called after this one.
    unset($node->menu);

    // Prevent the this module from attempting to create a menu twice.
    $node->nodehierarchy_create_menu = FALSE;
  }
}