Drupal should warn if your node delete will cause menu item re-parenting

Created on 15 September 2023, about 1 year ago

Problem/Motivation

Deleting a node in the middle/top of the menu tree causes unexpected results for content editors.

Steps to reproduce

1. Install Drupal Core
2. Create some nodes and build out a simple main menu tree like

Children's Health, Safety and Wellbeing
Mental Health
   |- ADHD
   |- Anxiety and depression
   |- Autism

3. Delete the "Mental Health" node
Woah, all of a sudden the main navigation (site-wide) has all these new top-level menu items. This is even more of a surprise if the "Mental Health" menu item was disabled, or its node was unpublished (because you would've forgotten that it has children).

Proposed resolution

Warn before the delete (but we can't do that everywhere), and notify after the delete.

Warn before

/node/*/delete currently says

Are you sure you want to delete the content item ?
This action cannot be undone.

We should also say something like

This page has 3 menu children. These will be moved one page higher in the menu hierarchy
* Foo (link to /admin/structure/menu/item//edit)
* Bar (link to /admin/structure/menu/item//edit)
* Baz (link to /admin/structure/menu/item//edit)

Notify after

Not all node deletions go through /node/*/delete. So we should also show a warning message afterwards (same as above, just changing the verb tense):

A page was deleted that had 3 menu children. These will be moved one page higher in the menu hierarchy
* Foo (link to /admin/structure/menu/item//edit)
* Bar (link to /admin/structure/menu/item//edit)
* Baz (link to /admin/structure/menu/item//edit)

Remaining tasks

* Discussion
* Implementation

User interface changes

* New messaging

API changes

None

Data model changes

None

Release notes snippet

TBD

โœจ Feature request
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Menu systemย  โ†’

Last updated about 17 hours ago

Created by

๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @dalin
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ
  • First commit to issue fork.
  • I have added some logic that tests whether the child links are present for the link that we are deleting. If we have child elements then it shows a message(may not be correctly defined now) that this link contains child links which will be effected due to the deletion of the parent link.I am attaching the screenshots as a kickstart to this.

    We can similarly do this after the successful deletion of the link as well as described in the issue.

  • last update about 1 year ago
    Custom Commands Failed
  • @utkarsh_33 opened merge request.
  • Pipeline finished with Failed
    about 1 year ago
    Total: 192s
    #33355
  • last update about 1 year ago
    30,241 pass, 66 fail
  • Pipeline finished with Failed
    about 1 year ago
    Total: 697s
    #33400
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ

    @Utkarsh_33
    This isn't about when menu items are deleted. It's about when nodes (or I guess other entities too) within the menu are deleted.

  • ๐Ÿ‡ง๐Ÿ‡ทBrazil joaopauloc.dev

    joaopauloc.dev โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    11 months ago
    Total: 160s
    #63818
  • Pipeline finished with Failed
    11 months ago
    Total: 643s
    #63825
  • Pipeline finished with Failed
    11 months ago
    Total: 204s
    #63877
  • Pipeline finished with Failed
    11 months ago
    Total: 646s
    #63878
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil joaopauloc.dev

    Hi folks, I have two questions.

    First, the warning message will be displayed only for a node that has a menu item as a parent? or should we warn users since the content has any kind of reference in menus? If yes I need to update the implementation.

    In Drupal 11 the menu link is also deleted if the content is deleted. We need to update the message, I thought something like

    This page has 3 menu children. These menu links will be deleted too if you continue.

    Thanks.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ

    @joaopauloc.dev
    I like how you moved to ContentEntityDeleteForm.php so that this will apply to all content, not just nodes. But I'm not sure that getQuestion() is the right place for this. There's several returns in that function, and you've just added one more. The original MR used buildForm(), and that might be a better choice. Especially since we want to also use a list.

    First, the warning message will be displayed only for a node that has a menu item as a parent? or should we warn users since the content has any kind of reference in menus?

    Only warn when an entity is being deleted, and that entity has a menu item, and that menu item has child menu items.

    In Drupal 11 the menu link is also deleted if the content is deleted. We need to update the message, I thought something like: This page has 3 menu children. These menu links will be deleted too if you continue.

    Yes, the menu item being deleted is what causes the problem that this ticket is trying to solve. But are you saying that in D11 the _children_ menu items are also blindly deleted??? I can't get https://simplytest.me/ to build D11 so that I can confirm. But if that's the case, then that's a separate critical issue of content destruction. I'm guessing that's _not_ happening.

    I've updated the ticket description to have clearer messaging. Can you take a look?

  • ๐Ÿ‡ง๐Ÿ‡ทBrazil joaopauloc.dev

    Thank you @dalin for the feedback.

    I'll adjust the menu to warn only on the criteria that you mentioned. Also, fix the getQuestion things that you mentioned.

    Regarding the menu item also been deleted I'll confirm one more time but I'm almost sure that was deleted too.

    I'll work on this issue until this weekend and let you know.
    thanks.

  • Assigned to joaopauloc.dev
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil joaopauloc.dev

    Hey @dalin, I could confirm that only the menu item of the content was deleted.
    Working on the issue...

  • Pipeline finished with Failed
    10 months ago
    Total: 165s
    #73776
  • Pipeline finished with Failed
    10 months ago
    Total: 162s
    #73780
  • Issue was unassigned.
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil joaopauloc.dev

    Updated the implementation to show the warning message before and after the content is deleted.
    Missing unit tests and fixing probably unit test issues.

  • Pipeline finished with Failed
    10 months ago
    Total: 612s
    #73782
  • Pipeline finished with Success
    10 months ago
    Total: 502s
    #79891
  • Status changed to Needs review 10 months ago
  • Status changed to RTBC 10 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia arunkumark Coimbatore

    The MR 5807 applied cleanly. The patch working as expected. Attached screenshot of manual testing.

  • Status changed to Needs work 10 months ago
  • ๐Ÿ‡ธ๐Ÿ‡ฐSlovakia poker10

    What will happe, if there will be hundreds of child menu items? I suppose this will not look good. Do we need to print the child menu items?

    I think we need to add a test for this new feature, so that we can confirm that the extra message is here, when the menu item has children and that the message is not there, when deleting a menu item without child items. Thanks!

  • Assigned to dpi
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Working on this recently.

  • Pipeline finished with Failed
    9 months ago
    Total: 205s
    #89214
  • Pipeline finished with Failed
    9 months ago
    Total: 155s
    #89220
  • Pipeline finished with Failed
    9 months ago
    Total: 169s
    #89225
  • Pipeline finished with Failed
    9 months ago
    Total: 481s
    #89228
  • Pipeline finished with Success
    9 months ago
    Total: 2860s
    #89237
  • Pipeline finished with Failed
    9 months ago
    Total: 183s
    #90182
  • Pipeline finished with Failed
    9 months ago
    Total: 174s
    #90201
  • Pipeline finished with Success
    9 months ago
    Total: 559s
    #90206
  • Issue was unassigned.
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    I took a look at the existing MR5807, and found some issues in my review:

    • The moderation state change looks irrelevant.
    • The changes here should work with any entity, not just nodes.
    • Shouldnt get menu items by menu link content entity to get descendants, via entity queries.
    • The menu renders lists with HTML in t-strings, Markup elements, etc.

    Rather than add feedback to the MR, I've started from scratch as I dont think the code is usable.

    Notes on the new implementation at MR6479:

    • Uses a similar method to `menu_ui_get_menu_link_defaults()` to determine the menu link of the entity in context.
    • Renders with the standard menu builder. Which uses a proper list element instead of markup
    • Reworded warning message, use singular labels instead of _content_
    • Used formatplural, depending on number of child menu items.
    • Created a internal class service, without service id. Instead of using the old classResolver pattern.
    • In line with hooks, the new form alter is not overridable:
      • the class been finalised.
      • no service ID.
      • This was chosen as it's possible to easily unfinalise or add a service ID in the future (new issue) if we decide thats needed. Doing the reverse is not possible without dealing with backwards compatibility and deprecations.
    • update the used `getTranslationFromContext` method with a generic, so PHPStan knows its @return-ing the same entity type as is passed into the parameter.
    • Test coverage.

    Screenshots

    Single menu item child

    Multiple menu item children

    Message after delete:

    Re: @poker10 feedback in #17,

    > What will happen, if there will be hundreds of child menu items?

    The message will only return the direct children. Not multiple levels down.

    > I think we need to add a test for this new feature

    Added. I think the tests are comprehensive.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Added notes to review. Remains _Needs review_.

  • Pipeline finished with Success
    9 months ago
    Total: 605s
    #91018
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Removed "node" references from issue summary.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia
  • Status changed to Needs work 9 months ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ

    I've discovered that this also includes a menu entity delete. Updating the description accordingly. I updated the remaining tasks.

    IMO we don't need to do anything special if there are lots (a hundred?) direct children. The list will be long. So be it. But this will almost never happen in real life.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ

    IMO we should still show something for menu items that the user doesn't have access to. They should be included in the total count, but in the list we should show "access denied" or something similar.

  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    I've discovered that this also includes a menu entity delete.

    Agree this is a case worth covering, updated the MR and tests to acommodate Menu Link Content entities.

    we don't need to do anything special if there are lots (a hundred?) direct children. The list will be long. So be it. But this will almost never happen in real life.

    I think it'd be more common than "almost never", but I think since the menu system is already capable of rendering the menu items to the front end in runtime, then it will also be fine for backend tasks.

    Nothing further required for the item.

    IMO we should still show something for menu items that the user doesn't have access to. They should be included in the total count, but in the list we should show "access denied" or something similar.

    I disagree, I dont think theres a case in Drupal where we inform the user there is some secret data the user doesnt know about but is affected by an operation, except when permission to do the operation straight up denies the user.

    To me, only exposing data relevant to the user is sufficient.

    ----

    Changes in MR.

  • Pipeline finished with Success
    9 months ago
    Total: 622s
    #91823
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Seems next remaining task is to agree on wording. UX team should have a say in that.

  • Status changed to Needs work 9 months ago
  • The Needs Review Queue Bot โ†’ tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide โ†’ to find step-by-step guides for working with issues.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia samit.310@gmail.com

    samit.310@gmail.com โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Success
    3 months ago
    Total: 1130s
    #255736
  • Status changed to Needs review 3 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia samit.310@gmail.com

    Fix conflicts and chnages in test file with D11 compatibility.

    Thanks
    Samit K.

  • Status changed to Needs work 3 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • Pipeline finished with Failed
    22 days ago
    #318739
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada dalin Guelph, ๐Ÿ‡จ๐Ÿ‡ฆ, ๐ŸŒ
  • First commit to issue fork.
  • Pipeline finished with Failed
    10 days ago
    Total: 168s
    #329922
  • Pipeline finished with Failed
    10 days ago
    Total: 142s
    #329944
  • Pipeline finished with Failed
    10 days ago
    Total: 146s
    #329948
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia shalini_jha

    After rebasing merge request (MR), it seems that PHPStan is flagging issues in StringTranslationTrait.php .

Production build 0.71.5 2024