Hi guys,

I'm currently working on a new Drupal 8.0.2 site, I seem to have a problem where the main navigation menu's order seems to keep reverting to alphabetical order instead of the specific fixed order I save it to. Usually when the cache is flushed or when I seem to change pages or edit articles.

I want the order...
Home - News - Campaigns - Events - Organisations - Blogs

however no matter what I do including creating the files in that order from Views pages they always seem to revert back to the alphabetical

Home - Blogs - Campaigns - Events - News - Organisations

Is this a bug or a corrupt setting?
All help appreciated thanks in advance

Comments

slewazimuth’s picture

On your website go to relative url admin/structure/menu/manage/main

That will take you to the "Edit menu Main navigation".

Click on "Show row weights"

The weight value you choose will determine the order the menu item is displayed. Lower values are displayed before higher values for positive values. Negative values are displayed in reverse where a higher negative value is displayed before a lower negative value.

Everytime you click "Save" you'll see the menu displayed with the order of the weight you set.

4Site’s picture

Thanks for your reply, however I know how to set the order, no matter if I use row numbers or drag and drop, the order eventually seems to revert back to alphabetical order. Anyone having similar problems? Regards

slewazimuth’s picture

Eventually means exactly what:

Immediately?
Within 5 minutes?
After cache is rebuilt?

4Site’s picture

Seems after a cache rebuilt mostly, or occasionally when I make changes to structure of articles in Display suite or Views, but I think that's because that automatically rebuilds the cache maybe?

quindio’s picture

I am having a similar problem where I have a parent menu "primary-links"
with several child submenus. I created a submenu "Top Issues" with a
weight of -50 and have other pre-existing sibling submenus with a weight
heavier(-48, -46, -44, -42). The menu order shows correct in:
/admin/structure/menu/manage/primary-links

But in the site's drop down menu, all of the pre-existing submenus
are displaying above the "Top Issues" submenu that has the lower weight.

I also tried this:
https://www.drupal.org/node/1007746#comment-9862563

But no luck!!!

Any ideas?

4Site’s picture

My issue seems to be related to Views, when you create a page and a menu item for that page using views, the order for that menu item seems unable to save it's row weight.

I'll raise it as a bug, if I don't get more feedback.

Irix’s picture

Same problem here.

robin.prieschl’s picture

I have the same problem.

The only way I can get around it for now is by deleting all the menu items created by views in the view and then manually adding links under the menu to the path created by views.

I cleared the cache and the menu is now staying in the right order.

4Site’s picture

Instead of setting the row weights in the Menu pages under Structure/Menus/... , if you go to the Views page for the relevant page instead and you click on the menu link in the Page settings, this opens up a new set of settings for that particular Menu entry here you can set the row weight and it stays saved.

Difficult to explain, let me know if anyone needs help.

geraldito’s picture

Bug still remains active as by Drupal 8.1 as reported in this issue: https://www.drupal.org/node/2671612

quindio’s picture

I just had to remigrate my D6 to D8 and the menus did not migrate in the
correct order. it seemed like they are following create order and not the
weight you give them. I recreated all of my primary superfish menus(more
than 100). At one time in one of my main menu I miss 1 so I had to add it
afterwards with the correct weight but it display it on the site as the last
menu with that drop down but it was correct in menu administration page.

rgpublic’s picture

...still happens for me, too. Only way to solve this is to delete every link of the menu and then add them back one by one in the intended order. In my case, I've found that the weights are in fact saved properly - they are correct when checking MenuLinkContent->getWeight() after Drupal::menuTree->load(...) and the weights are shown correctly in the Menu UI (Show row weights) so they have to be stored somewhere - but the subsequent $menu_tree->build doesn't really seem to care.

lilbebel’s picture

I'm having the same problem. Very frustrating as I keep having to re-organize my main menu yet it keeps randomly undoing what I've done.

StuddMan’s picture

Same problem here, Drupal 8.1.2. Menu order (weights) not retained.

John Studdard
COO, Achieve Agency
p. 561.450.5075
e. jstuddard@achieveagency.com

rgpublic’s picture

I accidentally discovered the solution on this page:

https://www.drupal.org/node/2226481

After loading a menu it seems to be important to apply the "generateIndexAndSort" manipulator like so:

$manipulators = array(
    array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
  );
$tree = $menu_tree->transform($tree, $manipulators);

This will make sure the index is really sorted. Otherwise it is simply unsorted (i.e. in creation order). So you only have the impression that the menu is sorted initially. It never was. And then when you create or change sth. in the menu it all falls apart. All too easy to overlook. Sigh. Don't know if this is everbody else's problem but perhaps it will help somebody. I was looking literally ages for this... Should really be documented better IMHO.

lilbebel’s picture

Thank you rgpublic. I am not a developer. Would you mind please specifying exactly the steps you took with this information in order to make the menu order stay as configured?

Many thanks. Much appreciated.

m

rgpublic’s picture

The problem I found out about only applies if you are rendering the menu *programatically yourself*. If you use the built-in drupal functions like blocks etc. to insert the menu into your site and you have this issue of menu items becoming scrambled, then you are very likely encountering a different issue. Sorry that I can't help you here, but perhaps someone else knows more about this...?

lilbebel’s picture

No problem. Thanks rgpublic.

quindio’s picture

Is there away to add that function call to the menu code?

marie77e’s picture

I had the same problem. Thank you, it worked like a charm! :)

Jeff Burnz’s picture

From some of the comments here it seems some are NOT using the default drupal menu block, but a 3rd party module, or even some sort of Views plugin to render the menu.

If you have this issue you MUST explain in EXACT detail how to replicate it, ALL the modules you are using and precisely under what circumstances the issue occurs, if you do not do this you are wasting your time commenting here - sorry but if you want a developer to help you must do these things as a bare minimum. Right now I have no idea how to replicate this issue, certainly it does not happen with Drupal 8 core alone (I've been using it for 2.5 years every day and never seen this, ever).

quindio’s picture

Hi Jeff,
I am not sure if that reply was to me but if it was, I was referring to the original problem
as stated by 4site where menus are not following the weight rule.

I was wondering if the solution by rgpublic could be added to the menu code to follow
the weight rule in drupal 8.

Most probably is not that easy.

Thanks!!!

Jeff Burnz’s picture

I was talking to everyone :)

rgpublic is programatically rendering the men, in a custom module etc. It's the right answer if that is your problem. No else has stated HOW they are rendering the menu - this is very important (programatically youself in preprocess, using a custom module from contrib like Superfish etc etc).

I've been using Drupal 8 for a long time and I havent seen this behaviour - there must be some other code running or there is a serious bug in Drupal core that none other of the 1000 or so people who built Drupal core noticed in the last 3+ years (you see how unlikely that is?).

lilbebel’s picture

Hello,

I too am using the core Drupal 8 install without any custom menu modules. I did nothing different than I had done in D7. I simply created a main navigation menu in core as always without doing anything unusual or custom, arranged my tabs and they keep getting reordered alphabetcially. I have tried recreating the menu and it keeps happening. To be specific, I am not using any menu modules other than core and the menu keeps ordering the tabs into alphabetical order automatically. It won't allow me to have them any other way other than in alphabetical order.

Thank you.

M

vayu’s picture

I'm using just plain Drupal core, the standard Drupal main menu and the Superfish library. When I change the order of the menu's by weight the weights are fine, the admin menu listing is fine, but Superfish shows them the way they were originally entered. I'm using the built in main menu, not one I created myself. I have this same behavior on two different Drupal 8 installs.

vayu’s picture

I'm not the one to fix this, because I don't know what I'm doing, but I took the code above from rgpublic found a spot in the Superfish plugin where it seemed like it might go and it fixed the problem for me.

From my limited ability to follow the code it seems like it might only work on the sub menus which is where I was having a problem, I didn't try it on the top level items. I also have no idea if there are hidden effects.

Here's what I did, in SuperfishBlock.php:
I added this code in the function expandAll:

            $manipulators = array(
            ['callable' => 'menu.default_tree_manipulators:checkAccess'],
            ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']
          );
          $subtree = $menu_tree->transform($subtree, $manipulators);

The whole function looks like this now:

/**
   * Loads the whole menu tree.
   */
  public function expandAll($tree) {
    foreach ($tree as $key => $element) {
       if ($element->hasChildren && null !== $element->link && !($element->link instanceof InaccessibleMenuLink)) {
        $menu_tree = \Drupal::menuTree();
        $parameters = new MenuTreeParameters();
        $parameters->setRoot($element->link->getPluginId())->excludeRoot()->setMaxDepth(1)->onlyEnabledLinks();
        $subtree = $menu_tree->load(NULL, $parameters);
        if ($subtree) {

         /* this is the added code that seems to work */
          $manipulators = array(
            ['callable' => 'menu.default_tree_manipulators:checkAccess'],
            ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']
          );
          $subtree = $menu_tree->transform($subtree, $manipulators);
        /* end mod */
        
          $tree[$key]->subtree = $this->expandAll($subtree);
        }
      }
    }
    return $tree;
  }
quindio’s picture

OK, I tried adding the php code following Vadu steps and
after making sure that the caches were clear (several tries)
A miracle, my submenu are displaying correctly according to
their weight.

I also, so far have not seen any hidden effects!!!

Great job Vadu

Thanks!!!

Jeff Burnz’s picture

stange@johnstange.net’s picture

Some previous iteration of this behavior manifested with core menus so I didn't think to check back on Superfish, but applying one of the patches from that thread did indeed resolve it for me. Thanks!

lilbebel’s picture

Thank you Jeff but I'm not using Superfish or any other contributed menu module. Simply core.

M

nehapandya55’s picture

Thanks,
$manipulators = array(
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort']
);
$subtree = $menu_tree->transform($subtree, $manipulators);

above code work for me also.