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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

dasj19 created an issue. See original summary.

viren18febS made their first commit to this issue’s fork.

viren18febs’s picture

Status: Active » Needs review
StatusFileSize
new651 bytes

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

dasj19’s picture

Patch looks good

vivek panicker’s picture

Change looks good.

vivek panicker’s picture

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:
alt

Code gets stopeed here:
alt

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.

vivek panicker’s picture

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.

alt
alt

So the patch is working fine and we can move this issue forward.

vivek panicker’s picture

Status: Needs review » Reviewed & tested by the community

dave reid made their first commit to this issue’s fork.

  • dave reid committed 3c5dbc76 on 8.x-1.x
    fix: #3402443 Return a 404 when an embedded entity can't be edited
    
    By:...
dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • dave reid committed 3c5dbc76 on 2.0.x
    fix: #3402443 Return a 404 when an embedded entity can't be edited
    
    By:...