Loading core/modules/navigation/src/EntityRouteHelper.php +6 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,12 @@ public function isContentEntityRoute(): bool { } public function getContentEntityFromRoute(): ?ContentEntityInterface { $path = $this->routeMatch->getRouteObject()->getPath(); $route = $this->routeMatch->getRouteObject(); if (!$route) { return NULL; } $path = $route->getPath(); if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) { return NULL; } Loading core/modules/navigation/tests/src/Kernel/NavigationEntityRouteHelperTest.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\navigation\Kernel; use Drupal\KernelTests\KernelTestBase; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; use Symfony\Component\HttpFoundation\Request; /** * Tests \Drupal\navigation\EntityRouteHelper. * * @see \Drupal\navigation\EntityRouteHelper */ #[Group('navigation')] #[RunTestsInSeparateProcesses] class NavigationEntityRouteHelperTest extends KernelTestBase { /** * {@inheritdoc} */ protected static $modules = [ 'system', 'navigation', 'layout_builder', 'user', ]; /** * Tests getContentEntityFromRoute() method when the route does not exist. */ public function testGetContentEntityFromRouteWithNonExistentRoute(): void { $request = Request::create('/does-not-exist'); $response = $this->container->get('http_kernel')->handle($request); $this->assertEquals(404, $response->getStatusCode()); $this->assertNull($this->container->get('navigation.entity_route_helper')->getContentEntityFromRoute()); } } Loading
core/modules/navigation/src/EntityRouteHelper.php +6 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,12 @@ public function isContentEntityRoute(): bool { } public function getContentEntityFromRoute(): ?ContentEntityInterface { $path = $this->routeMatch->getRouteObject()->getPath(); $route = $this->routeMatch->getRouteObject(); if (!$route) { return NULL; } $path = $route->getPath(); if (!$entity_type = $this->getContentEntityPaths()[$path] ?? NULL) { return NULL; } Loading
core/modules/navigation/tests/src/Kernel/NavigationEntityRouteHelperTest.php 0 → 100644 +41 −0 Original line number Diff line number Diff line <?php declare(strict_types=1); namespace Drupal\Tests\navigation\Kernel; use Drupal\KernelTests\KernelTestBase; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; use Symfony\Component\HttpFoundation\Request; /** * Tests \Drupal\navigation\EntityRouteHelper. * * @see \Drupal\navigation\EntityRouteHelper */ #[Group('navigation')] #[RunTestsInSeparateProcesses] class NavigationEntityRouteHelperTest extends KernelTestBase { /** * {@inheritdoc} */ protected static $modules = [ 'system', 'navigation', 'layout_builder', 'user', ]; /** * Tests getContentEntityFromRoute() method when the route does not exist. */ public function testGetContentEntityFromRouteWithNonExistentRoute(): void { $request = Request::create('/does-not-exist'); $response = $this->container->get('http_kernel')->handle($request); $this->assertEquals(404, $response->getStatusCode()); $this->assertNull($this->container->get('navigation.entity_route_helper')->getContentEntityFromRoute()); } }