diff --git a/core/modules/menu_ui/menu_ui.module b/core/modules/menu_ui/menu_ui.module index d7cebf146ee68e717842b9c57bd4b861b86b41e8..1e2d9a051c0d2ce5adbaad75f5378f76137ec1ab 100644 --- a/core/modules/menu_ui/menu_ui.module +++ b/core/modules/menu_ui/menu_ui.module @@ -125,6 +125,7 @@ function _menu_ui_node_save(NodeInterface $node, array $values) { $entity->menu_name->value = $values['menu_name']; $entity->parent->value = $values['parent']; $entity->weight->value = isset($values['weight']) ? $values['weight'] : 0; + $entity->isDefaultRevision($node->isDefaultRevision()); $entity->save(); } diff --git a/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraint.php b/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraint.php index 35b29e92a9699e969df2a098c0596993327c852c..b5828af51f202bdb0b89f0a6456abd92ae78d166 100644 --- a/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraint.php +++ b/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraint.php @@ -15,5 +15,8 @@ class MenuSettingsConstraint extends Constraint { public $message = 'You can only change the menu settings for the <em>published</em> version of this content.'; + public $messageWeight = 'You can only change the menu item weight for the <em>published</em> version of this content.'; + public $messageParent = 'You can only change the parent menu item for the <em>published</em> version of this content.'; + public $messageRemove = 'You can only remove the menu item in the <em>published</em> version of this content.'; } diff --git a/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php b/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php index e47ee3e11a2cc8eafc9cb8281bbbd45d2a89f16f..33bd6eb854c5e3d96c66076cdaf43180766c03e2 100644 --- a/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php +++ b/core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php @@ -31,39 +31,35 @@ public function validate($entity, Constraint $constraint) { $values['parent'] = $parent; } - // Handle the case when a menu link is added to a pending revision. - if (!$defaults['entity_id'] && $values['enabled']) { - $violation_path = 'menu'; - } // Handle the case when the menu link is deleted in a pending revision. - elseif (empty($values['enabled']) && $defaults['entity_id']) { - $violation_path = 'menu'; + if (empty($values['enabled']) && $defaults['entity_id']) { + $this->context->buildViolation($constraint->messageRemove) + ->atPath('menu') + ->setInvalidValue($entity) + ->addViolation(); } - // Handle all the other menu link changes in a pending revision. + // Handle all the other non-revisionable menu link changes in a pending + // revision. elseif ($defaults['entity_id']) { - if (($values['title'] != $defaults['title'])) { - $violation_path = 'menu.title'; - } - elseif (($values['description'] != $defaults['description'])) { - $violation_path = 'menu.description'; - } - elseif ($defaults['entity_id'] && ($values['menu_name'] != $defaults['menu_name'])) { - $violation_path = 'menu.menu_parent'; + if ($defaults['entity_id'] && ($values['menu_name'] != $defaults['menu_name'])) { + $this->context->buildViolation($constraint->messageParent) + ->atPath('menu.menu_parent') + ->setInvalidValue($entity) + ->addViolation(); } elseif (isset($values['parent']) && ($values['parent'] != $defaults['parent'])) { - $violation_path = 'menu.menu_parent'; + $this->context->buildViolation($constraint->messageParent) + ->atPath('menu.menu_parent') + ->setInvalidValue($entity) + ->addViolation(); } elseif (($values['weight'] != $defaults['weight'])) { - $violation_path = 'menu.weight'; + $this->context->buildViolation($constraint->messageWeight) + ->atPath('menu.weight') + ->setInvalidValue($entity) + ->addViolation(); } } - - if ($violation_path) { - $this->context->buildViolation($constraint->message) - ->atPath($violation_path) - ->setInvalidValue($entity) - ->addViolation(); - } } } diff --git a/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php b/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php index b5d4b43c8224dc8824cc2bbfb58855bf1d8641e2..cc86eeff80f9112978ea667d6a5637e4b1210bd6 100644 --- a/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php +++ b/core/modules/menu_ui/tests/src/Functional/MenuUiContentModerationTest.php @@ -79,72 +79,64 @@ public function testMenuUiWithPendingRevisions() { $this->assertSession()->linkExists('Test menu link'); - // Try to change the menu link title and save a new non-default (draft) + // Try to change the menu link weight and save a new non-default (draft) // revision. $edit = [ - 'menu[title]' => 'Test menu link draft', + 'menu[weight]' => 1, 'moderation_state[0][state]' => 'draft', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Check that the menu settings were not applied. - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); - $this->assertSession()->linkExists('Test menu link'); - $this->assertSession()->linkNotExists('Test menu link draft'); + $this->assertSession()->pageTextContains('You can only change the menu item weight for the published version of this content.'); - // Try to change the menu link description and save a new non-default - // (draft) revision. + // Try to change the menu link parent and save a new non-default (draft) + // revision. $edit = [ - 'menu[description]' => 'Test menu link description', + 'menu[menu_parent]' => 'main:test_page_test.front_page', 'moderation_state[0][state]' => 'draft', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Check that the menu settings were not applied. - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); + $this->assertSession()->pageTextContains('You can only change the parent menu item for the published version of this content.'); - // Try to change the menu link weight and save a new non-default (draft) - // revision. + // Try to delete the menu link and save a new non-default (draft) revision. $edit = [ - 'menu[weight]' => 1, + 'menu[enabled]' => 0, 'moderation_state[0][state]' => 'draft', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); // Check that the menu settings were not applied. - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); + $this->assertSession()->pageTextContains('You can only remove the menu item in the published version of this content.'); + $this->assertSession()->linkExists('Test menu link'); - // Try to change the menu link parent and save a new non-default (draft) - // revision. + // Try to change the menu link title and description and save a new + // non-default (draft) revision. $edit = [ - 'menu[menu_parent]' => 'main:test_page_test.front_page', + 'menu[title]' => 'Test menu link draft', + 'menu[description]' => 'Test menu link description', 'moderation_state[0][state]' => 'draft', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); + $this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()])); - // Check that the menu settings were not applied. - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); + // Ensure the content was not immediately published. + $this->assertSession()->linkExists('Test menu link'); - // Try to delete the menu link and save a new non-default (draft) revision. + // Publish the node and ensure the new link text was published. $edit = [ - 'menu[enabled]' => 0, - 'moderation_state[0][state]' => 'draft', + 'moderation_state[0][state]' => 'published', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); - - // Check that the menu settings were not applied. - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); - $this->assertSession()->linkExists('Test menu link'); + $this->assertSession()->linkExists('Test menu link draft'); // Try to save a new non-default (draft) revision without any changes and // check that the error message is not shown. $edit = ['moderation_state[0][state]' => 'draft']; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); - // Check that the menu settings were not applied. - $this->assertSession()->pageTextNotContains('You can only change the menu settings for the published version of this content.'); - $this->assertSession()->linkExists('Test menu link'); - // Create a node. $node = $this->drupalCreateNode(); @@ -153,14 +145,24 @@ public function testMenuUiWithPendingRevisions() { $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); $this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()])); - // Add a menu link and save and create a new non-default (draft) revision. + // Add a menu link and save and create a new non-default (draft) revision + // and ensure it's not immediately published. $edit = [ 'menu[enabled]' => 1, - 'menu[title]' => 'Test menu link', + 'menu[title]' => 'Second test menu link', 'moderation_state[0][state]' => 'draft', ]; $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); - $this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.'); + $this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()])); + $this->assertSession()->linkNotExists('Second test menu link'); + + // Publish the content and ensure the new menu link shows up. + $edit = [ + 'moderation_state[0][state]' => 'published', + ]; + $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); + $this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()])); + $this->assertSession()->linkExists('Second test menu link'); } }