Skip to content

Commit

Permalink
Added the dashboard context ID to actions that use custom routes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Jun 16, 2020
1 parent 3af51c7 commit 329eb5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/Factory/ActionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -23,14 +24,16 @@
final class ActionFactory
{
private $adminContextProvider;
private $dashboardRegistry;
private $authChecker;
private $translator;
private $urlGenerator;
private $crudUrlGenerator;

public function __construct(AdminContextProvider $adminContextProvider, AuthorizationCheckerInterface $authChecker, TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator, CrudUrlGenerator $crudUrlGenerator)
public function __construct(AdminContextProvider $adminContextProvider, DashboardControllerRegistry $dashboardRegistry, AuthorizationCheckerInterface $authChecker, TranslatorInterface $translator, UrlGeneratorInterface $urlGenerator, CrudUrlGenerator $crudUrlGenerator)
{
$this->adminContextProvider = $adminContextProvider;
$this->dashboardRegistry = $dashboardRegistry;
$this->authChecker = $authChecker;
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
Expand Down Expand Up @@ -91,6 +94,8 @@ public function processGlobalActions(ActionConfigDto $actionsDto): ActionCollect
private function processAction(string $pageName, ActionDto $actionDto, ?EntityDto $entityDto = null): ActionDto
{
$adminContext = $this->adminContextProvider->getContext();
$dashboardControllerFqcn = $adminContext->getDashboardControllerFqcn();
$adminContextId = $this->dashboardRegistry->getContextIdByControllerFqcn($dashboardControllerFqcn);
$translationDomain = $adminContext->getI18n()->getTranslationDomain();
$defaultTranslationParameters = $adminContext->getI18n()->getTranslationParameters();
$currentPage = $adminContext->getCrud()->getCurrentPage();
Expand All @@ -114,7 +119,7 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt
$defaultTemplatePath = $adminContext->getTemplatePath('crud/action');
$actionDto->setTemplatePath($actionDto->getTemplatePath() ?? $defaultTemplatePath);

$actionDto->setLinkUrl($this->generateActionUrl($currentPage, $adminContext->getRequest(), $actionDto, $entityDto));
$actionDto->setLinkUrl($this->generateActionUrl($adminContextId, $currentPage, $adminContext->getRequest(), $actionDto, $entityDto));

if (!$actionDto->isGlobalAction() && \in_array($pageName, [Crud::PAGE_EDIT, Crud::PAGE_NEW], true)) {
$actionDto->setHtmlAttribute('form', sprintf('%s-%s-form', $pageName, $entityDto->getName()));
Expand All @@ -123,7 +128,7 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt
return $actionDto;
}

private function generateActionUrl(string $currentAction, Request $request, ActionDto $actionDto, ?EntityDto $entityDto = null): string
private function generateActionUrl(string $adminContextId, string $currentAction, Request $request, ActionDto $actionDto, ?EntityDto $entityDto = null): string
{
if (null !== $url = $actionDto->getUrl()) {
if (\is_callable($url)) {
Expand All @@ -139,6 +144,8 @@ private function generateActionUrl(string $currentAction, Request $request, Acti
$routeParameters = $routeParameters($entityInstance);
}

$routeParameters = array_merge(['eaContext' => $adminContextId], $routeParameters);

return $this->urlGenerator->generate($routeName, $routeParameters);
}

Expand Down
9 changes: 5 additions & 4 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@

->set(ActionFactory::class)
->arg(0, new Reference(AdminContextProvider::class))
->arg(1, new Reference(AuthorizationChecker::class))
->arg(2, new Reference('translator'))
->arg(3, new Reference('router'))
->arg(4, new Reference(CrudUrlGenerator::class))
->arg(1, new Reference(DashboardControllerRegistry::class))
->arg(2, new Reference(AuthorizationChecker::class))
->arg(3, new Reference('translator'))
->arg(4, new Reference('router'))
->arg(5, new Reference(CrudUrlGenerator::class))

->set(SecurityVoter::class)
->arg(0, new Reference(AuthorizationChecker::class))
Expand Down

0 comments on commit 329eb5b

Please sign in to comment.