Skip to content

Commit 3d057fe

Browse files
committed
Skip CollectionField entry-type setup on INDEX/DETAIL pages
CollectionConfigurator::configureEntryType() instantiates the target CRUD controller and calls configureFields(PAGE_EDIT) on it (or PAGE_NEW) so it can build entry_type/prototype options. These options are only consumed when the field is rendered as a form (i.e. on NEW/EDIT pages); on INDEX/DETAIL the field falls back to formatCollection() which only reads the value and a custom option. Running configureFields(PAGE_EDIT) on the target controller while displaying the parent entity's DETAIL page does not just waste work: it also runs the user's configureFields() with no real entity instance available, which makes controllers that legitimately read $context->getEntity()->getInstance() inside configureFields() crash on a NULL entity (#7460). Bail out early when the current action is neither EDIT nor NEW so the form setup only happens on the pages that actually need it.
1 parent 0846e65 commit 3d057fe

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

src/Field/Configurator/CollectionConfigurator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ private function countNumElements(mixed $collection): int
125125

126126
private function configureEntryType(FieldDto $fieldDto, EntityDto $entityDto, AdminContext $context): void
127127
{
128+
// entry_type and prototype options are only consumed when the field is
129+
// rendered as a form (NEW/EDIT). On INDEX/DETAIL the field is rendered
130+
// through formatCollection(), so the entry-type setup is wasted work
131+
// and, more importantly, calling configureFields(PAGE_EDIT) on the
132+
// target CRUD controller can run user code that expects a real entity
133+
// instance (it has none here): see #7460.
134+
if (!\in_array($context->getCrud()?->getCurrentPage(), [Crud::PAGE_EDIT, Crud::PAGE_NEW], true)) {
135+
return;
136+
}
137+
128138
if (true === $fieldDto->getCustomOption(CollectionField::OPTION_ENTRY_USES_CRUD_FORM)) {
129139
if (!$entityDto->getClassMetadata()->hasAssociation($fieldDto->getProperty())) {
130140
throw new \RuntimeException(sprintf('The "%s" collection field of "%s" cannot use the "useEntryCrudForm()" method because it is not a Doctrine association.', $fieldDto->getProperty(), $context->getCrud()?->getControllerFqcn()));

0 commit comments

Comments
 (0)