Problem/Motivation

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

Steps to reproduce

  1. Install Drupal Core
  2. Create some entities
  3. Build out a simple main menu tree. For example:
    Menu item 1
    Menu item 2
       |- Menu item 2.a
       |- Menu item 2.b
       |- Menu item 2.c
    Menu item 3
    Menu item 4
    

3. Either delete the menu item for "Menu item 2" or its node.

Woah, all of a sudden the main navigation (site-wide) has three new top-level menu items! This is even more of a surprise if the "Menu item 2" menu item was disabled, or the related entity was unpublished (because you would've forgotten that it has menu children).

Proposed resolution

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

Pre-delete warning

Delete forms currently:

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

Update delete forms with informational text:

This content has 3 menu children. Proceeding will move these one level higher in the menu hierarchy:
* Foo (link to /admin/structure/menu/item/[id]/edit)
* Bar (link to /admin/structure/menu/item/[id]/edit)
* Baz (link to /admin/structure/menu/item/[id]/edit)

Post-delete notification

Show a warning message afterwards (same as above, just changing the verb tense):

Content was deleted that had menu children. These have been moved one level higher in the menu hierarchy:

* Foo (link to /admin/structure/menu/item/[id]/edit)
* Bar (link to /admin/structure/menu/item/[id]/edit)
* Baz (link to /admin/structure/menu/item/[id]/edit)

All entity types

We should should generalize to all entity types. e.g. "[entity type] was deleted that had menu children..."

Remaining tasks

  • Discussion Complete
  • Finish implementation of pre-delete warning Complete
  • Start implementation of post-delete notification Complete
  • Agree on wording, and confirm that the wording in the patch is in line with what's in this description
  • Decide if/how to show menu items that the user doesn't have access to
  • Confirm that code changes work for all entity types (including deleting a menu item with children)
  • Confirm that tests are thorough

User interface changes

Messages on delete form.
singular warning message
Messages on content delete form.
message modal on content delete form

Message via messenger after entity is deleted via entity delete form.
warning message

API changes

None

Data model changes

None

Release notes snippet

TBD

Issue fork drupal-3387665

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dalin created an issue. See original summary.

dalin’s picture

Issue summary: View changes
dalin’s picture

Issue summary: View changes

Utkarsh_33 made their first commit to this issue’s fork.

utkarsh_33’s picture

StatusFileSize
new359.51 KB

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.

dalin’s picture

@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.

joaopauloc.dev made their first commit to this issue’s fork.

joaopauloc.dev’s picture

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.

dalin’s picture

Issue summary: View changes

@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?

joaopauloc.dev’s picture

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.

joaopauloc.dev’s picture

Assigned: Unassigned » joaopauloc.dev

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

joaopauloc.dev’s picture

Assigned: joaopauloc.dev » Unassigned

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

joaopauloc.dev’s picture

Status: Active » Needs review
arunkumark’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new251.89 KB
new197.18 KB

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

Delete

Delete

poker10’s picture

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

Thanks for working on this. What will happen, if there will be hundreds of child menu items? I suppose this will not look good. Do we need to output the child menu items in the message?

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!

dpi’s picture

Assigned: Unassigned » dpi

Working on this recently.

dpi’s picture

Assigned: dpi » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new114.49 KB
new159.53 KB
new89.7 KB

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

Single

Multiple menu item children

Multiple

Message after delete:

post 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.

dpi’s picture

Added notes to review. Remains _Needs review_.

dpi’s picture

Issue summary: View changes

Removed "node" references from issue summary.

dpi’s picture

Title: Drupal should warn if your node delete will cause menu item re-parenting » Warn user when entity delete will cause menu item re-parenting
dalin’s picture

Issue summary: View changes
Status: Needs review » Needs work

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.

dalin’s picture

Issue summary: View changes

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.

dpi’s picture

Status: Needs work » Needs review

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.

smustgrave’s picture

Issue tags: +Needs usability review

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

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

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.

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

samitk’s picture

Status: Needs work » Needs review

Fix conflicts and chnages in test file with D11 compatibility.

Thanks
Samit K.

smustgrave’s picture

Status: Needs review » Needs work

dalin’s picture

Issue summary: View changes

shalini_jha made their first commit to this issue’s fork.

shalini_jha’s picture

After rebasing MR, it seems that PHPStan is flagging issues in StringTranslationTrait.php .

shalini_jha’s picture

Status: Needs work » Needs review

I have checked and fixed the pipeline failure, and rebased the MR . Also checked the last feedback is already addressed, so moving this Back to NR. Kindly review.

benjifisher’s picture

Status: Needs review » Needs work
Issue tags: -Needs usability review +Needs issue summary update

We discussed this issue at #3387665: Warn user when entity delete will cause menu item re-parenting. That issue will have a link to a recording of the meeting.

Thanks for working on this issue. At least one attendee has run into exactly this problem, and there were a lot of child menu items involved.

For the record, the attendees at the usability meeting were @benjifisher, @rkoller, @simohell, and @worldlinemine.

When you tag an issue for usability review, please make it easy for the usability team to review the issue. Update the issue summary:

  • The "Proposed resolution" section should describe all the changes made in the issue.
  • The "User interface changes" should show the existing UI and the proposed UI.
  • The "Remaining tasks" should include one explaining the usability issue(s).

Most of the time, I prefer to have plain text in the "Proposed resolution" section and screenshots in the "User interface changes" section.

You can also attend the weekly usability meeting to present an issue.

Usability review

We have a few recommendations:

  1. Shorten the text in the confirmation form.
  2. Suggest changing the parent of the menu item before deleting it.
  3. Special treatment when deleting custom menu links.
  4. Follow-up issue: provide an option to delete the child menu items.

Shorten the text

There is room for improvement, but this is the best we could do during the meeting. After the standard "This action cannot be undone.", either

Deleting this [content item] will make these [count] child menu items top level:
...

or

Deleting this [content item] will move these [count] child menu items under [parent link]:
...

Make adjustments for singular or plural, as the current MR already does.

Suggest changing the parent

A good status message starts by describing the situation, but then it suggests what to do about it. In this case, a common action will be to change the parent of the menu item that is going to be deleted: then, after deletion, all the child menu items will be children of the new parent.

Changing the parent item can be done from the menu page (where all the items in the menu are listed, with a click-and-drag interface), from the edit page for the individual menu link (where the parent can be selected from a list) or from the edit page of the content item. We feel that the click-and-drag interface is problematic. There might be permission issues with editing the menu item, but that gives the most flexibility.

If the user is deleting a content entity (not a custom menu link ... see the next point) and the suggestion is to edit the related menu link, then it would help to give a link to that edit page.

We do not have suggested text, but this information can go after the list of affected child items.

Special treatment when deleting custom menu links

Custom menu links are content items, so the warning gets added to the confirmation form when deleting a custom menu link. So far, so good! But the text on the current MR, when deleting a custom menu link, includes "The menu items for this custom menu link", which does not make sense. Pay attention to the case where the content item being deleted is a custom menu link.

With the text proposed earlier, that entire sentence is removed, so maybe we do not have to worry about it. But keep this case in mind while continuing to work on this issue.

Follow-up issue

Sometimes, the user will want to delete the child menu items instead of move them to a different parent. There are a lot of things to be considered here, so we recommend adding a follow-up issue to discuss how to implement this. Maybe the right thing to do is turn the menu page into a form that allows bulk operations. Maybe there should be an option on the confirmation form, or the edit form for a custom menu link, to delete children. Should that include all descendants? How can we make it clear whether we are deleting content entities or their related custom menu links?

If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

rkoller’s picture

Usability review

We discussed this issue at #3420206: Drupal Usability Meeting 2024-02-16. The link to the recording of the meeting is: https://youtu.be/GeLGUAqrMTQ

For the record, the attendees at the usability meeting were @AaronMcHale, @benjifisher, @rkoller, @simohell, and @worldlinemine.

During followup discussions for the usability meeting on friday, we’ve realized that we had already discussed this issue a few months ago in february; unfortunately, it slipped through, and we haven’t had posted any summary yet. Apologies. :(

But the two reviews complement each other quite well, because back then we haven’t had the time to get into the whole wordsmithing part. And as a disclaimer, I haven’t rewatched the entire recording, but just focused on close to the end, where we usually summarize our conclusions. So in addition to the points @benjifisher summarized in #3387665-37: Warn user when entity delete will cause menu item re-parenting the following aspects should be considered:

In the examples and screenshots shared on this issue, the number of listed children on the confirmation dialoge modal and the warning message is rather small. But one of the attendees raised the concern, based on his own experiences, that the odds are high to have a very large set of menu items with many children and grandchildren. By providing a list of menu items, that list will become unwieldy fast, if all affected menu items are being listed. Providing some logic that displays a list with equal or less than for example three items, while hiding the list with more than three items might overcomplicate things. So the recommended approach we had a consensus on was to only to provide a summary on the confirmation dialog modal and the warning message (micro copy wise the proposal from the “shorten the text” section in #3387665-37: Warn user when entity delete will cause menu item re-parenting would work), the URLs should be moved to a log message. That log message should be created containing the list of affected URLs, so the information about the made change is available persistently for easier recovery, and the retracing of the applied steps, if necessary.

And it was noted that the following scenario is currently no covered by the MR yet - if a node is being deleted and that node is contained in more than one menu, the user is currently not being informed about that fact. Should that detail be moved to a followup issue or already be worked on within the scope of this issue?

If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

joaopauloc.dev’s picture

Issue summary: View changes
StatusFileSize
new707.65 KB
new736.94 KB
joaopauloc.dev’s picture

StatusFileSize
new6.2 MB

Hi folks.
I updated the issue summary adding a screenshot with the User interface changes as recommended in comment #37.
Also, when I tested the MR I found the following issue.
error to. show warning message
Finally, update the modal message with the comments suggested in comment #37. Regarding this message, I would like to suggest another one when there is only one item, for the singular message we could use something like:
Deleting this @singular_label will move its child menu item to the top level:
What do you guys think?

joaopauloc.dev’s picture

Issue summary: View changes
StatusFileSize
new973.5 KB
joaopauloc.dev’s picture

Status: Needs work » Needs review
StatusFileSize
new645.77 KB

Hi @benjifisher and @rkoller, thanks for reviewing this issue.

These are my suggestions for the points Suggest changing the parent, Special treatment when deleting custom menu links.
For the Suggest changing the parent
suggested changes
The link will be opened in another tab. What do you guys think?

Regarding the Special treatment when deleting custom menu links
I changed the text message as you guys suggested.

smustgrave’s picture

Status: Needs review » Needs work

So I went into test

Drupal 11.x
Added an article under Main navigation (Article 1)
Added an article under the one I just created. (Article 1.1)
Went to delete Article 1
I never received a warning or anything.

joaopauloc.dev’s picture

StatusFileSize
new501.05 KB
new560.77 KB
new142.72 KB

Hey @smustgrave.
Thank you for reviewing this one.
I followed your steps, and I could see the warning messages.
Do you remember where you were when you tried to remove the node?
Follow the images.

Node page
node page delete

Delete from node edit page
delete from node edit page

Delete action on content list page
delete action on content list page

All the messages above also appear on the menu delete page/action.

smustgrave’s picture

Yea I did not get that. But can retest in the morning

joaopauloc.dev’s picture

Status: Needs work » Needs review
joaopauloc.dev’s picture

Since we have a new form alter hook class https://www.drupal.org/node/3499495
I changed the attribute class on MenuUiEntityDeleteFormHooks to use the FormAlter instead of Hook class.

smustgrave’s picture

Status: Needs review » Needs work

Appears to need a rebase.

dpi’s picture

Can we look into and potentially close #2309531: Add a test for re-parenting of menu links. if there is sufficient overlap.

joaopauloc.dev’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new2.84 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. 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.

joaopauloc.dev’s picture

Status: Needs work » Needs review
arunkumark’s picture

StatusFileSize
new172.01 KB
new129.26 KB
new183.83 KB
new260.84 KB
new187.63 KB

Tested the MR on local and observed as below. Looks like MR is working as expected. Good for RTBC.

Without Patch:
Without patch

With Patch(on MR):

Menu Structure - Before delete:
Menu structure before delete

Warning for the Menu delete:
Warining  patch

Success message - After delete:
after structure patch

Menu Structure - After delete:
Menu-structure-after-delete

smustgrave’s picture

Still 3 open threads can those be closed?

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

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.

joaopauloc.dev changed the visibility of the branch 3387665-drupal-should-warn to hidden.

joaopauloc.dev changed the visibility of the branch drupal-3387665-3387665-dpi-rework to hidden.

joaopauloc.dev’s picture

Status: Needs work » Needs review
eric.vvf’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new37.96 KB
new50.62 KB
new104.65 KB

I followed the Steps to reproduce recommendations, and the solutions seem to work correctly.

Drupal core 11.x-dev

Before the patch:
Message before the patch

After applying the patch:
Message before the patch
Message before the patch

dcam made their first commit to this issue’s fork.

dcam’s picture

I'm checking all RTBC issues to ensure their new tests use attributes. Because these changes are minor and the tests are passing I'm going to leave the issue at RTBC.

quietone’s picture

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

@arunkumark and @eric.vvf thanks for testing. To set an issue to RTBC it also needs a complete review which includes a code review and to confirm that the change passes the Core gates.

On reading the comments I see that there has been no response to #53, which is to review the open threads in the MR to decide if they can be closed. I also made a comment on the MR, the out of scope change should be remove.

There are several items in the remaining tasks that are open. Have they been resolved.?

I did not thoroughly review the 2 Usability reviews here to confirm that the MR implements the recommendations.

I updated credit.

Setting to NW.

quietone’s picture

In a Slack conversation about credit email I learned nicxvan received an email saying they were credited but that is not reflected in the contrib record now. Perhaps I added them and then removed then. I don't know. In any case adding them now.

As a reminder, the person committing will review credit.

joaopauloc.dev’s picture

Status: Needs work » Needs review
nicxvan’s picture

Gave a high level overview and noted a couple things that need to be addressed for performance reasons.

nicxvan’s picture

Status: Needs review » Needs work
joaopauloc.dev’s picture

Status: Needs work » Needs review

All comments addressed.

smustgrave’s picture

Status: Needs review » Needs work

appears to be feedback on the MR. Resolved 2 threads though.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.