Problem/Motivation
Deleting a node programmatically through $node->delete(), the error below occurs
Error: Call to a member function isTranslatable() on null in _menu_ui_node_save() (line 95 of core/modules/menu_ui/menu_ui.module).
Steps to reproduce
Adding a new moderation state "delete" into Editorial workflow after the node was archived, the admin user tries to cleanly delete the node.
function custom_module_form_page_submit (array $form, \Drupal\Core\Form\FormStateInterface $form_state){
$entity = $form_state->getFormObject()->getEntity();
$nid = $entity->id();
if ($moderation_state == 'delete') {
$node = Node::load($nid);
if (isset($node)) {
$node->delete();
}
}
}
After the user clicks "submit" button, the error occurs
The website encountered an unexpected error. Please try again later.
Error: Call to a member function isTranslatable() on null in _menu_ui_node_save() (line 95 of core/modules/menu_ui/menu_ui.module).
Proposed resolution
- Add check to see if $entity is declared and is not null before proceeding.
Remaining tasks
✅ Adding the check condition ( if (isset($entity)) ) before accessing $entity->isTranslatable()
function _menu_ui_node_save(NodeInterface $node, array $values) {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
if (!empty($values['entity_id'])) {
$entity = MenuLinkContent::load($values['entity_id']);
if (isset($entity)) {
if ($entity->isTranslatable()) {
......
}
}
else {
// Create a new menu_link_content entity.
$entity = MenuLinkContent::create([
'link' => ['uri' => 'entity:node/' . $node->id()],
'langcode' => $node->language()->getId(),
]);
$entity->enabled->value = 1;
}
if (isset($entity)) {
.......
$entity->save();
}
}
User interface changes
API changes
Data model changes
Release notes snippet