Problem/Motivation
When a menu is deleted, which was previously referenced in a node types' available menu settings, an error is thrown and "parent item" select field contains invalid options.
Issue is still present in 8.2, 8.3., 8.5.4
To reproduce
Required modules: menu_ui, node
- Create a menu.
- Create, then edit a node type. In Menu Settings -> Available menus. Select the previously created menu. (Figure)
- Save the node type.
- Delete the previously created menu. (Figure)
- Create or edit a node entity, with the previous node type.
- The following issues arise:
An error is thrown on a node entity edit form:
Notice: Undefined index: MENUNAME in menu_ui_form_node_form_alter() (line 279 of core/modules/menu_ui/menu_ui.module).
And invalid parent item select options:

Proposed resolution
Add soft dependency from menu configs to node type.
Remaining tasks
Patch, tests.
User interface changes
API changes
Data model changes

Comments
Comment #2
jan.rysavy commentedNote: correct menu_ui.module error line is 266. Originally reported line 267 was caused by my debug print.
Comment #3
cilefen commentedThank you for reporting this. I followed the steps to reproduce (thank you for including some!) on 8.0.x HEAD but the error did not occur. Is it possible a step towards the error was omitted?
I have tagged this "Needs title update" because the title as written does not describe the problem. Something more like "Deleted menus are still referenced as available menus on content types and cause an error when content is edited" may be better.
Comment #4
jan.rysavy commentedComment #5
jan.rysavy commentedMaybe problem is already fixed in 8.0.x HEAD? I'm on 8.0.0 RC1.
Just to be sure I just repeated described sequence and there is no other step involved. Please see attached screenshots.
Comment #6
jan.rysavy commentedComment #7
cilefen commentedWas the "home" node added to the test2 menu at any stage?
Comment #8
jan.rysavy commentedI'm not 100% sure, so I created a new page, a new menu and reproduced problem again just with described steps.
Btw, to "fix" this problem you must open Structure > Content Types > Page again and just edit and save it. Error message will be gone.
Comment #9
cilefen commentedActually the error did occur but it is in my watchdog log because I don't display them. So, the steps are reproducible. I am sorry to have had you do it twice.
Comment #10
jan.rysavy commentedGreat, thanks for quick confirmation.
Comment #11
dawehnerThat is totally in scope of 8.0.x, so its just a simple bug.
Comment #14
dpiSimplified issue summary, using issue template, moving to correct component, taking over.
Comment #15
dpiContains both fixes and tests. See what testbot thinks.
Comment #16
dpiGreat! Attaching test only patch.
Comment #18
dpiSummary of above patch (#15):
It contains tests and fixes:
Ensures the Parent item
<select>element found on node forms do not contain an empty<option>where a deleted menu would normally occupy (see screenshot in issue summary). This simultaneously fixes the PHP notice that appeared.When a menu configuration is deleted, an entity hook will iterate over all node types and remove any menus referenced in the third party configuration storage. Tests ensure the entity delete hook purges invalid data on menu deletion. A new test file was created since there were no pre-existing kernel tests for menu_ui. The test creates a menu configuration, adds the menu to a node type's available menu settings, then deletes the menu configuration entity.
Patch is ready for review since #15 passed, and test only patch failed on the introduced assertions.
Comment #19
benjy commentedCouldn't the node module implement node_menu_delete() ?
Replacing this we're losing the sort behaviour from menu_ui_get_menus()?
Comment #20
dpiThe node module does not know about the menu items, or available menus. I put that functionality in
menu_ui_menu_deletesince menu_ui module is responsible for it all.No problem, didnt realise this was a thing from reading the function doc block.
Comment #21
dpiPatch changes reverts to using
menu_ui_get_menus. Invalid menu ID's filtered witharray_intersect_key.Comment #24
misc commentedUpdated patch for 8.4.x-dev
Comment #25
misc commentedSee now that the tests will fail with patch. But unassigning for now.
Comment #28
Yaazkal commentedHi, this happens not only when a menu has been created and then deleted, but also when a migration takes place.
To reproduce
I guess that this happens because in D7 the menu is 'main-menu' but in D8 the menu is 'main', so from the D8 perspective, 'main-menu' doesn't exist. So I guess that this can be handled too in the migration process as seems that that information of the available menu for a given content type is not migrated/updated.
Not sure if a new issue must be created but as this is related I report it here. Let me know if I had to create a new issue.
Comment #29
arosboro commentedThis resolved the issue for me after migrating from Drupal 7 to Drupal 8.
Comment #30
mirom commentedComment #31
dillix commentedWe also have this issue on migrated site. Is there a way to fix this on already migrated site?
Comment #32
dillix commentedI resaved my content types and all errors gone away:)
Comment #34
Adon Irani commentedConfirming resaving my content types worked for me as well.
Comment #35
imanoop commentedThank you for sharing this. Perfect solution.
Thanks,
Anoop Singh
Comment #36
dadderley commentedThanks dillix
Comment #37
crash_springfield commentedFor those tracking site configuration with Git, you could re-save the content type and then export configuration or you could edit the YAML directly. For example, this is happening with my "article" content type, so I can update "node.type.article.yml"
from:
to:
Comment #38
kellyimagined commentedStill an issue in 8.5.4 as well. I have updated the parent.
Comment #39
edwardchiapetI made an update to the patch for 8.5.5.
Comment #40
edwardchiapetPrevious patch was wrong - here is the the updated patch for 8.5.x.
Comment #42
Christopher Riley commentedI had to manually run the patch it didn't play nice with composer. Anyone have any ideas?
Comment #44
ckaotikI just wish there was better integration for third_party_settings. For example, a way to say "By the way, that thing is now a dependency!". Or handling for default settings, so the provider is not added as a dependency when it's added config is empty.
Then we could do something like this:
Comment #46
johnwebdev commentedI followed the steps to reproduce on 8.9.x, and the error seems not to be occurring anymore.
Here is the test I wrote, (following the steps listed in the issue)
which passes.
However, it looks like the "llama" menu, is still present on the node type configuration after deleting the menu.
So the following patch updates the test above, to ensure the third party settings is no longer present, which currently fails.
I am not entirely sure when and how this was fixed, and perhaps we could refactor the test to a kernel, because the fix we need here currently would only need to update the third party settings, however, I guess unless not covered somewhere else, it would be nice to prove that we don't actually have this problem anymore. I'll let someone else decide that :)
Comment #50
larowlanTriaged as part of bug smash initiative - this needs a reroll, looks like the last passing patch was from @dpi in #21
Comment #51
yogeshmpawar.
Comment #52
yogeshmpawarComment #54
tvb commentedAs mentioned in #46: displaying the error in the UI cannot be reproduced, but the deleted menu is still available in the node type configuration in third party settings.
Run this snippet after deleting a menu in step 4 above:
Comment #55
tvb commentedRebasing: two merge conflicts in MenuUiNodeTest.php and menu_ui.module. New patch based on #21 en #46:
menu_ui.module:
Moved patch code in menu_ui_menu_delete() to MenuDeleteForm.php
Replace $menu->id() with $this->entity->id().
This hunk fixes the third party settings after deleting a menu via the UI.
Skipped re-applying patch code in menu_ui_form_node_form_alter(). This seems no longer necessary.
menu_ui/tests/src/Kernel/MenuNodeTest.php:
This kernel test, which does not require a UI, always fails. It was replaced with the testDeletedMenuIsNoLongerReferencedAsAnAvailableMenu() (see below)
menu_ui/tests/src/Functional/MenuUiNodeTest.php:
Retrieved from menu_ui/src/Tests/MenuNodeTest.php (cfr. point 2)
Add property and modify setup() accordingly.
Replace assertEqual() with assertEquals().
Replace assertOption() with assertSession()->optionExists().
Replace assertNoOption() with aassertSession()->optionNotExists.
Add testDeletedMenuIsNoLongerReferencedAsAnAvailableMenu() from 2592829-46--TEST-ONLY.patch in comment #46.
Replace $menu->delete(); with:
Replace drupalPostForm() with drupalGet() and submitForm().
Replace assertOptionByText() with assertSession()->optionExists().
Replace assertOption() with assertSession()->optionExists().
Replace assertNoOption() with aassertSession()->optionNotExists.
Comment #58
pameeela commentedHmm, I can't reproduce this anymore. Following the steps in the IS, it works as expected.
Before deleting:

After deleting:

And no warnings in the logs.
Comment #59
pameeela commentedI'm going to close this since the original issue is no longer valid. I will create a follow up for the lingering config.
Comment #60
pameeela commented