Generated with the help of an LLM.
Problem/Motivation
When a node has a pending revision (for example a draft created with Content Moderation), saving the node form or triggering an AJAX request in it without having clicked "Add/Edit menu link" produces:
Warning: Undefined array key "title" in Drupal\menu_ui\Plugin\Validation\Constraint\MenuSettingsConstraintValidator->validate() (line 42 of core/modules/menu_ui/src/Plugin/Validation/Constraint/MenuSettingsConstraintValidator.php). Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated
When errors are displayed, the warning is printed into the AJAX response, which then fails with "An AJAX HTTP error occurred ... StatusText: parsererror", for example when editing a paragraph.
The module adds core's menu_ui_node_builder as entity builder. That builder copies $form_state->getValue('menu') to $node->menu. Until the widget has been loaded, the menu form only contains menu[customize], so $node->menu holds no link values. MenuSettingsConstraintValidator runs for non-default revisions and reads $values['title'] without checking it.
The 2.x branch has the same problem in Hooks::formNodeFormAlter(). There, menu_ui_node_builder() is also deprecated in drupal:11.4.0.
Steps to reproduce
- Install Content Moderation and enable the Editorial workflow for the Basic page content type.
- Enable the asynchronous menu widget for Basic page, with at least one available menu.
- Create and publish a Basic page.
- Edit it and save it as Draft, so that the node has a pending revision.
- Edit the latest revision and save it again without clicking "Add/Edit menu link".
- The warning is shown or logged.
Proposed resolution
Replace menu_ui_node_builder with a module-specific entity builder that only sets $node->menu when the widget has been loaded ($form_state->get('menu_ui_async_widget')). When the property is left unset, the validator returns early, which matches core's own comment: "do not attempt to validate the menu settings since they are not being modified".
Remaining tasks
- Fix on 1.0.x.
- Port to 2.x as a method on
Hooks. - Consider a kernel or functional test covering a pending revision.
User interface changes
None.
API changes
New function menu_ui_async_widget_node_builder() (1.0.x).
Data model changes
None.
Comments
Comment #4
peter törnstrand commentedFixed in 1.0.4 and 2.x.