Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/6598 pretty urls and selected menu item #6771

Open
wants to merge 4 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ddev/commands/web/run-tests
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

cd ../easyadminbundle
composer install
composer update
vendor/bin/simple-phpunit -v
23 changes: 17 additions & 6 deletions .ddev/commands/web/setup
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/bin/bash

# Installing Symfony Framework
composer create-project symfony/skeleton:"7.1.*" -n
composer create-project symfony/skeleton:"7.2.*" -n
mv -f ./skeleton/* ./
mv -f ./skeleton/.env ./
mv -f ./skeleton/.env.dev ./
mv -f ./skeleton/.gitignore ./
rmdir ./skeleton

Expand All @@ -13,12 +14,22 @@ composer config extra.symfony.allow-contrib true
composer require "easycorp/easyadmin-bundle": "*@dev" symfony/apache-pack -n
composer require debug -n --dev

# Copy example entities, form and controllers
# Create symlinks for example entities, form, controllers and config
mkdir src/Controller/Admin
ln -s ../../../../easyadminbundle/.ddev/example/Controller/Admin/BlogArticleCrudController.php src/Controller/Admin/BlogArticleCrudController.php
ln -s ../../../../easyadminbundle/.ddev/example/Controller/Admin/CategoryCrudController.php src/Controller/Admin/CategoryCrudController.php
ln -s ../../../../easyadminbundle/.ddev/example/Controller/Admin/DashboardController.php src/Controller/Admin/DashboardController.php
ln -s ../../../easyadminbundle/.ddev/example/Entity/BlogArticle.php src/Entity/BlogArticle.php
ln -s ../../../easyadminbundle/.ddev/example/Entity/Category.php src/Entity/Category.php
ln -s ../../../easyadminbundle/.ddev/example/Entity/ContentBlock.php src/Entity/ContentBlock.php
mkdir src/Form/
cp -r ../easyadminbundle/.ddev/example/Entity/* src/Entity/
cp -r ../easyadminbundle/.ddev/example/Controller/* src/Controller/
cp -r ../easyadminbundle/.ddev/example/Form/* src/Form/
ln -s ../../../easyadminbundle/.ddev/example/Form/ContentBlockType.php src/Form/ContentBlockType.php
ln -s ../../../easyadminbundle/.ddev/example/config/routes/easyadmin.yaml config/routes/easyadmin.yaml

# Initialize DB
echo 'DATABASE_URL="mysql://db:db@db:3306/db?serverVersion=10.4.0-MariaDB&charset=utf8mb4"' >> .env.local
echo 'DATABASE_URL="mysql://db:db@db:3306/db?serverVersion=10.11.0-MariaDB&charset=utf8mb4"' >> .env.local
./bin/console doc:sch:up --force

# Clear Cache
sleep 2
./bin/console c:c
2 changes: 1 addition & 1 deletion .ddev/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ additional_hostnames: []
additional_fqdns: []
database:
type: mariadb
version: "10.4"
version: "10.11"
use_dns_when_possible: true
composer_version: "2"
web_environment: []
Expand Down
11 changes: 11 additions & 0 deletions .ddev/example/Controller/Admin/BlogArticleCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use App\Entity\BlogArticle;
use App\Entity\ContentBlock;
use App\Form\ContentBlockType;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminCrud;
use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Filter\EntityFilter;

#[AdminCrud(routePath: '/blog-article')]
class BlogArticleCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
Expand All @@ -36,4 +40,11 @@ public function configureFields(string $pageName): iterable
})
;
}

public function configureFilters(Filters $filters): Filters
{
$filters->add(EntityFilter::new('category'));

return $filters;
}
}
2 changes: 2 additions & 0 deletions .ddev/example/Controller/Admin/CategoryCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Controller\Admin;

use App\Entity\Category;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminCrud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

#[AdminCrud(routePath: '/category')]
class CategoryCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
Expand Down
2 changes: 2 additions & 0 deletions .ddev/example/Controller/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use App\Entity\BlogArticle;
use App\Entity\Category;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminDashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[AdminDashboard]
class DashboardController extends AbstractDashboardController
{
#[Route('/', name: 'admin')]
Expand Down
3 changes: 3 additions & 0 deletions .ddev/example/config/routes/easyadmin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
easyadmin:
resource: .
type: easyadmin.routes
44 changes: 39 additions & 5 deletions src/Menu/MenuItemMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ private function doMarkSelectedLegacyMenuItem(array $menuItems, Request $request
*/
private function doMarkSelectedPrettyUrlsMenuItem(array $menuItems, Request $request): array
{
// the menu-item matching is a 2-phase process:
// the menu-item matching is a 3-phase process:
// 1) traverse all menu items and try to find an exact match with the current URL
// 2) if no exact match is found, traverse all menu items again and try to find a partial match
// 3) if still no match is found, traverse all menu items one last time and ignore the query string parameters
$currentUrlWithoutHost = $request->getPathInfo();
$currentUrlQueryParams = $request->query->all();
unset($currentUrlQueryParams['sort'], $currentUrlQueryParams['page'], $currentUrlQueryParams['query']);
Expand All @@ -187,7 +188,17 @@ private function doMarkSelectedPrettyUrlsMenuItem(array $menuItems, Request $req
$menuItemDto->setSubItems($this->doMarkSelectedPrettyUrlsMenuItem($subItems, $request));
}

if ($menuItemDto->getLinkUrl() === $normalizedCurrentUrl) {
// Remove host part from menu item link URL
$urlParts = parse_url($menuItemDto->getLinkUrl());
$linkUrlWithoutHost = $urlParts['path'];
if (\array_key_exists('query', $urlParts)) {
$linkUrlWithoutHost .= '?'.$urlParts['query'];
}
if (\array_key_exists('fragment', $urlParts)) {
$linkUrlWithoutHost .= '#'.$urlParts['fragment'];
}

if ($linkUrlWithoutHost === $normalizedCurrentUrl) {
$menuItemDto->setSelected(true);

return $menuItems;
Expand All @@ -208,6 +219,29 @@ private function doMarkSelectedPrettyUrlsMenuItem(array $menuItems, Request $req
EA::CRUD_ACTION => Action::INDEX,
]))->generateUrl();

if ($this->traverseMenuItemsAndCheckCurrentUrl($menuItems, $currentUrlWithIndexCrudAction, $request)) {
return $menuItems;
}

$currentUrlWithIndexCrudActionWithoutQueryParams = $this->adminUrlGenerator->unsetAll()->setAll([
EA::DASHBOARD_CONTROLLER_FQCN => $request->attributes->get(EA::DASHBOARD_CONTROLLER_FQCN),
EA::CRUD_CONTROLLER_FQCN => $crudControllerFqcn,
EA::CRUD_ACTION => Action::INDEX,
])->generateUrl();

$this->traverseMenuItemsAndCheckCurrentUrl($menuItems, $currentUrlWithIndexCrudActionWithoutQueryParams, $request);

return $menuItems;
}

/**
* @param MenuItemDto[] $menuItems
*/
private function traverseMenuItemsAndCheckCurrentUrl(
array $menuItems,
string $currentUrlToCheck,
Request $request,
): bool {
foreach ($menuItems as $menuItemDto) {
if ($menuItemDto->isMenuSection()) {
continue;
Expand All @@ -218,14 +252,14 @@ private function doMarkSelectedPrettyUrlsMenuItem(array $menuItems, Request $req
}

// compare the ending of the URL instead of a strict equality because link URLs can be absolute URLs
if ('' !== $menuItemDto->getLinkUrl() && str_ends_with($currentUrlWithIndexCrudAction, $menuItemDto->getLinkUrl())) {
if ('' !== $menuItemDto->getLinkUrl() && str_ends_with($currentUrlToCheck, $menuItemDto->getLinkUrl())) {
$menuItemDto->setSelected(true);

return $menuItems;
return true;
}
}

return $menuItems;
return false;
}

/**
Expand Down