Problem/Motivation

An error is being thrown when navigating to URL that does not exist.

Error: Call to a member function getPath() on null in Drupal\navigation\EntityRouteHelper->getContentEntityFromRoute() (line 58 of /var/www/html/web/core/modules/navigation/src/EntityRouteHelper.php)

Steps to reproduce

Install Drupal, log in as administrator and navigate to a URL that does not exit in the system.

Proposed resolution

Change getContentEntityFromRoute method of EntityRouteHelper class to use Null-safe operator and cast the result to string to avoid using NULL as a key of the array and triggering a deprecation notice.

  public function getContentEntityFromRoute(): ?ContentEntityInterface {
    $route = $this->routeMatch->getRouteObject();
    if (!$route) {
      return NULL;
    }

    $path = $route->getPath();
    if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) {
      return NULL;
    }

    $entity = $this->routeMatch->getParameter($entity_type);
    if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type) {
      return $entity;
    }

    return NULL;
  }

Remaining tasks

None

User interface changes

None

Introduced terminology

None

API changes

None

Data model changes

None

Release notes snippet

Issue fork drupal-3565886

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

pjotr.savitski created an issue. See original summary.

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

libbna’s picture

Status: Active » Needs review
StatusFileSize
new65.79 KB
new38.87 KB

I was able to reproduce the issue on PHP v8.3 and resolved it as per the suggestion.

sourav_paul’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new155.66 KB
new96.35 KB

I've also get the same error on D11.3 with php 8.3, I've applied MR as a patch which has applied nice & cleanly. It resolved the issue.

Moving it to RTBC.

ss:
img

img

catch’s picture

Status: Reviewed & tested by the community » Needs work

I think we should probably be getting the route object, checking whether it's null, and only then getting the path and seeing if it's in the content entity paths, otherwise we're checking if there's an empty string in the array with the current approach.

Also this could probably have test coverage.

diegodz’s picture

diegodz’s picture

I am seeing a similar fatal error in Drupal 11 when accessing 404 pages with the
Navigation module enabled. Possibly the same root cause.

pjotr.savitski’s picture

@catch I agree with the suggestion on the code side and that would be a lot more explicit and readable.

I suppose that the cleanest way to test that would be either to write a unit test that mocks all the constructor arguments or write a kernel test that fetched the service and test just that method with current request stack being given a request to non-existing path.

I wont get to writing the test code right now, but here is more or less the code you've described.

  public function getContentEntityFromRoute(): ?ContentEntityInterface {
    $route = $this->routeMatch->getRouteObject();
    if (!$route) {
      return NULL;
    }
    
    $path = $route->getPath();
    if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) {
      return NULL;
    }

    $entity = $this->routeMatch->getParameter($entity_type);
    if ($entity instanceof ContentEntityInterface && $entity->getEntityTypeId() === $entity_type) {
      return $entity;
    }

    return NULL;
  }
pjotr.savitski’s picture

@diegodz Yes, that should be the same thing. The page content is served with code 404 and the error message is added to the end of the page. I did not initially notice the visuals and found that from the log.

pjotr.savitski’s picture

Title: Navigation module throws an error on mising entity URL » Navigation module throws an error on mising URL
Issue summary: View changes
diegodz’s picture

I've closed the other issue as a duplicate to keep everything centralized here.

As @pjotr.savitski also pointed out, the root cause isn't related to the entity. The error occurs when processing a request for a non-existent route (404), where the navigation module assumes a valid route/path object and calls getPath() if the result is null.

pjotr.savitski changed the visibility of the branch 3565886-navigation-module-throws to hidden.

pjotr.savitski changed the visibility of the branch 3565886-navigation-module-throws to active.

pjotr.savitski’s picture

Status: Needs work » Needs review
diegodz’s picture

Status: Needs review » Reviewed & tested by the community

I have applied this adjustment in the projects where the error was occurring, and in all cases it was successfully resolved.
Additionally, I performed manual QA to validate the correct behavior, and no further issues were identified.

f0ns’s picture

Just ran into this funky error and found this issue.

The website encountered an unexpected error. Try again later.
Error: Call to a member function getPath() on null in Drupal\navigation\EntityRouteHelper->getContentEntityFromRoute() (line 58 of core/modules/navigation/src/EntityRouteHelper.php).

The suggested fix seems to solve the issue.

nicxvan’s picture

I ran the test only job, took me a second to find the new location!

I confirmed it fails with the expected error:

1) Drupal\Tests\navigation\Kernel\NavigationEntityRouteHelperTest::testGetContentEntityFromRouteWithNonExistentRoute
Error: Call to a member function getPath() on null
/builds/issue/drupal-3565886/core/modules/navigation/src/EntityRouteHelper.php:58
/builds/issue/drupal-3565886/core/modules/navigation/tests/src/Kernel/NavigationEntityRouteHelperTest.php:38
ERRORS!

The video from diegodz confirms the fix applied as well.

I reviewed the code and the test, and I think there are two modules in the test that I'm not sure we need.

Not going to bump this out of RTBC at the moment, but it would be good to confirm.

sourav_paul’s picture

I've tested the latest changes on D11.3 with php 8.3, Patch was applied cleanly & issue got resolved.
Sharing ss for reference.

Before:
img

After:
img

Applied patch:
img

RTBC +1

Thanks...

pjotr.savitski’s picture

Issue summary: View changes
catch’s picture

Title: Navigation module throws an error on mising URL » Navigation module throws an error on missing URL
casey’s picture

StatusFileSize
new2.23 KB

Snapshot of latest state of MR for safe usage with composer patches

berdir’s picture

diegodz’s picture

Hi @berdir, yes, it is indeed a duplicate. In fact, I also closed the thread https://www.drupal.org/project/drupal/issues/3566389 that I created when I realized it was related and wanted to centralize everything here.

You can see the video in comment #7 where you can see that it's the same problem.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

  • larowlan committed 32b49a77 on 11.3.x
    fix: #3565886 Navigation module throws an error on missing URL
    
    By:...

  • larowlan committed a6d613ee on 11.x
    fix: #3565886 Navigation module throws an error on missing URL
    
    By:...

  • larowlan committed acaf0b1c on main
    fix: #3565886 Navigation module throws an error on missing URL
    
    By:...
larowlan’s picture

Version: main » 11.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed to main and backported to 11.x and 11.3.x as the risk of disruption is low and this is a bug fix.

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.

Status: Fixed » Closed (fixed)

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