Skip to content
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
4 changes: 3 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemMatcherInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Orm\EntityPaginatorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface;
use EasyCorp\Bundle\EasyAdminBundle\DependencyInjection\EasyAdminExtension;
use EasyCorp\Bundle\EasyAdminBundle\EventListener\AdminRouterSubscriber;
use EasyCorp\Bundle\EasyAdminBundle\EventListener\CrudResponseListener;
Expand Down Expand Up @@ -281,7 +282,8 @@
->arg(2, tagged_iterator(EasyAdminExtension::TAG_FIELD_CONFIGURATOR))
->arg(3, service(FormLayoutFactory::class))

->set(FieldProvider::class)
->set(FieldProviderInterface::class)
->class(FieldProvider::class)
->arg(0, service(AdminContextProvider::class))

->set(FilterFactory::class)
Expand Down
4 changes: 2 additions & 2 deletions src/Attribute/AdminDashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function __construct(
/**
* @var string|null $routePath The path of the Symfony route that will be created for the dashboard (e.g. '/admin)
*/
public /* ?string */ $routePath = null,
/* ?string */ public $routePath = null,
/**
* @var string|null $routeName The name of the Symfony route that will be created for the dashboard (e.g. 'admin')
*/
public /* ?string */ $routeName = null,
/* ?string */ public $routeName = null,
/**
* @var array{
* requirements?: array<string, string>,
Expand Down
22 changes: 22 additions & 0 deletions src/Contracts/Provider/FieldProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider;

use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;

interface FieldProviderInterface
{
/**
* @param iterable<FieldInterface|string> $fields
*
* @return FieldCollection<FieldDto>
*/
public function createCollection(iterable $fields): FieldCollection;

/**
* @return array<FieldInterface>
*/
public function getDefaultFields(string $pageName): array;
}
24 changes: 15 additions & 9 deletions src/Controller/AbstractCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
Expand Down Expand Up @@ -47,7 +48,6 @@
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityUpdater;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Provider\FieldProvider;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Security\Permission;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -96,7 +96,7 @@ public function configureFilters(Filters $filters): Filters

public function configureFields(string $pageName): iterable
{
return $this->container->get(FieldProvider::class)->getDefaultFields($pageName);
return $this->container->get(FieldProviderInterface::class)->getDefaultFields($pageName);
}

public static function getSubscribedServices(): array
Expand All @@ -111,7 +111,7 @@ public static function getSubscribedServices(): array
EntityFactory::class => '?'.EntityFactory::class,
EntityRepository::class => '?'.EntityRepository::class,
EntityUpdater::class => '?'.EntityUpdater::class,
FieldProvider::class => '?'.FieldProvider::class,
FieldProviderInterface::class => '?'.FieldProviderInterface::class,
FilterFactory::class => '?'.FilterFactory::class,
FormFactory::class => '?'.FormFactory::class,
PaginatorFactory::class => '?'.PaginatorFactory::class,
Expand All @@ -130,7 +130,7 @@ public function index(AdminContext $context)
throw new ForbiddenActionException($context);
}

$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
$fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_INDEX));
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $fields, $context->getEntity());
$queryBuilder = $this->createIndexQueryBuilder($context->getSearch(), $context->getEntity(), $fields, $filters);
$paginator = $this->container->get(PaginatorFactory::class)->create($queryBuilder);
Expand Down Expand Up @@ -184,7 +184,8 @@ public function detail(AdminContext $context)
throw new InsufficientEntityPermissionException($context);
}

$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)));
$fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_DETAIL));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());

Expand Down Expand Up @@ -219,7 +220,8 @@ public function edit(AdminContext $context)
throw new InsufficientEntityPermissionException($context);
}

$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
$fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_EDIT));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
/** @var TEntity $entityInstance */
Expand Down Expand Up @@ -305,7 +307,8 @@ public function new(AdminContext $context)
/** @var class-string<TEntity> $entityFqcn */
$entityFqcn = $context->getEntity()->getFqcn();
$context->getEntity()->setInstance($this->createEntity($entityFqcn));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
$fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_NEW));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());

Expand Down Expand Up @@ -473,8 +476,11 @@ public function autocomplete(AdminContext $context): JsonResponse

/** @var CrudControllerInterface $controller */
$controller = $this->container->get(ControllerFactory::class)->getCrudControllerInstance($autocompleteContext[EA::CRUD_CONTROLLER_FQCN] ?? $context->getRequest()->get(EA::CRUD_CONTROLLER_FQCN), Action::INDEX, $context->getRequest());

$fields = $this->container->get(FieldProviderInterface::class)->createCollection($controller->configureFields($autocompleteContext['originatingPage']));

/** @var FieldDto|null $field */
$field = FieldCollection::new($controller->configureFields($autocompleteContext['originatingPage']))->getByProperty($autocompleteContext['propertyName']);
$field = $fields->getByProperty($autocompleteContext['propertyName']);
/** @var \Closure|null $queryBuilderCallable */
$queryBuilderCallable = $field?->getCustomOption(AssociationField::OPTION_QUERY_BUILDER_CALLABLE);

Expand All @@ -494,7 +500,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD

public function renderFilters(AdminContext $context): KeyValueStore
{
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
$fields = $this->container->get(FieldProviderInterface::class)->createCollection($this->configureFields(Crud::PAGE_INDEX));
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());

Expand Down
12 changes: 8 additions & 4 deletions src/Provider/FieldProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
namespace EasyCorp\Bundle\EasyAdminBundle\Provider;

use Doctrine\DBAL\Types\Types;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\AdminContextProviderInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Provider\FieldProviderInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;

/**
* @author Javier Eguiluz <[email protected]>
*/
final class FieldProvider
final class FieldProvider implements FieldProviderInterface
{
public function __construct(
private readonly AdminContextProviderInterface $adminContextProvider,
) {
}

/**
* @return array<Field>
*/
public function createCollection(iterable $fields): FieldCollection
{
return FieldCollection::new($fields);
}

public function getDefaultFields(string $pageName): array
{
$defaultPropertyNames = [];
Expand Down
Loading