Menu items are arranged by their alphabetical ordering into select items.

Here is one way working solution to have the same ordering than menu items have in menu manager. It is not 100% bulletproof 'cause comparing non weighted values together has no influence.

CommentFileSizeAuthor
#1 menuperformance-1875140.diff3.65 KBwroxbox

Comments

wroxbox’s picture

StatusFileSize
new3.65 KB

And here is the patch.

firebird’s picture

Status: Active » Needs review

Hi, and thanks for your contribution. I've just actually fixed this in a slightly different, and a lot simpler way. What I've done is I just added a few orderby-clauses to the query that gets the menu links:

  $query = db_select('menu_links')
  ->fields('menu_links', array('link_title', 'mlid'))
  ->condition('plid', $mlid)
  ->condition('menu_name', $menu_name)
  ->orderBy('weight', 'ASC');

  for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
    $query->orderBy('p' . $i, 'ASC');
  }

  $result = $query->execute();

The for-loop has been copied from Drupal's menu.inc. That's the way the links are originally ordered, so I'm hoping it'll work as it should.

This has now been committed to the latest version in git, please test that and let me know if there are any problems.

wroxbox’s picture

Thank you @firebird

Imho that is still missing the client side ordering for items in select lists?

firebird’s picture

Yes, I didn't quite get where that would be needed. What kind of use case do you have in mind where the items would need to be sorted in the client?

wroxbox’s picture

In the widget "Provide a menu link" (node/add/edit form) those select option values are ordered alphabetically.

My patch was also ordering them to reflect the same ordering that menu structure has.

removed

for (var key in data) {
  if (data.hasOwnProperty(key)) {
  var obj = data[key];

changed

var optionObjects = [];
  for (var options in data) {
  optionObjects.push({"mlid":data[options].mlid, "link_title":data[options].link_title, "weight":data[options].weight});
 }
optionObjects.sort(dynamicSort("weight"));
//@Todo do something to 0 comparison. Every item should have weight

for (var key in optionObjects) {
  if (optionObjects.hasOwnProperty(key)) {
  var obj = optionObjects[key];
    obj.mlid != Drupal.settings.menuperformance.current_mlid) {
                     $('#edit-menu-menuperformance-node-menu-parent-link-0').append('<option value="' + obj.mlid + '">' + obj.link_title + '</option>');
    }

So I need that weight info into javascript and convert it to sortable object with the weight info.

firebird’s picture

I still don't get it. That's exactly what I fixed. The menu items are now ordered based on the menu structure order when creating or editing nodes. If the sorting is done on the server side, I don't see why we'd need to sort them again on the client side.

I've created a test menu that looks like this:
-P1
--C1
-P2
-AP3

In the /node/add form, the menu items are now correctly ordered (P1, P2, AP3) when using the latest version from git. When using the latest stable version (7.x-1.0-beta3), the links are indeed incorrectly ordered. If you have steps to reproduce the incorrect ordering from a clean install and using the latest dev version, please post them here.

wroxbox’s picture

Status: Needs review » Closed (duplicate)

Correct. You were right. Tested it more carefully.

I mark this as duplicate for http://drupal.org/node/1841226 and close.