Skip to content

Commit e7115e9

Browse files
committed
bug #7136 Fix hiding and showing of fields in AssociationField and CollectionFi… (michaelKaefer)
This PR was merged into the 4.x branch. Discussion ---------- Fix hiding and showing of fields in AssociationField and CollectionFi… …eld if crud form is used `FieldFactory` removes configured fields by checking against the current page (for example `Crud::PAGE_NEW`) here: `$fieldDto->isDisplayedOn($currentPage)`. Now consider using one of: - `AssociactionField::new(...)->renderAsEmbeddedForm()` - `CollectionField::new(...)->useEntryCrudForm()` `FieldFactory` gets the `$currentPage` of the request: `$this->adminContextProvider->getContext()->getCrud()->getCurrentPage()`. If there are nested CRUD forms (like in the 2 above examples) there have to be multiple current pages. One for the root entity and one for every nested CRUD form. For example when editing (`Crud::PAGE_EDIT`) the root entity we can add new (`Crud::PAGE_NEW`) items to a collection. This is not possible and a partial fix was introduced in #5704 and edited in #7061. This fix produced another error (for example removing fields on `index` pages, I can elaborate if necessary). This PR reverts the partial fixes and adds a new fix by passing different `$currentPage` to `FieldFactory` a) for the root entity and b) for nested CRUD forms. Commits ------- 5bc8490 Fix hiding and showing of fields in AssociationField and CollectionField if crud form is used
2 parents a851f7d + 5bc8490 commit e7115e9

7 files changed

Lines changed: 164 additions & 65 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tests-coverage-view-in-browser: ## Open the generated HTML coverage in your defa
1818

1919
## —— Linters —————————————————————————————————
2020
linter-code-syntax: ## Lint PHP code (in dry-run mode, does not edit files)
21-
docker run --rm -it --pull always -w=/app -v $(shell pwd):/app oskarstark/php-cs-fixer-ga:latest --diff -vvv --dry-run --using-cache=no
21+
docker run --rm -it --pull always -w=/app -v "$(shell pwd)":/app oskarstark/php-cs-fixer-ga:latest --diff -vvv --dry-run --using-cache=no
2222
linter-docs: ## Lint docs
2323
docker run --rm -it --pull always -e DOCS_DIR='/docs' -v $(shell pwd)/doc:/docs oskarstark/doctor-rst:latest --short
2424

src/Controller/AbstractCrudController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function index(AdminContext $context)
144144
}
145145

146146
$entities = $this->container->get(EntityFactory::class)->createCollection($context->getEntity(), $paginator->getResults());
147-
$this->container->get(EntityFactory::class)->processFieldsForAll($entities, $fields);
147+
$this->container->get(EntityFactory::class)->processFieldsForAll($entities, $fields, Crud::PAGE_INDEX);
148148
$procesedFields = $entities->first()?->getFields() ?? FieldCollection::new([]);
149149
$context->getCrud()->setFieldAssets($this->getFieldAssets($procesedFields));
150150
$actions = $this->container->get(EntityFactory::class)->processActionsForAll($entities, $context->getCrud()->getActionsConfig());
@@ -184,7 +184,7 @@ public function detail(AdminContext $context)
184184
throw new InsufficientEntityPermissionException($context);
185185
}
186186

187-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)));
187+
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_DETAIL)), Crud::PAGE_DETAIL);
188188
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
189189
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
190190

@@ -219,7 +219,7 @@ public function edit(AdminContext $context)
219219
throw new InsufficientEntityPermissionException($context);
220220
}
221221

222-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)));
222+
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_EDIT)), Crud::PAGE_EDIT);
223223
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
224224
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
225225
/** @var TEntity $entityInstance */
@@ -305,7 +305,7 @@ public function new(AdminContext $context)
305305
/** @var class-string<TEntity> $entityFqcn */
306306
$entityFqcn = $context->getEntity()->getFqcn();
307307
$context->getEntity()->setInstance($this->createEntity($entityFqcn));
308-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)));
308+
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), FieldCollection::new($this->configureFields(Crud::PAGE_NEW)), Crud::PAGE_NEW);
309309
$context->getCrud()->setFieldAssets($this->getFieldAssets($context->getEntity()->getFields()));
310310
$this->container->get(EntityFactory::class)->processActions($context->getEntity(), $context->getCrud()->getActionsConfig());
311311

@@ -495,7 +495,7 @@ public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityD
495495
public function renderFilters(AdminContext $context): KeyValueStore
496496
{
497497
$fields = FieldCollection::new($this->configureFields(Crud::PAGE_INDEX));
498-
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields);
498+
$this->container->get(EntityFactory::class)->processFields($context->getEntity(), $fields, Crud::PAGE_INDEX);
499499
$filters = $this->container->get(FilterFactory::class)->create($context->getCrud()->getFiltersConfig(), $context->getEntity()->getFields(), $context->getEntity());
500500

501501
/** @var FormInterface&FiltersFormType $filtersForm */

src/Factory/EntityFactory.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,31 @@ public function __construct(FieldFactory $fieldFactory, ActionFactory $actionFac
3838
$this->eventDispatcher = $eventDispatcher;
3939
}
4040

41-
public function processFields(EntityDto $entityDto, FieldCollection $fields): void
41+
public function processFields(EntityDto $entityDto, FieldCollection $fields, ?string $pageName = null): void
4242
{
43-
$this->fieldFactory->processFields($entityDto, $fields);
43+
if (null === $pageName) {
44+
trigger_deprecation(
45+
'easycorp/easyadmin-bundle',
46+
'4.27.0',
47+
'Argument "$pageName" is missing. Omitting it will cause an error in 5.0.0.',
48+
);
49+
}
50+
51+
$this->fieldFactory->processFields($entityDto, $fields, $pageName);
4452
}
4553

46-
public function processFieldsForAll(EntityCollection $entities, FieldCollection $fields): void
54+
public function processFieldsForAll(EntityCollection $entities, FieldCollection $fields, ?string $pageName = null): void
4755
{
56+
if (null === $pageName) {
57+
trigger_deprecation(
58+
'easycorp/easyadmin-bundle',
59+
'4.27.0',
60+
'Argument "$pageName" is missing. Omitting it will cause an error in 5.0.0.',
61+
);
62+
}
63+
4864
foreach ($entities as $entity) {
49-
$this->processFields($entity, clone $fields);
65+
$this->processFields($entity, clone $fields, $pageName);
5066
$entities->set($entity);
5167
}
5268
}

src/Factory/FieldFactory.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,20 @@ public function __construct(
7171
{
7272
}
7373

74-
public function processFields(EntityDto $entityDto, FieldCollection $fields): void
74+
public function processFields(EntityDto $entityDto, FieldCollection $fields, ?string $currentPage = null): void
7575
{
7676
$this->replaceGenericFieldsWithSpecificFields($fields, $entityDto);
7777

7878
$context = $this->adminContextProvider->getContext();
79-
$currentPage = $context->getCrud()->getCurrentPage();
79+
80+
if (null === $currentPage) {
81+
trigger_deprecation(
82+
'easycorp/easyadmin-bundle',
83+
'4.27.0',
84+
'Argument "$currentPage" is missing. Omitting it will cause an error in 5.0.0.',
85+
);
86+
$currentPage = $context->getCrud()->getCurrentPage();
87+
}
8088

8189
$isDetailOrIndex = \in_array($currentPage, [Crud::PAGE_INDEX, Crud::PAGE_DETAIL], true);
8290
foreach ($fields as $fieldDto) {
@@ -94,14 +102,6 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo
94102
continue;
95103
}
96104

97-
// when creating new entities with "useEntryCrudForm" on an edit page we must
98-
// explicitly check for the "new" page because $currentPage will be "edit"
99-
if ($isDetailOrIndex && (null === $entityDto->getInstance()) && !$fieldDto->isDisplayedOn(Crud::PAGE_NEW)) {
100-
$fields->unset($fieldDto);
101-
102-
continue;
103-
}
104-
105105
foreach ($this->fieldConfigurators as $configurator) {
106106
if (!$configurator->supports($fieldDto, $entityDto)) {
107107
continue;

src/Field/Configurator/AssociationConfigurator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,24 @@ private function configureCrudForm(FieldDto $field, EntityDto $entityDto, string
304304
if (null === $associatedEntity) {
305305
$targetCrudControllerAction = Action::NEW;
306306
$targetCrudControllerPageName = $field->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_NEW_PAGE_NAME) ?? Crud::PAGE_NEW;
307+
$crudPageName = Crud::PAGE_NEW;
307308
} else {
308309
$targetCrudControllerAction = Action::EDIT;
309310
$targetCrudControllerPageName = $field->getCustomOption(AssociationField::OPTION_EMBEDDED_CRUD_FORM_EDIT_PAGE_NAME) ?? Crud::PAGE_EDIT;
311+
$crudPageName = Crud::PAGE_EDIT;
310312
}
311313

312314
$field->setFormTypeOption(
313315
'entityDto',
314-
$this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, $targetCrudControllerAction, $targetCrudControllerPageName),
316+
$this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, $targetCrudControllerAction, $targetCrudControllerPageName, $crudPageName),
315317
);
316318
}
317319

318320
/**
319321
* @param class-string $entityFqcn
320322
* @param class-string $crudControllerFqcn
321323
*/
322-
private function createEntityDto(string $entityFqcn, string $crudControllerFqcn, string $crudControllerAction, string $crudControllerPageName): EntityDto
324+
private function createEntityDto(string $entityFqcn, string $crudControllerFqcn, string $crudControllerAction, string $crudControllerPageName, string $crudPageName): EntityDto
323325
{
324326
$entityDto = $this->entityFactory->create($entityFqcn);
325327

@@ -331,7 +333,7 @@ private function createEntityDto(string $entityFqcn, string $crudControllerFqcn,
331333

332334
$fields = $crudController->configureFields($crudControllerPageName);
333335

334-
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields));
336+
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
335337

336338
return $entityDto;
337339
}

src/Field/Configurator/CollectionConfigurator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
101101
}
102102

103103
$crudEditPageName = $field->getCustomOption(CollectionField::OPTION_ENTRY_CRUD_EDIT_PAGE_NAME) ?? Crud::PAGE_EDIT;
104-
$editEntityDto = $this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, Action::EDIT, $crudEditPageName);
104+
$editEntityDto = $this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, Action::EDIT, $crudEditPageName, Crud::PAGE_EDIT);
105105
$field->setFormTypeOption('entry_options.entityDto', $editEntityDto);
106106

107107
$crudNewPageName = $field->getCustomOption(CollectionField::OPTION_ENTRY_CRUD_NEW_PAGE_NAME) ?? Crud::PAGE_NEW;
108-
$newEntityDto = $this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, Action::NEW, $crudNewPageName);
108+
$newEntityDto = $this->createEntityDto($targetEntityFqcn, $targetCrudControllerFqcn, Action::NEW, $crudNewPageName, Crud::PAGE_NEW);
109109

110110
try {
111111
$field->setFormTypeOption('prototype_options.entityDto', $newEntityDto);
@@ -157,7 +157,7 @@ private function countNumElements(mixed $collection): int
157157
* @param class-string $targetEntityFqcn
158158
* @param class-string $targetCrudControllerFqcn
159159
*/
160-
private function createEntityDto(string $targetEntityFqcn, string $targetCrudControllerFqcn, string $crudAction, string $pageName): EntityDto
160+
private function createEntityDto(string $targetEntityFqcn, string $targetCrudControllerFqcn, string $crudAction, string $crudControllerPageName, string $crudPageName): EntityDto
161161
{
162162
$entityDto = $this->entityFactory->create($targetEntityFqcn);
163163

@@ -167,9 +167,9 @@ private function createEntityDto(string $targetEntityFqcn, string $targetCrudCon
167167
$this->requestStack->getMainRequest()
168168
);
169169

170-
$fields = $crudController->configureFields($pageName);
170+
$fields = $crudController->configureFields($crudControllerPageName);
171171

172-
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields));
172+
$this->entityFactory->processFields($entityDto, FieldCollection::new($fields), $crudPageName);
173173

174174
return $entityDto;
175175
}

0 commit comments

Comments
 (0)