33namespace EasyCorp \Bundle \EasyAdminBundle \Factory ;
44
55use Doctrine \DBAL \Types \Types ;
6+ use EasyCorp \Bundle \EasyAdminBundle \Collection \EntityCollection ;
67use EasyCorp \Bundle \EasyAdminBundle \Collection \FieldCollection ;
78use EasyCorp \Bundle \EasyAdminBundle \Config \Crud ;
89use EasyCorp \Bundle \EasyAdminBundle \Contracts \Field \FieldConfiguratorInterface ;
@@ -71,8 +72,15 @@ public function __construct(
7172 {
7273 }
7374
74- public function processFields (EntityDto $ entityDto , FieldCollection $ fields ): void
75+ public function processFields (EntityCollection $ entities , FieldCollection $ fields ): void
7576 {
77+ if (0 === $ entities ->count ()) {
78+ return ;
79+ }
80+
81+ // something that can be and should be done only once for all entities
82+ $ entityDto = clone $ entities ->first ();
83+ $ entityDto ->setInstance (null );
7684 $ this ->preProcessFields ($ fields , $ entityDto );
7785
7886 $ context = $ this ->adminContextProvider ->getContext ();
@@ -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 ((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 ;
@@ -123,12 +123,42 @@ public function processFields(EntityDto $entityDto, FieldCollection $fields): vo
123123
124124 $ fields ->set ($ fieldDto );
125125 }
126+ unset($ entityDto , $ fieldDto );
126127
127128 if (!$ fields ->isEmpty ()) {
128129 $ this ->fieldLayoutFactory ->createLayout ($ fields , $ this ->adminContextProvider ->getContext ()?->getCrud()?->getCurrentPage() ?? Crud::PAGE_INDEX );
129130 }
130131
131- $ entityDto ->setFields ($ fields );
132+ $ originalFields = $ fields ;
133+ foreach ($ entities as $ entityDto ) {
134+ $ fields = clone $ originalFields ;
135+ foreach ($ fields as $ fieldDto ) {
136+ if (false === $ this ->authorizationChecker ->isGranted (Permission::EA_VIEW_FIELD , $ fieldDto )) {
137+ $ fieldDto ->markAsInaccessible ();
138+
139+ continue ;
140+ }
141+
142+ // when creating new entities with "useEntryCrudForm" on an edit page we must
143+ // explicitly check for the "new" page because $currentPage will be "edit"
144+ if ((null === $ entityDto ->getInstance ()) && !$ fieldDto ->isDisplayedOn (Crud::PAGE_NEW )) {
145+ $ fieldDto ->markAsInaccessible ();
146+
147+ continue ;
148+ }
149+
150+ foreach ($ this ->fieldConfigurators as $ configurator ) {
151+ if (!$ configurator ->supports ($ fieldDto , $ entityDto )) {
152+ continue ;
153+ }
154+
155+ // @phpstan-ignore-next-line argument.type
156+ $ configurator ->configure ($ fieldDto , $ entityDto , $ context );
157+ }
158+ }
159+
160+ $ entityDto ->setFields ($ fields );
161+ }
132162 }
133163
134164 private function preProcessFields (FieldCollection $ fields , EntityDto $ entityDto ): void
0 commit comments