-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-choices-based-on-role.php
27 lines (24 loc) · 1.01 KB
/
get-choices-based-on-role.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
// This can be done by changing the choices via setFormTypeOptions
// on the Field in configureFields. I've put an example below that
// checks if user roles is not ROLE_ADMIN if not it will only show
// the allowed choices, this seems to work just the way I want it to.
// Took a bit of guesswork and digging since this wasn't clearly explained in the docs.
public function configureFields(string $pageName): iterable
{
$fields = [];
if (array_search('ROLE_ADMIN', $this->getUser()->getRoles()) === false) {
/** @var User|null $user */
$user = $this->entityManager->getRepository(User::class)->findOneBy([
'username' => $this->getUser()->getUsername()
]);
if ($user) {
$fields[] = AssociationField::new('suppliers')->onlyOnForms()->setFormTypeOptions([
"choices" => $user->getEnabledSuppliers()->toArray()
]);
}
} else {
$fields[] = AssociationField::new('suppliers')->onlyOnForms();
}
return $fields;
}