diff --git a/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php b/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php index 70a5b2a33c2feaa9e88d719da7634df75080af2d..5af6de2041a5132d85f2ddd8a5e97a3d53d53224 100644 --- a/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php +++ b/core/modules/content_moderation/tests/src/Functional/LayoutBuilderContentModerationIntegrationTest.php @@ -100,6 +100,7 @@ public function testLayoutModeration() { 'title' => 'bar', 'menu_name' => 'main', 'description' => 'view bar', + 'link_enabled' => TRUE, 'parent' => '', ]); diff --git a/core/modules/menu_ui/config/schema/menu_ui.schema.yml b/core/modules/menu_ui/config/schema/menu_ui.schema.yml index 468802390140225aa1c579cc66f1ad47975360b7..6240be629ee1f93c64191a8928fd652b3376b1bc 100644 --- a/core/modules/menu_ui/config/schema/menu_ui.schema.yml +++ b/core/modules/menu_ui/config/schema/menu_ui.schema.yml @@ -21,3 +21,6 @@ node.type.*.third_party.menu_ui: parent: type: string label: 'Parent' + link_enabled: + type: boolean + label: 'Enable menu link' diff --git a/core/modules/menu_ui/menu_ui.module b/core/modules/menu_ui/menu_ui.module index cff6e972911bf49e05d96d5a09c898a849aa807f..10c10aa57c280757c2b6ab28c2b9d0e69cb8867a 100644 --- a/core/modules/menu_ui/menu_ui.module +++ b/core/modules/menu_ui/menu_ui.module @@ -108,8 +108,8 @@ function _menu_ui_node_save(NodeInterface $node, array $values) { 'link' => ['uri' => 'entity:node/' . $node->id()], 'langcode' => $node->language()->getId(), ]); - $entity->enabled->value = 1; } + $entity->enabled->value = $values['link_enabled'] ?? 1; $entity->title->value = trim($values['title']); $entity->description->value = trim($values['description']); $entity->menu_name->value = $values['menu_name']; @@ -174,6 +174,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) { 'menu_name' => $menu_link->getMenuName(), 'parent' => $menu_link->getParentId(), 'weight' => $menu_link->getWeight(), + 'enabled' => $menu_link->isEnabled(), ]; } } @@ -194,6 +195,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) { 'menu_name' => $menu_name, 'parent' => '', 'weight' => 0, + 'enabled' => $node_type->getThirdPartySetting('menu_ui', 'link_enabled', (int) !empty($menu_name)), ]; } return $defaults; @@ -296,6 +298,13 @@ function menu_ui_form_node_form_alter(&$form, FormStateInterface $form_state) { '#description' => t('Menu links with lower weights are displayed before links with higher weights.'), ]; + $form['menu']['link']['link_enabled'] = [ + '#type' => 'checkbox', + '#title' => t('Enabled'), + '#description' => t('A flag for whether the link should be visible in menus or hidden.'), + '#default_value' => $defaults['enabled'] ?? 1, + ]; + foreach (array_keys($form['actions']) as $action) { if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') { $form['actions'][$action]['#submit'][] = 'menu_ui_form_node_form_submit'; @@ -389,6 +398,11 @@ function menu_ui_form_node_type_form_alter(&$form, FormStateInterface $form_stat ]; $options_cacheability->applyTo($form['menu']['menu_parent']); + $form['menu']['link_enabled'] = [ + '#type' => 'checkbox', + '#title' => t('Enable menu link (default)'), + '#default_value' => $type->getThirdPartySetting('menu_ui', 'link_enabled', 1), + ]; $form['#validate'][] = 'menu_ui_form_node_type_form_validate'; $form['#entity_builders'][] = 'menu_ui_form_node_type_form_builder'; } @@ -421,6 +435,7 @@ function menu_ui_form_node_type_form_validate(&$form, FormStateInterface $form_s function menu_ui_form_node_type_form_builder($entity_type, NodeTypeInterface $type, &$form, FormStateInterface $form_state) { $type->setThirdPartySetting('menu_ui', 'available_menus', array_values(array_filter($form_state->getValue('menu_options')))); $type->setThirdPartySetting('menu_ui', 'parent', $form_state->getValue('menu_parent')); + $type->setThirdPartySetting('menu_ui', 'link_enabled', $form_state->getValue('link_enabled')); } /** diff --git a/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php b/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php index 2491150a6f5a6edce8b4af0f10d257bb632682a0..a1bff3de2a6ec9fb88053ec7f4d4418b95df474c 100644 --- a/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php +++ b/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php @@ -85,6 +85,7 @@ public function testMenuUiWithPendingRevisions() { // Add a menu link and save a new default (published) revision. $edit = [ 'menu[enabled]' => 1, + 'menu[link_enabled]' => 1, 'menu[title]' => 'Test menu link', 'moderation_state[0][state]' => 'published', ]; @@ -120,6 +121,7 @@ public function testMenuUiWithPendingRevisions() { // Try to delete the menu link and save a new non-default (draft) revision. $edit = [ 'menu[enabled]' => 0, + 'menu[link_enabled]' => 0, 'moderation_state[0][state]' => 'draft', ]; $this->drupalGet('node/' . $node->id() . '/edit'); @@ -170,6 +172,7 @@ public function testMenuUiWithPendingRevisions() { // and ensure it's not immediately published. $edit = [ 'menu[enabled]' => 1, + 'menu[link_enabled]' => 1, 'menu[title]' => 'Second test menu link', 'moderation_state[0][state]' => 'draft', ]; diff --git a/core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php b/core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php index 35aada1ec559b0445fda5b3f2553366fdc985a18..a2fbfa17125b8388cc9e6e3e131f4ba7e49e6d9a 100644 --- a/core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php +++ b/core/modules/menu_ui/tests/src/Functional/MenuUiNodeTest.php @@ -67,6 +67,35 @@ protected function setUp(): void { $this->drupalLogin($this->editor); } + /** + * Test correct configuring of default enabling link setting by content type. + */ + public function testContentTypeLinkEnableConfiguration() { + // Check that unconfigured content type defaults to enabled links. + $this->drupalGet('node/add/page'); + $this->assertSession()->checkboxChecked('menu[link_enabled]'); + + // Configure menu links to be disabled. + $edit = [ + 'menu_options[main]' => 1, + 'menu_parent' => 'main:', + 'link_enabled' => FALSE, + ]; + $this->drupalGet('admin/structure/types/manage/page'); + $this->submitForm($edit, 'Save content type'); + $this->assertSession()->pageTextContains('The content type Basic page has been updated.'); + $this->drupalGet('node/add/page'); + $this->assertSession()->checkboxNotChecked('menu[link_enabled]'); + + // Configure menu links to be enabled. + $edit['link_enabled'] = TRUE; + $this->drupalGet('admin/structure/types/manage/page'); + $this->submitForm($edit, 'Save content type'); + $this->assertSession()->pageTextContains('The content type Basic page has been updated.'); + $this->drupalGet('node/add/page'); + $this->assertSession()->checkboxChecked('menu[link_enabled]'); + } + /** * Tests creating, editing, deleting menu links via node form widget. */ @@ -124,6 +153,7 @@ public function testMenuNodeFormWidget() { 'menu_options[main]' => 1, 'menu_options[tools]' => 1, 'menu_parent' => 'main:', + 'link_enabled' => TRUE, ]; $this->drupalGet('admin/structure/types/manage/page'); $this->submitForm($edit, 'Save content type'); @@ -134,6 +164,7 @@ public function testMenuNodeFormWidget() { 'title[0][value]' => $node_title, 'menu[enabled]' => 1, 'menu[title]' => 'Test preview', + 'menu[link_enabled]' => 1, ]; $this->drupalGet('node/add/page'); $this->submitForm($edit, 'Preview'); @@ -177,6 +208,7 @@ public function testMenuNodeFormWidget() { 'menu[enabled]' => 1, 'menu[title]' => $node_title, 'status[value]' => FALSE, + 'menu[link_enabled]' => 1, ]; $this->drupalGet('node/' . $node->id() . '/edit'); $this->submitForm($edit, 'Save'); @@ -189,13 +221,34 @@ public function testMenuNodeFormWidget() { $this->drupalGet('test-page'); $this->assertSession()->linkExists($node_title, 0, 'Found a menu link with the node published'); + // Assert that link is not enabled / does not exist in the rendered menu + // if link_enabled option is disabled. + $edit['menu[link_enabled]'] = 0; + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->submitForm($edit, 'Save'); + $this->drupalGet('test-page'); + $this->assertSession()->linkNotExists($node_title, 'Found no menu link with the node menu link disabled'); + // If we go to main menu we shall see the disabled menu link. + $this->drupalGet('admin/structure/menu/manage/main'); + $this->assertSession()->checkboxNotChecked("Enable $node_title menu link"); + // Re-enable link via node form and check menu. + $edit['menu[link_enabled]'] = 1; + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->submitForm($edit, 'Save'); + $this->drupalGet('test-page'); + // If we go to main menu we shall see the enabled menu link. + $this->drupalGet('admin/structure/menu/manage/main'); + $this->assertSession()->checkboxChecked("Enable $node_title menu link"); + // Log back in as normal user. $this->drupalLogin($this->editor); + // Edit the node and create a menu link. $edit = [ 'menu[enabled]' => 1, 'menu[title]' => $node_title, 'menu[weight]' => 17, + 'menu[link_enabled]' => 1, ]; $this->drupalGet('node/' . $node->id() . '/edit'); $this->submitForm($edit, 'Save'); @@ -219,6 +272,8 @@ public function testMenuNodeFormWidget() { $link->set('enabled', FALSE); $link->save(); $this->drupalGet($node->toUrl('edit-form')); + // Remove this submission value as it will re-enable menu link on save. + unset($edit['menu[link_enabled]']); $this->submitForm($edit, 'Save'); $link = MenuLinkContent::load($link_id); $this->assertFalse($link->isEnabled(), 'Saving a node with a disabled menu link keeps the menu link disabled.'); @@ -338,6 +393,7 @@ public function testMultilingualMenuNodeFormWidget() { 'menu[enabled]' => 1, 'menu[title]' => $node_title, 'menu[weight]' => 17, + 'menu[link_enabled]' => 1, ]; $options = ['language' => $languages[$langcodes[0]]]; $url = $node->toUrl('edit-form', $options); @@ -349,6 +405,7 @@ public function testMultilingualMenuNodeFormWidget() { 'menu[enabled]' => 1, 'menu[title]' => $translated_node_title, 'menu[weight]' => 17, + 'menu[link_enabled]' => 1, ]; $options = ['language' => $languages[$langcodes[1]]]; $url = $node->toUrl('edit-form', $options);