Problem/Motivation

If a user visits a path /node/1234/whatlinkshere, but that node 1234 has been deleted, the user is shown a WSOD with a php error message:

TypeError: Drupal\whatlinkshere\LinkManager::getReferenceContent(): Argument #1 ($entity) must be of type Drupal\Core\Entity\EntityInterface, null given, called in /home/gwulocom/domains/gwulo.com/public_html/web/modules/contrib/whatlinkshere/src/Controller/WhatLinksHereController.php on line 120 in Drupal\whatlinkshere\LinkManager->getReferenceContent() (line 300 of /home/gwulocom/domains/gwulo.com/public_html/web/modules/contrib/whatlinkshere/src/LinkManager.php)

Steps to reproduce

Proposed resolution

Could they see a "404 Page not found" instead?

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

davidhk created an issue. See original summary.

davidhk’s picture

Issue summary: View changes
Arturo-q’s picture

Assigned: Unassigned » Arturo-q

Hi, I will be working on it.

Arturo-q’s picture

Assigned: Arturo-q » Unassigned
Status: Active » Needs review
StatusFileSize
new1.07 KB

Here is the patch, it should fix the issue.
Moving to needs review.

juancec’s picture

Assigned: Unassigned » juancec

HI, I'll review it.

juancec’s picture

Assigned: juancec » Unassigned
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new10.3 KB

After applying the patch a 'NotFoundHttpException' exception will be thrown when go to a deleted/inexisting path. So I think it is fixed. Good job @Arturo1007. Moving it to RTBC.

davidhk’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.95 KB

Thank you for the quick response.

I've since seen the same problem happen with a media entity, and I guess the same will happen with taxonomy terms too, so I've also added the change from #4 to the taxonomyTerm() and media() functions in the attached patch.

johan.gant’s picture

This is great, thanks. Something I've seen across other contrib modules as a more reliable check for loaded entity values is the use of the instanceof test rather than just a check for null. For example, instead of:

if (!$entity) {
  throw new NotFoundHttpException();
}    

try this:

if ($entity instanceof NodeInterface === FALSE) {
  throw new NotFoundHttpException();
}    

You would need to add the required use namespace declaration, and ensure the interface or class instance used matches what you expect to be returned. This process helps with static analysis tools such as phpstan and IDEs like PHPStorm's indexing/symbol map, which all make a for more productive development experience.

Arturo-q’s picture

Assigned: Unassigned » Arturo-q
Status: Needs review » Needs work

Hey, okay, got it, I will be working on it.

Arturo-q’s picture

Assigned: Arturo-q » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.81 KB

Hi, I've made the changes pointed on #8,
I also made the validations with media and Taxonomy like this:

    if ($entity instanceof MediaInterface === FALSE) {
      throw new NotFoundHttpException();
    }
    if ($entity instanceof TermInterface === FALSE) {
      throw new NotFoundHttpException();
    }

Kindly review it.
Regards!.

johan.gant’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, thanks!

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

  • omahm committed 131fe392 on 2.x
    Issue #3313430 by Arturo1007, davidhk, juancec, johan.gant: php TypeOf...
omahm’s picture

Status: Reviewed & tested by the community » Fixed

Merged into dev. Thanks all.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.