Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Does anyone have any tips for sorting the order of nodes in a view based on the weight given to the node's menu listing?
I have already used CCK to define a field to give the nodes a custom user defined "weight" but it seems a waste of energy to have to define this rather than use the weight already defined for the menu listing, since that is the order I want to use in my view display. . .
Any help is greatly appreciated. . .
I'm new to this so I hope the question makes sense!
Comment | File | Size | Author |
---|---|---|---|
#48 | views_menu_sort.zip | 6.88 KB | Mitsuko |
#42 | views_menu_sort.tar_.gz | 6.47 KB | kaido.toomingas |
#8 | views_menu_sort.zip | 8.34 KB | marcuslim |
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedCurrently Views doesn't offer a method to support by menu weight unless it's in a book outline. This might be something that can come in the future. Changing to feature request.
Comment #2
Bartezz CreditAttribution: Bartezz commentedsubscribing
Comment #3
k3vin CreditAttribution: k3vin commentedsubscribing
Comment #4
prom3theus CreditAttribution: prom3theus commentedI'm using a custom solution with a CCK addon (computed field). Add a computed field to the content type what you need to orderable by menu weight, and use the below code as the computed field's 'computed code' value:
Check in the 'Display this field' and 'Store using the database settings below', use Data Type: int, set Data Length: 4 and save. After this, the saved field appears in views as a normal CCK field, but contents the associated node's menu item weight - now you can use as filter or ordering display.
Ofcourse you need to reupdate all content in this content type - this is just for fun :'(
And, BTW subscribing too :)
Comment #5
Bartezz CreditAttribution: Bartezz commentedThanx for sharing!!! Will try asap!
Comment #6
Bartezz CreditAttribution: Bartezz commentedCheck this: http://drupal.org/node/343299
Comment #7
dindon CreditAttribution: dindon commentedsubscribing
Comment #8
marcuslim CreditAttribution: marcuslim commentedI've written a very simple module that does this, while working on a project that requires sorting nodes according to their menu listing weight. Simply install the module, enable it, then in Views UI, under Sort Criteria, add the field 'Menu sorted nodes: Menu item weight' and choose ascending. Then the nodes for the view will be displayed according to their menu listing's weights, in ascending order from least weight to most weight.
Comment #9
Bartezz CreditAttribution: Bartezz commentedThanx!
Comment #10
Colin_T CreditAttribution: Colin_T commentedThis looks like a duplicate of #110021: Add node weight to view's sort criteria
Comment #11
Colin_T CreditAttribution: Colin_T commentedWhile it is a duplicate, the solution there doesn't work for me.
Comment #12
merlinofchaos CreditAttribution: merlinofchaos commentedThat gives you no right to litter.
Comment #13
Colin_T CreditAttribution: Colin_T commentedI was assuming that the duplicate I pointed out (assigned to 4.x & resolved by design) was a distinct entity from this one.
Should this work for 6.x?
Comment #14
marcuslim CreditAttribution: marcuslim commentedYes, it works for 6.x. I tested it on Druapl 6.10.
Comment #15
Colin_T CreditAttribution: Colin_T commentedmarcuslim - Thanks - hadn't properly realised I needed the weight module as well to achieve that.
Your views sort module seems to do the job nicely without requiring an extra field on the input forms, which seems a neater solution to me.
Comment #16
Matti303 CreditAttribution: Matti303 commentedMany thanks for the module Marcuslim!! \o/
Comment #17
Plascual CreditAttribution: Plascual commentedThank you marcuslim! This is a very nice add-on.
But I have a litte problem with results order since I use Nice_Menus drop-down menus. The parent menu links to the same node as the first child menu, so views_menu_sort module seems to add one more result. That node appears twice in the views list.
I was wondering if it would be complicated to add settings to the module based on menu deepness ? Or maybe just eliminate those duplicates ?
Thanks in advance !
Plascual
EDIT: I realized that setting Distinct => 'Yes' in Views basic settings corrects this.
Comment #18
glennr CreditAttribution: glennr commented#8 worked well for me too. Thanks, marcuslim.
Comment #19
manumilou CreditAttribution: manumilou commentedyes very useful module !
Comment #20
BrightBoldPerfect, marcuslim! Exactly what I needed.
Comment #21
Leo Pitt CreditAttribution: Leo Pitt commented@marcuslim #8
Thanks for the views_menu_sort module.
Elegant, light-weight, and behaves exactly as I would have wanted.
Seems to work fine on Drupal 6.14 with Views 2.6
Comment #22
obrigado CreditAttribution: obrigado commentedMarcuslim, thanks for your great module. Any chance of starting a project page for it, or maybe merging it into Views 3?
Comment #23
monti CreditAttribution: monti commentedhave tried to use "views_menu_sort", provided by marcuslim (comment #8 above).
It is not working for me. I am not sure why. I use the weight module, which outside of views works properly. Inside views, I used marcuslim's function, but there is no impact. any direction, idea?
Cheers !
Comment #24
woodhous@email.unc.edu CreditAttribution: woodhous@email.unc.edu commentedHi Maruslim,
Thank you for the module. I am unable to set the weight on the nodes I need to sort in my view because they don't have a menu associated with them. The Page content type does not save the weight unless I assign a menu title. My menu nodes use a view reference to pull in related content based on taxonomy and I would like the ability to sort the nodes in the view reference. Is there any way to set the menu weight on a content item without it showing up on a menu.
Comment #25
bwoods CreditAttribution: bwoods commentedSweet, #8 worked for me as well. Thanks!
Comment #26
marcushenningsen CreditAttribution: marcushenningsen commentedWorks beautifully! Thanks a lot. Changing status.
Marcus
Comment #27
merlinofchaos CreditAttribution: merlinofchaos commentedErr, RTBC indicates that there is a patch. This module should probably be its own thing.
Comment #28
BenMirkhah CreditAttribution: BenMirkhah commentedAfter trying out many ways I like the #4 approach however "weight" alone will not be enough in a multi-level menu because then many items will have the same weight so instead I tweaked it to use p1 + p2 as explained here: http://drupal.org/node/141866
as well as depth and weight to get it working in perfect order for a 3 level menu...
$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';
also in order to do a force update on all existing nodes to calculate the computed cck-field (varchar 20):
print "start ";
$result = db_query("SELECT nid FROM {node}");
while($nid = db_fetch_object($result)) {
$node = node_load($nid->nid);
node_save($node);
}
print "end ";
Comment #29
mattiasj CreditAttribution: mattiasj commented#8 worked great for us, a lightweight and clean solution - thanks!
Comment #30
cleanthes CreditAttribution: cleanthes commentedThanks @marcuslim. Worked first time, perfectly.
Comment #31
Nimo CreditAttribution: Nimo commentedThanks @marcuslim. Wonderful!
Comment #32
jdln CreditAttribution: jdln commentedBenMirkhah's code from #28 works for me.
However im manually having to update all my nodes. How do I do a 'force update on all existing nodes'? What do I do with the code below?
Thanks
Comment #33
jdln CreditAttribution: jdln commentedAnyone?
Comment #34
Bartezz CreditAttribution: Bartezz commentedCreate a node with a php filter instead of filtered or full html (need to enable a core module for this), copy paste code (need to put between
and
tags. View the node et voila :) Backup first!Cheers
Comment #35
jdln CreditAttribution: jdln commentedWorking. Thanks.
Comment #36
jdln CreditAttribution: jdln commentedThe code from #28 has stopped working for me, and not for the first time. My CCK computed filed settings are identical to an older version of the site which is working fine, so I guess this code is unreliable?
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.
Ive experimented with 'Data Length' but what should this value be?
Ive created a support request in Computed Fields:
http://drupal.org/node/1005094
Comment #37
Bartezz CreditAttribution: Bartezz commentedHave you tried #8?
Comment #38
jdln CreditAttribution: jdln commentedYes, but it doesn't work for a hierarchical menu.
Comment #39
Bartezz CreditAttribution: Bartezz commentedAh, that could be right indeed. I've only used it for first level menu nodes.... sorry.
Cheers
Comment #40
remaye CreditAttribution: remaye commented#8 works (still) very well for me!
Thanks @marcuslim !
Is this module maintained ?
And is there any hope to have this feature integrated within Draggable Views ?
It would be great, in addition to sort items by menu weight, to have the ability to re-order the menu itself within the same view ...
Or is there any other solution to reorder menus without having to change the menu weight of each item in the node form or giving access to the menu administration itself ?
If any have a tip ... thanks!
Comment #41
squarecandy CreditAttribution: squarecandy commented#8 works great. (single level menu) Thanks!!!
Comment #42
kaido.toomingas CreditAttribution: kaido.toomingas commentedThanks a lot @marcuslim and here are little changes to get this work with domain_access module. Worked fine with drupal 6.19 and views 2.x :).
Comment #43
jonaswouters CreditAttribution: jonaswouters commentedIs this available for 7?
Comment #44
jdln CreditAttribution: jdln commentedSorry to backtrack, but is there a solution for a hierarchical menu? Is there some work around?
Thanks
Comment #45
ofktoubro CreditAttribution: ofktoubro commented#8 tnx
Comment #46
biwashingtonial CreditAttribution: biwashingtonial commentedJust discovered this: http://drupal.org/project/menu_node_views
It provides quite a few Views handlers based on nodes' associated menu links, including sort (which does seem to work in hierarchical menus, based on a short test drive).
Comment #47
steven_kropp CreditAttribution: steven_kropp commentedAny ideas on how to take the module in #8 and make it work for Drupal 7, as far as I can see there is no way STILL to sort by menu weight for drupal 7... seems to me like a very necessary config.
Comment #48
Mitsuko CreditAttribution: Mitsuko commentedThis is a version for Drupal 7. Menu node module is required. It provides in views :
- menu weight field
- menu weight sort filter
- menu link id filter to exclude particular entries (based on mlid)
Comment #49
Liaz CreditAttribution: Liaz commented#8 worked perfect and easy for me.
Thanks a lot @marcuslim
Comment #50
2pha#46 worked for me, drupal 7
Comment #51
jasonawantHello,
FWIW, for Drupal 7, the following modules provided menu: depth and menu: weight, which allowed me to sort view results according to their menu item hierarchy.
http://drupal.org/project/menu_node
http://drupal.org/project/menu_node_views
Jason
Comment #52
reallybigears CreditAttribution: reallybigears commentedmarcuslim - thank you. I just installed your module and so far so good. Really can't believe this isn't a built in option. Excellent. thanks
Comment #53
ricancie CreditAttribution: ricancie commentedThanks soooooo much marcuslim!!!!
Comment #54
Wtower CreditAttribution: Wtower commentedAnother possibility for this is described in: http://drupal.stackexchange.com/questions/106538/how-to-sort-content-in-...
Comment #55
AdamPS CreditAttribution: AdamPS commented@jdln and anyone else interested
As far as I can tell, none of these solutions work for a hierarchical menu, assuming like me you are looking for depth-first ordering.
Comment #56
nno CreditAttribution: nno commented#48 works great. Thank you very much
Comment #57
thomaswalther CreditAttribution: thomaswalther commentedMy solution, sorting all nodes in views like the sorting from their menu sorting: