I posted about this earlier without fully comprehending the issue. #841814: Menu Weight doesn't coexist with non-taxonomy menu items

With the new changes to menu weighting the menu is now created rather well in mimicking the taxonomy weight in the menu weight.

This particular problem arises if you
-edit a taxonomy menu directly (not the taxonomy itself)
-add a non-taxonomy menu item manually
-then drag the order of your new item

Now if you edit a taxonomy term/menu based node upon saving the order will become out of wack. The reason is because when you use the drag handles in editing a menu Drupal resets ALL the menu item weights based of -50 being the lowest. When you manually add menu items the weight is default 0. The default lowest weight that taxonomy menu uses when creating the menu items is also 0 for the lowest weight.

So a created menu from taxonomy menu will have 0 be the lowest weight ranging upwards. However whenever you use the drag handles to reorder menu items the weights will all be reset based off of -50 instead of 0. Even if you didn't add a new manual menu entry if you dragged in the menu editor the weights would all go down to -50 and then every time you save an associated term node the weight would be set by taxonomy menu in the 0-50 range, resulting in odd ordering until all nodes are saved again.

The explanation is a lot longer than my solution which is to simply subtract 50 from the weight assignment taxonomy menu uses in creating menu items. It seems sort of odd at first however since changing the base functionality of how Drupal performs its weighting when dragging seems a lot more involved it should be a fix without any real downside. Basically the taxonomy created menu would operate on a base weight of -50 to begin with, so adding additional non-taxonomy items to the taxonomy menu wouldn't cause any conflict when dragging.

Menu order would still be the same as the taxonomy order and new non-tax items would default at the bottom of the menu with a 0 weight. Really the problem is Drupal's handling of weights when dragging and the inconsistent setting of base 0 vs -50 however it seems easier to solve here.

Comments

indytechcook’s picture

Nice explanation and simple solution. This needs some good testing. It would be great if it resolves the issues.

NatCromlech’s picture

I have been experiencing this same issue and wanted to help test this patch. However the patch does not seem to be working for me.

root@web3:/var/www/sites/all/modules/taxonomy_menu# patch -p0 < menuweight.patch 
patching file taxonomy_menu.module                                               
Hunk #1 FAILED at 539.                                                           
1 out of 1 hunk FAILED -- saving rejects to file taxonomy_menu.module.rej        
root@web3:/var/www/sites/all/modules/taxonomy_menu# cat taxonomy_menu.module.rej 
*************** function _taxonomy_menu_save($item) {                            
*** 539,545 ****
    $link['link_title'] = $item['name'];
    $link['menu_name'] = $item['menu_name'];
    $link['plid'] = $plid;
-   $link['weight'] = $item['weight'];
    $link['module'] = 'taxonomy_menu';
    $link['expanded'] = variable_get('taxonomy_menu_expanded_'. $item['vid'], TRUE);
    $link['link_path'] = $path;
--- 539,545 ----
    $link['link_title'] = $item['name'];
    $link['menu_name'] = $item['menu_name'];
    $link['plid'] = $plid;
+   $link['weight'] = $item['weight']-50;
    $link['module'] = 'taxonomy_menu';
    $link['expanded'] = variable_get('taxonomy_menu_expanded_'. $item['vid'], TRUE);
    $link['link_path'] = $path;

I have rolled back to the 6.x-2.9 version for now. I will try to manually make the cahnges included in your patch file to taxonomy_menu.module and see how that goes.

Just wanted to let you know about the patch file. As always, thanks for your help, Druapl community.

Nick Lewis’s picture

StatusFileSize
new680 bytes

Rerolled Patch. Behavior is definitely less buggy than before.

NatCromlech’s picture

Thank you so much for doing this. The patch applied without any errors. I am currently testing it.

dstol’s picture

Thanks Nick and Nat.

Please report back here when you're done with testing.

dstol’s picture

Duplicate post.

NatCromlech’s picture

The patch applied just fine. So far, this appears to have resolved the issues I was experiencing on two different sites, using the 6.x-2.9 version of this module. Thank you again for your help.

dstol’s picture

Status: Needs review » Reviewed & tested by the community

Marking as RTBC, I'll commit this later.

dstol’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 6.x-2.x-dev. Thanks.

Status: Fixed » Closed (fixed)

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

loopduplicate’s picture

It took me awhile to find this post. This issue through me for a loop for a while. When do you think you'll release a new stable version with this patch?

hanoii’s picture

I know this one is closed, but.. I am following some issues similar to #992368: Menu order changes when node is tagged with a taxonomy term. , can someone point to where or why the weight is always taken from the taxonomy items? What happens if you change an order of an item manually, it will be overwritten, is that something wanted?

daboo’s picture

I know that this is closed, but I wanted to share a little script that you can save as "updatemenu.php" in your root web folder and either run manually or setup a cron job to call it at a specified interval. In my case, it automatically sets the weights to 0 every 20 minutes, thus reverting to an Alpha sort. It's a simple and effective workaround.

Here's the CRON job:
/usr/bin/wget -O - -q "http://www.mysite.com/updatemenu.php" > /dev/null 2>&1

Here's the script:

$hostname="localhost"; 
$username="username"; 
$password="password"; 
$database="database"; 

// Connect to the database 
mysql_connect($hostname, $username, $password) OR DIE("Unable to connect"); 
mysql_select_db("$database") or die("Unable to select database"); 

$query="UPDATE menu_links SET weight = '0' WHERE module = 'taxonomy_menu'"; 
//  $query="SELECT * FROM menu_links";
$checkresult = mysql_query($query); 
if ($checkresult) echo 'update query succeeded'; 
else echo 'update query failed'; 

mysql_close();