I'm wondering how it is possible (with a hook) to show the weight next to the menu title in the parent drop down found in the Menu Settings tab.

So this is what it is at this moment: enter image description here

And this is what I want it to be, so the admin knows what weight to fill in to place the menu item at a certain place: enter image description here


Doing a hook_form_alter on the node edit form should do the trick:

  use Drupal\Core\Form\FormStateInterface;

  function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** to appear on the node/add/page form, change this or add more for other content types **/
  if ($form_id == 'node_page_form') {
    $menuItems = $form['menu']['link']['menu_parent']['#options'];
    $menuItemsWithWeight = array();
    foreach ($menuItems as $menuKey => $menuValue) {
      $storage =  Drupal::entityManager()->getStorage('menu_link_content');
      $uuid = ltrim(strstr($menuKey, ':'), ':');
      $uuid = ltrim(strstr($uuid, ':'), ':');
      $link = $storage->loadByProperties(['uuid' => $uuid]);
      if(count($link) > 0) {
        $link = reset($link);
        $weight = $link->getWeight();
        $menuValue = $menuValue . ' ('.$weight.')';
      $menuItemsWithWeight[$menuKey] = $menuValue;
    $form['menu']['link']['menu_parent']['#options'] = $menuItemsWithWeight;
  • thanks for the answer, works fine. But it still wasn't happy with the user experience, so I ended up using the Menu Link Weight module. – Joost Jul 24 '18 at 10:38

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.