How to apply some code to each entity being displayed in Admin's list view?
For example, if I have a TagManager and need to load tags for each entity being displayed, how do I do that? Is there a method to override in entity's Admin or can I bind to some list form event? I could not find a place to do that.
I don't wan't to bind to entity's onLoad event.
EDIT: In your entityAdminController :
public function listAction()
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
foreach($datagrid->getResults() as $object)
{
//do what you want with $object
}
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid
));
}
Related
How can i access the id of a sonata admin entity. suppose i have an entity EndPoint which have a function getProject(), how can i get the id of that project. I tried the code bellow but this gave me the following error: Attempted to call an undefined method named "getProject" of class "ContainerAgbGotv\srcApp_KernelDevDebugContainer".
class EndPointAdmin extends AbstractAdmin{
protected function configureFormFields(FormMapper $form)
{ $container = $this->getConfigurationPool()->getContainer();
$em = $container->getProject();
$array = [];
foreach ($em as $ems) {
if (!empty($ems->getEnv())) {
$array[$ems->getEnv()] = $ems->getEnv();
}}
$result = array_unique($array);
$form
->add('name',ChoiceType::class, [
'choices'=> $result,
'placeholder' => 'Choose an option',
'required' => false
])
->add('ipAdress',TextType::class)
->add('port',IntegerType::class)
;
}
thanks for helping.
In an admin you will have access to
$this->getSubject()
which will return you the current entity for that admin. It's worth noting configureFormFields is used for both creating and editing an entity so getSubject() may return null.
I'm developing the Admin Panel for my website, and one part of it is managing users. I'm using FOSUserBundle for those tasks.
I'm using custom template (AdminLTE) for forms, and I cannot add a dropdown to select roles for user when I add a new one.
In UserType.php
$builder->add('roles', 'choice', array(
'label' => 'User Role',
'choices' => $this->roleHelper->getRolesForForm()
));
In WCB\SaleBundle\Helper\RoleHelper.php
...
public function getRolesForForm()
{
$roleList = self::flatArray($this->rolesHierarchy);
$roles = array();
foreach ($roleList as $roleId => $roleName) {
if ($roleId == 'ROLE_ADMIN') {
$roles[$roleName] = 'Admin';
}
if ($roleId == 'ROLE_USER') {
$roles[$roleName] = 'User';
}
}
return $roles;
}
...
Above getRolesForForm() function will return this array, which is correct format for using with Symfony's choice field type:
Array
(
[ROLE_ADMIN] => Admin
[ROLE_USER] => User
)
And the form's not working anymore, with this exception:
The value of type "array" cannot be converted to a valid array key.
When I add 'multiple' = true to form builder, it works, but it's not a dropdown. It's HTML select box which allow multiple selection.
$builder->add('roles', 'choice', array(
'label' => 'User Role',
'choices' => $this->roleHelper->getRolesForForm(),
'multiple' => true
));
I think, for role selection, it should be a dropdown, not a multiple-selection box. How can I achieve this? Anything wrong with my code? Thank you :)
I think it should be a multiple-selection box indeed.
Take into account that a user can (and usually will) have several roles. In your case, a user who has ROLE_ADMIN also has ROLE_USER (ROLE_ADMIN "includes" ROLE_USER if you are using FOSUserBundle).
Not really sure if this may be the problem since I don't have your full code and I am not sure what self::flatArray($this->rolesHierarchy) is returning but please notice that you are using $roleName, not $roleId as your array key. If $roleName is not a string then you will get this problem.
I faced same issue lately and this is my work around (I created DataTransformer):
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
$builder->get('roles')->addModelTransformer(new RolesTransformer());
}
}
And DataTransformer class:
use AppBundle\Entity\User;
use Symfony\Component\Form\DataTransformerInterface;
class RolesTransformer implements DataTransformerInterface
{
public function transform($roles)
{
foreach ($roles as $role) {
if ($role !== User::ROLE_DEFAULT) {
return $role;
}
}
return $roles[0];
}
public function reverseTransform($string)
{
return [
$string, User::ROLE_DEFAULT
];
}
}
The role attribute from UserModel of FOSUserBundle expected an array. If you use a select normal, they don't return an array.
I have added the following code in my Admin class,
class ProductPriceAdmin extends Admin
{
protected function configureRoutes(RouteCollection $collection)
{
parent::configureRoutes($collection);
$collection->add('price'); //I want to add a "id" as route parameter
}
}
Here price is my custom function which I have declared in the controller as below,
class ProductPriceController extends Controller
{
public function priceAction($id) //I want to use this variable
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid,
'csrf_token' => $this->getCsrfToken('sonata.batch'),
));
}
}
How to add route parameter when adding dynamic routing ?
Thanks,
Faisal Nasir
Try this:
$collection->add('edit_price', 'price/{id}');
also you can view in Symfony\Component\Routing\Route\RouteCollection method add().
I have created a custom list view in sonata admin to display a calendar.
I'm trying to add events to the calendar dynamically, but I'm getting an error with the CSRF token being invalid.
I have the following code:
public function listAction()
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
$datagrid = $this->admin->getDatagrid();
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
$em = $this->getDoctrine()->getManager();
$events = $em->getRepository('BMCrmBundle:Event')->findAll();
$event = new Event();
$formEvent = $this->createForm(new EventType(), $event );
return $this->render($this->admin->getTemplate('list'), array(
'action' => 'list',
'form' => $formView,
'datagrid' => $datagrid,
'csrf_token' => $this->getCsrfToken('sonata.batch'),
'events' => $events,
'formEvent' => $formEvent->createView()
));
}
view
var url = "{{ path('create_event', { _sonata_admin: 'bm.crm.admin.event'} ) }}";
$.post(url, form.serialize(), function(data) {
alert(data);
});
This always returns that the CSRF token is invalid
Any ideas?
Check if in your view, you have the following line:
{{ form_rest(form) }}
because I believe that you are rendering form fields one by one and not the whole form at once and forgot to render the rest of the form, which contains the CSRF token.
How to limit the number of embedded form with the type "sonata_type_collection" ?
$formMapper->add('phones', 'sonata_type_collection',
array(
'required' => true,
'by_reference' => false,
'label' => 'Phones',
),
array(
'edit' => 'inline',
'inline' => 'table'
)
I would like limit to last five phones, I found only this solution for now, limit the display in the template twig "edit_orm_one_to_many", but i don't like that.
I found a solution by rewriting the edit action in the controller,
such in the documentation sonataAdminBundle I created my admin controller class:
class ContactAdminController extends Controller
{
public function editAction($id = null)
{
// the key used to lookup the template
$templateKey = 'edit';
$em = $this->getDoctrine()->getEntityManager();
$id = $this->get('request')->get($this->admin->getIdParameter());
// $object = $this->admin->getObject($id);
// My custom method to reduce the queries number
$object = $em->getRepository('GestionBundle:Contact')->findOneAllJoin($id);
if (!$object)
{
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}
if (false === $this->admin->isGranted('EDIT', $object))
{
throw new AccessDeniedException();
}
$this->admin->setSubject($object);
/** #var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);
// Trick is here ###############################################
// Method to find the X last phones for this Contact (x = limit)
// And set the data in form
$phones = $em->getRepository('GestionBundle:Phone')->findLastByContact($object, 5);
$form['phones']->setData($phones);
// #############################################################
if ($this->get('request')->getMethod() == 'POST')
{
$form->bindRequest($this->get('request'));
$isFormValid = $form->isValid();
// persist if the form was valid and if in preview mode the preview was approved
if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved()))
{
$this->admin->update($object);
$this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success');
if ($this->isXmlHttpRequest())
{
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}
// redirect to edit mode
return $this->redirectTo($object);
}
// show an error message if the form failed validation
if (!$isFormValid)
{
$this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error');
}
elseif ($this->isPreviewRequested())
{
// enable the preview template if the form was valid and preview was requested
$templateKey = 'preview';
}
}
$view = $form->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
return $this->render($this->admin->getTemplate($templateKey), array(
'action' => 'edit',
'form' => $view,
'object' => $object,
));
}
}