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

Make the "ea" global variable dynamic #6273

Merged
merged 1 commit into from
Oct 12, 2024
Merged
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: 2 additions & 0 deletions src/Context/AdminContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
/**
* A context object that stores all the state and config of the current admin request.
*
* Please duplicate any new methods added here in the AdminContextProvider class.
*
* @author Javier Eguiluz <[email protected]>
*/
final class AdminContext
Expand Down
136 changes: 134 additions & 2 deletions src/Provider/AdminContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@

use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* Inject this in services that need to get the admin context object.
Expand All @@ -20,10 +31,131 @@ public function __construct(RequestStack $requestStack)
$this->requestStack = $requestStack;
}

public function getContext(): ?AdminContext
public function getContext(bool $throw = false): ?AdminContext
{
$currentRequest = $this->requestStack->getCurrentRequest();

return null !== $currentRequest ? $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE) : null;
if (null === $currentRequest) {
if ($throw) {
throw new \LogicException('Cannot use the EasyAdmin context: no request is available.');
}

return null;
}

return $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE);
}

public function getRequest(): Request
{
return $this->getContext(true)->getRequest();
}

public function getReferrer(): ?string
{
return $this->getContext(true)->getReferrer();
}

public function getI18n(): I18nDto
{
return $this->getContext(true)->getI18n();
}

public function getCrudControllers(): CrudControllerRegistry
{
return $this->getContext(true)->getCrudControllers();
}

public function getEntity(): EntityDto
{
return $this->getContext(true)->getEntity();
}

public function getUser(): ?UserInterface
{
return $this->getContext(true)->getUser();
}

public function getAssets(): AssetsDto
{
return $this->getContext(true)->getAssets();
}

public function getSignedUrls(): bool
{
return $this->getContext(true)->getSignedUrls();
}

public function getAbsoluteUrls(): bool
{
return $this->getContext(true)->getAbsoluteUrls();
}

public function getDashboardTitle(): string
{
return $this->getContext(true)->getDashboardTitle();
}

public function getDashboardFaviconPath(): string
{
return $this->getContext(true)->getDashboardFaviconPath();
}

public function getDashboardControllerFqcn(): string
{
return $this->getContext(true)->getDashboardControllerFqcn();
}

public function getDashboardRouteName(): string
{
return $this->getContext(true)->getDashboardRouteName();
}

public function getDashboardContentWidth(): string
{
return $this->getContext(true)->getDashboardContentWidth();
}

public function getDashboardSidebarWidth(): string
{
return $this->getContext(true)->getDashboardSidebarWidth();
}

public function getDashboardHasDarkModeEnabled(): bool
{
return $this->getContext(true)->getDashboardHasDarkModeEnabled();
}

/**
* @return LocaleDto[]
*/
public function getDashboardLocales(): array
{
return $this->getContext(true)->getDashboardLocales();
}

public function getMainMenu(): MainMenuDto
{
return $this->getContext(true)->getMainMenu();
}

public function getUserMenu(): UserMenuDto
{
return $this->getContext(true)->getUserMenu();
}

public function getCrud(): ?CrudDto
{
return $this->getContext(true)->getCrud();
}

public function getSearch(): ?SearchDto
{
return $this->getContext(true)->getSearch();
}

public function getTemplatePath(string $templateName): string
{
return $this->getContext(true)->getTemplatePath($templateName);
}
}
5 changes: 1 addition & 4 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function getFilters(): array

public function getGlobals(): array
{
$context = $this->adminContextProvider->getContext();

// when there's an admin context, make it available in all templates as a short named variable
return null === $context ? [] : ['ea' => $context];
return ['ea' => $this->adminContextProvider];
}

/**
Expand Down