When a user previews a node a menu link is generated. The link is never used and is left dangling in the system.

Steps to reproduce:
1. node/add/article
2. Enter title and provide a menu link
3. Press preview
4. Navigate to /admin/structure/menu/manage/main: You will se the menu link pointing to node/UUID

Performed on clean vanilla Drupal 8.2.1 with Token 1.0-beta2.

token_node_menu_link_submit() is the culprit here and should probobly check if the user has pressed the preview button or save button.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jseffel created an issue. See original summary.

gceja’s picture

We experienced the same issue.
When going to add a new page, then clicking on the “Provide a menu link”.
Then pressing “Preview”, a temporary menu link gets generated.
And when leaving the new page, because you decide you don’t need it, that temporary menu link remains and shows up on the menu.

This is an issue because now we have broken links showing up on the menu.

Tested on:
Drupal 8.2.3
Token 1.0.0-beta2

darrenwh’s picture

Hi,

The bug is on line 778 of token.module, the system is not looking for the flag $node->in_preview. Unfortunately following further investigation the flag is not set on the node, this will need further investigation.

So the fix will be to add a check like :

if ($node->isNew() && empty($node->in_preview)) {
mbrc’s picture

While the in_preview flag is not yet available, we can check the triggering element in $form_state. I'm not certain that checking the generated ID of the element is the best way to go, and I'm sure that this change needs some tests, but here's a patch that fixes the issue until there's a better solution.

krina.addweb’s picture

Status: Needs review » Reviewed & tested by the community

Hi mbrc, Thanks! for the patch this really solved my issue.

Berdir’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Entity builders are called before submit buttons, and in_preview is set there.

Wondering if we should instead add this as a submit callback, especially after #2068063: Change "Save and keep un-/published" buttons to a "Published" checkbox and an included "Save" button is in. Because every ajax callback also calls this I think.

Tests would be great as well.

sukotto’s picture

#4 worked for us. Thank you!

If anyone reads this and doesn't know how to use the patch, let me share a sanitized version of our deployment instructions:

  1. login to my-server
  2. su to my-admin-user
  3. cd to /tmp
  4. download patch
  5. curl https://www.drupal.org/files/issues/token_node_preview_menu_link-2823731-4.patch --output token_node_preview_menu_link-2823731-4.patch
        
  6. cd to token folder
  7. cd /var/www/html/examplecom/modules/token

  8. backup token.module
  9. cp token.module /tmp/token.module.pre-patch

  10. put site into maintenance mode
  11. patch the files
  12. patch -p1 < /tmp/token_node_preview_menu_link-2823731-4.patch
    Sample output:

    patching file token.module
    Hunk #1 succeeded at 743 with fuzz 2 (offset 4 lines).
        
  13. sanity-check diff
  14. diff /tmp/token.module.pre-patch token.module
    Sample output:

    745a746,749
    >           // Don't create a menu link if the node is being previewed.
    >           if ($form_state->getTriggeringElement()['#id'] == 'edit-preview') {
    >             return;
    >           }
        
  15. flush drupal caches
  16. https://example.com/admin/config/development/performance

  17. test
    1. start a new article
    2. Menu Settings -> [x] provide a menu link
    3. URL Alias -> [x] Generate automatic URL alias
    4. click "preview"
    5. return to article
    6. save article
    7. confirm there is only ONE entry in the menu sidebar
  18. take site out of maintenance mode
lil.destro’s picture

shubham.prakash’s picture

Status: Needs work » Needs review
szecsodimlaszlo’s picture

I had the same problem.
I can confirm that the patch #8 solved it.

malte.koelle’s picture

rosieneko’s picture

I can confirm that patch #11 works for me and solves the issue for me. There is no menu link created in Preview mode when the node hasn't been saved yet.

  • Berdir committed 44bd0cb on 8.x-1.x authored by malte.koelle
    Issue #2823731 by malte.koelle, lil.destro, mbrc, Berdir: Node preview...
Berdir’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Thanks for the test. Made some coding standard and deprecation fixes:

diff --git a/tests/src/Functional/TokenMenuTest.php b/tests/src/Functional/TokenMenuTest.php
index 8768207..134d51a 100644
--- a/tests/src/Functional/TokenMenuTest.php
+++ b/tests/src/Functional/TokenMenuTest.php
@@ -457,9 +457,9 @@ class TokenMenuTest extends TokenTestBase {
   }
 
   /**
-   * Test that no menu link is generated when the node gets previewed.
+   * Tests that no menu link is generated when the node gets previewed.
    */
-  public function testPreviewMenuLink(){
+  public function testPreviewMenuLink() {
     $this->drupalCreateContentType(['type' => 'article']);
     $permissions = [
       'access administration pages',
@@ -476,8 +476,9 @@ class TokenMenuTest extends TokenTestBase {
       'menu[enabled]' => TRUE,
       'menu[title]' => 'English menu title',
     ];
-    $this->drupalPostForm('/node/add/article', $edit, t('Preview'));
-    $menu_links = \Drupal::entityManager()->getStorage('menu_link_content')->loadByProperties(['menu_name' => 'main']);
+    $this->drupalGet('node/add/article');
+    $this->submitForm($edit, t('Preview'));
+    $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['menu_name' => 'main']);
     $this->assertEmpty($menu_links);
   }

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.