Call to a member function isTranslatable() on null in _menu_ui_node_save()

Created on 1 November 2021, almost 3 years ago
Updated 25 January 2024, 8 months ago

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

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Menu UI 

Last updated about 1 month ago

No maintainer
Created by

🇨🇦Canada jamesyao

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024