I have a list of menu items in a book. It looks like this

Body
Introduction
Page 1
Page 2

I want to nodes to be listed such as

Page 1
Introduction
Body
Page 2

When I looked at the menu_links table in the database I can see that the weight column shows the order of the pages. So I should be able to alter the weight value using code to change reorder the list of nodes in the menu. Here is my code:

29   function chris_reorganize_project_pages($node) {
30    $p_nid = $node->nid; 
31    if ($p_nid !== 0){
32      $id_nid_plus_1 = $p_nid + 1;
33      $node = node_load($id_nid_plus_1);
34      $node->menu_links->weight = -10;
35          node_save($node);
36         }
37   }

Unfortunately I get the following error when I use this code.

Strict warning: Creating default object from empty value in chris_reorganize_project_pages() (line 34 of /Users/website/sites/default/modules/chris_project_reorder/chris_project_reorder.rules.inc).

I am sure I am calling the $node wrong or my attempt to access the menu through the $node is not the way to do it.

Any ideas?

Comments

So would I do something like

So would I do something like this?

function chris_reorganize_project_pages($node) {
$p_nid = $node->nid;
chris_menu_alter();
}

function chris_menu_alter($p_nid){
if ($p_nid !== 0){
$id_nid_plus_1 = $p_nid + 1;

$items['/admin/structure/block/manage/book/navigation/' .$id_nid_plus_1]['weight'] = -15;

entityfieldquery()
}
}

Spineless

Using hook_menu_link_alter()

I am attempting to change the weight of the node link. I am using the example of hook_menu_link_alter() by looking at the following:

http://api.drupal.org/api/drupal/modules!user!user.module/function/user_menu_link_alter/7

Looking at this code I see a very similar approach where the ['plid'] is changed. So I created the following code:

function chris_menu_link_alter(&$link){
global $parent_nid;
$node = node_load($parent_nid);
$id_nid = $node->nid;
$id_nid_plus_2 = $id_nid + 2;
   $eeee = 'node/' .$id_nid_plus_2;
       if($link['link_path']== $eeee){
         $link['weight'] = 3;
         watchdog('reorder-database','The database weight = ' .$link['weight'], array(), WATCHDOG_WARNING);
          }
   watchdog('reorder-database','The function is finished.  Weight = ' .$link['weight'], array(), WATCHDOG_WARNING); 
}

The code up to the IF statement works for sure. The code within the IF statement does not appear to trigger as I cannot see the "The database weight =" Watchdog statement in the system log.

I attempted a number of versions of this code as well but all do not work. Here is what I tried.

attempt 2

       if($link['link_path']== 'node/' .$id_nid_plus_2){
         $link['weight'] = 3;
         watchdog('reorder-database','The database weight = ' .$link['weight'], array(), WATCHDOG_WARNING);
          }

attempt 3

       if($link['link_path']== 'node/198'){
         $link['weight'] = 3;
         watchdog('reorder-database','The database weight = ' .$link['weight'], array(), WATCHDOG_WARNING);
          }

Again I am attempting to change the weight value of node 198 to equal 3 so it is placed at a different location in the menu.

Why doesn't the If statement trigger, or save or display the watchdog message?

Spineless
nobody click here