1

I am not familiar with the Drupal 7 database API. I want to alphabetized a menu.
Can anyone help me write a database query to retrieve all the menu item links of a certain parent (plid = 228) and order ascending by title? Within that result set, can anyone help me write a database update method to update each menu item's weight sequentially (counter starts at 0 and increments)?

2

Something like this should work, but I have not tested this code. Please comment on this if you have questions.

$result = db_select('menu_links', 'ml') 
  ->fields('ml', 'mlid')
  ->condition('ml.plid', $plid, '=')
  ->orderBy('ml.link_title')
  ->execute();

$i=0;
foreach ($result as $row) {
  db_update('menu_links')
    ->fields(array('weight' => $i))
    ->condition('mlid', $row->mlid)
    ->execute();
  $i++;
}

At this point they would all be ordered by title, and you could print the menu wherever needed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.