|
12 | 12 | use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
13 | 13 | use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; |
14 | 14 | use EasyCorp\Bundle\EasyAdminBundle\Config\Asset; |
15 | | -use EasyCorp\Bundle\EasyAdminBundle\Config\Assets; |
16 | 15 | use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
17 | 16 | use EasyCorp\Bundle\EasyAdminBundle\Config\Filters; |
18 | 17 | use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore; |
@@ -134,20 +133,22 @@ public function configureFields(string $pageName): iterable |
134 | 133 | yield FormField::addFieldset('Organizer information') |
135 | 134 | ->setLabel(new TranslatableMessage('admin.event.organizer.headline')); |
136 | 135 |
|
137 | | - if ($this->isGranted(UserRoles::ROLE_EDITOR->value)) { |
138 | | - yield AssociationField::new('organization') |
139 | | - ->setLabel(new TranslatableMessage('admin.event.edited.organization')); |
140 | | - } else { |
141 | | - yield AssociationField::new('organization') |
142 | | - ->setLabel(new TranslatableMessage('admin.event.edited.organization')) |
143 | | - ->setQueryBuilder( |
144 | | - fn (QueryBuilder $queryBuilder) => $queryBuilder |
145 | | - ->select('o') |
146 | | - ->from(Organization::class, 'o') |
147 | | - ->where(':user MEMBER OF o.users') |
148 | | - ->setParameter('user', $this->getUser()) |
149 | | - ); |
| 136 | + $organization = AssociationField::new('organization') |
| 137 | + ->setLabel(new TranslatableMessage('admin.event.edited.organization')) |
| 138 | + // We assume at least one organization exist for non-editor users |
| 139 | + // (cf. editor stuff below). |
| 140 | + ->setRequired(true) |
| 141 | + ->setFormTypeOption('placeholder', new TranslatableMessage('admin.event.organizer.placeholder')); |
| 142 | + // Limit organization choices for non-editors to the organizations the user is a member of. |
| 143 | + if (!$this->isGranted(UserRoles::ROLE_EDITOR->value)) { |
| 144 | + $userOrganizations = $this->getUser()->getOrganizations(); |
| 145 | + $organization |
| 146 | + ->setFormTypeOption('choices', $userOrganizations) |
| 147 | + // Make sure that the user is not forced to make a choice if none exists. |
| 148 | + ->setRequired($userOrganizations->count() > 0); |
150 | 149 | } |
| 150 | + yield $organization; |
| 151 | + |
151 | 152 | yield AssociationField::new('partners') |
152 | 153 | ->setLabel(new TranslatableMessage('admin.event.edited.partners')) |
153 | 154 | ->hideOnDetail(); |
|
0 commit comments