I have a user edit form where I would like to administer the roles assigned to a user.
Currently I have a multi-select list, but I have no way of populating it with the role hierarchy defined in security.yml.
Is there some way that I get this information to the form builder in the FormType class?
$builder->add('roles', 'choice', array(
'required' => true,
'multiple' => true,
'choices' => array(),
));
Looking around I found that I can get the roles from the container in a controller with:
$roles = $this->container->getParameter('security.role_hierarchy.roles');
I have also discovered that I could potentially set this as a dependency to be injected on the FormType class in services.xml:
<parameters>
<parameter key="security.role_heirarchy.roles">ROLE_GUEST</parameter>
</parameters>
<services>
<service id="base.user.form.type.user_form" class="Base\UserBundle\Form\UserType" public="false">
<tag name="form.type" />
<call method="setRoles">
<argument>%security.role_heirarchy.roles%</argument>
</call>
</service>
</services>
This however does not work and does not seem to ever call the setRoles method.
So how can I get this to work?
In your controller
In UserType :