PHP error: Call to a member function id() on null

Created on 17 November 2023, about 1 year ago
Updated 20 November 2023, about 1 year ago

Problem/Motivation

While looking at the logs I noticed that on our production site there were quite a few errors related to the following.
The URL that triggered the error looks like this: /entity-embed/edit-embedded/media/5af3cba5-8dc3-474c-9b21-70962d733e77
The error it outputs:
Error: Call to a member function id() on null in Drupal\entity_embed\Controller\EditEmbeddedEntity->edit() (line 22 of modules/contrib/entity_embed/src/Controller/EditEmbeddedEntity.php).

Steps to reproduce

1. Try to access a non-existing media entity by the route /entity-embed/edit-embedded/media/5af3cba5-8dc3-474c-9b21-70962d733e77 where the media id does not exist.
2. Notice the error in the logs

Proposed resolution

The following is a quick fix, it can probably be improved significant:

In the file EditEmbeddedEntitiy.php Change the line 22:

return $this->redirect("entity.$type.edit_form", [$type => $entity->id()]);

to

return $entity
      ? $this->redirect("entity.$type.edit_form", [$type => $entity->id()])
      : $this->redirect('system.404');

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

RTBC

Version

1.5

Component

Code

Created by

🇩🇰Denmark dasj19

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @dasj19
  • First commit to issue fork.
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India viren18febS

    I have updated the code as per solution provided & added patch, please review.

  • 🇩🇰Denmark dasj19

    Patch looks good

  • 🇮🇳India Vivek Panicker Kolkata

    Change looks good.

  • 🇮🇳India Vivek Panicker Kolkata

    However under what situation are we arriving at this condition?

    I created a node, embedded it in an article page, then deleted the node.

    When I load the article page I see this:

    Code gets stopeed here:

    In the error logs I get a proper log:

      2    20/Nov 14:06   entity_embed   Error      Drupal\entity_embed\Exception\EntityNotFoundException: Unable to load embedded node entity 593344e1-23fc-4524-8a8e-aadefe70f43a. in Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter->process() (line 174  
                                                    of /app/web/modules/contrib/entity_embed/src/Plugin/Filter/EntityEmbedFilter.php).                                                                                                                            
    
    

    So we would need some steps to reproduce the issue first.

  • 🇮🇳India Vivek Panicker Kolkata

    The issue can be reproduced in the following way:
    - Embed content A inside content B
    - Delete content A
    - Edit content B
    - Now click on edit button for embedded content.
    - See the error mentioned in the issue description in the log.

    I applied the patch and checked.

    We see the same popup that we see when we click on the embed button.


  • Status changed to RTBC about 1 year ago
Production build 0.71.5 2024