diff --git a/src/Entity/Form/MenuLinkFormTrait.php b/src/Entity/Form/MenuLinkFormTrait.php index ab00b889..50171361 100644 --- a/src/Entity/Form/MenuLinkFormTrait.php +++ b/src/Entity/Form/MenuLinkFormTrait.php @@ -151,6 +151,13 @@ protected function attachMenuLinkForm(array $form, FormStateInterface $formState ->menuParentSelector ->parentSelectElement($default, $id, $this->getAvailableMenus()); + $form['menu']['link']['weight'] = [ + '#type' => 'number', + '#title' => t('Weight'), + '#default_value' => $menuLink->getWeight(), + '#description' => $this->t('Menu links with lower weights are displayed before links with higher weights.'), + ]; + $form['actions']['submit']['#submit'][] = '::attachMenuLinkFormSubmit'; return $form; @@ -199,6 +206,7 @@ public function attachMenuLinkFormSubmit(array $form, FormStateInterface $formSt ->set('menu_name', $menuName) ->set('parent', $parent) ->set('langcode', $entity->language()->getId()) + ->set('weight', $values['weight']) ->save(); $entity->set($this->menuLinkFieldName, $menuLink)->save(); diff --git a/tests/src/Functional/MenuLinkFormTest.php b/tests/src/Functional/MenuLinkFormTest.php index f7d6d743..75411828 100644 --- a/tests/src/Functional/MenuLinkFormTest.php +++ b/tests/src/Functional/MenuLinkFormTest.php @@ -96,6 +96,7 @@ public function testMenuLinkForm() : void { $this->submitForm([ 'menu[enabled]' => 1, 'menu[title]' => 'Test title ' . $langcode, + 'menu[weight]' => 9, ], 'Save'); // Make sure menu link is show in edit form. @@ -119,6 +120,7 @@ public function testMenuLinkForm() : void { ->entity ->getTranslation($langcode); + $this->assertEquals(9, $menuLink->getWeight()); $this->assertEquals('Test title ' . $langcode, $menuLink->getTitle()); }