diff --git a/doc/crud.rst b/doc/crud.rst index f4efa2b2e8..fe6ce95e00 100644 --- a/doc/crud.rst +++ b/doc/crud.rst @@ -820,6 +820,7 @@ Use the ``unsetAll()`` method to remove all existing query parameters:: $url = $this->adminUrlGenerator ->setController(SomeCrudController::class) ->setAction('theActionName') + ->setFragment('tab-main') ->generateUrl(); // ... @@ -848,7 +849,8 @@ method (it will be called automatically for you): {% set url = ea_url() .setController('App\\Controller\\Admin\\SomeCrudController') - .setAction('theActionName') %} + .setAction('theActionName') + .setFragment('tab-main') %} Generating CRUD URLs from outside EasyAdmin ........................................... diff --git a/src/Config/Option/EA.php b/src/Config/Option/EA.php index 70fbd43cd1..2b23188557 100644 --- a/src/Config/Option/EA.php +++ b/src/Config/Option/EA.php @@ -28,6 +28,7 @@ final class EA public const ROUTE_NAME = 'routeName'; public const ROUTE_PARAMS = 'routeParams'; public const ROUTE_CREATED_BY_EASYADMIN = 'routeCreatedByEasyAdmin'; + public const ROUTE_FRAGMENT = '_fragment'; public const SORT = 'sort'; /** @deprecated this parameter is no longer used because menu items are now highlighted automatically */ public const SUBMENU_INDEX = 'submenuIndex'; diff --git a/src/Router/AdminUrlGenerator.php b/src/Router/AdminUrlGenerator.php index 0f7ad7e50f..904eff664f 100644 --- a/src/Router/AdminUrlGenerator.php +++ b/src/Router/AdminUrlGenerator.php @@ -76,6 +76,13 @@ public function setEntityId(mixed $entityId): AdminUrlGeneratorInterface return $this; } + public function setFragment(string $fragment): AdminUrlGeneratorInterface + { + $this->setRouteParameter(EA::ROUTE_FRAGMENT, $fragment); + + return $this; + } + public function get(string $paramName): mixed { if (false === $this->isInitialized) { diff --git a/src/Router/AdminUrlGeneratorInterface.php b/src/Router/AdminUrlGeneratorInterface.php index 32de85301f..c7486ddba3 100644 --- a/src/Router/AdminUrlGeneratorInterface.php +++ b/src/Router/AdminUrlGeneratorInterface.php @@ -23,6 +23,8 @@ public function setRoute( public function setEntityId(mixed $entityId): self; + public function setFragment(string $fragment): self; + public function get(string $paramName): mixed; public function set(string $paramName, mixed $paramValue): self; diff --git a/tests/Router/AdminUrlGeneratorTest.php b/tests/Router/AdminUrlGeneratorTest.php index a33f905d38..75abf2318e 100644 --- a/tests/Router/AdminUrlGeneratorTest.php +++ b/tests/Router/AdminUrlGeneratorTest.php @@ -281,6 +281,14 @@ public function testRelativeUrls() $this->assertSame('http://localhost/admin?crudAction=index&crudControllerFqcn=App%5CController%5CAdmin%5CSomeCrudController&foo=bar&foo1=bar1', $adminUrlGenerator->generateUrl()); } + public function testSetFragment() + { + $adminUrlGenerator = $this->getAdminUrlGenerator(); + + $adminUrlGenerator->setFragment('tab-1'); + $this->assertSame('http://localhost/admin?foo=bar#tab-1', $adminUrlGenerator->generateUrl()); + } + private function getAdminUrlGenerator(bool $signedUrls = false, bool $absoluteUrls = true): AdminUrlGeneratorInterface { self::bootKernel();