I need a view to have a sort order which is the same as the menu's order. So im trying to get the menu weight of nodes into a computed field.

For a menu of just 1 level this seems to work fine:

$res = db_fetch_array(db_query("SELECT weight FROM {menu_links} WHERE link_path = 'node/%s'", $node->nid));
$weight = is_array($res) ? $res['weight'] : 0;
$node_field[0]['value'] = $weight;

However I have a multi-level menu. I got this code from post 28 on this forum but it doesn't work for me:
http://drupal.org/node/313140

$res = db_fetch_array(db_query("SELECT * FROM {menu_links} WHERE link_path = 'node/%s'", $node->nid));
$node_field[0]['value'] = is_array($res) ? (
str_pad($res['p1'], 4, '0', STR_PAD_LEFT).'-'.
str_pad($res['p2'], 4, '0', STR_PAD_LEFT).'-'.
str_pad($res['depth'], 4, '0', STR_PAD_LEFT).'-'.
str_pad(100+($res['weight']), 4, '0', STR_PAD_LEFT) ) : '0000-0000-0000-0000';

Just to be clear, if this is my menu:
Link1
-LinkA
-LinkB
Link2
-LinkC
-LinkD

Then I need the order to be this:
Link1
LinkA
LinkB
Link2
LinkC
LinkD

Thanks

Comments

jdln’s picture

Status: Active » Fixed

Sorry, done some more testing and actually I think the code is correct.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jdln’s picture

Status: Closed (fixed) » Active

OK, its stopped working again. Its identical to an older version that is working fine so it seems the code is unreliable.

Ive installed some more modules in the newer version, could their be conflicts? As this module accesses the database directly im assuming this is unlikely?

Ive ticked this 'Store using the database settings below (required for Views use)'.

And the data type is 'int'.

Ive experimented with different values for 'Data Length', what should the value be and should it still work if I choose a bigger than necessary number?

Thanks

jdln’s picture

I forgot to mention, by making the field visible I can see in views it doesn't have a value.

On the node's display settings ive set the field to have a label and be visible but I cant see it on the node view page. It seems weird not even the label is shown.

jdln’s picture

If I make the data type varchar and length 64 then I can see the values below:

node1
computed_order_af: 2101-2102-0002-0053
node2
computed_order_af: 2101-2103-0002-0052
node3
computed_order_af: 2101-2104-0002-0051
node4
computed_order_af: 2101-2171-0002-0050

The menu order is node4 node3 node2 node1. So the last 4 numbers are actually correct, but the 2nd 'row' are causing the problem.

mmjvb’s picture

Issue summary: View changes
Status: Active » Closed (outdated)