I have a many many to many relation in Sonata (two one to many relations to be exact), Brand and Retailer. In the Brand admin Edit page, I want to display all the retailers as a list (so just a read only version), instead of having the normal edit (at the moment, on this brand edit page, I can manage the relationship between this brand and retailers - add a new one, delete an existing one).
I tried to explore two routes so far:
My issue is, with both options, I didn't manage to get to a solution So here is what I have done:
1 - Loading a custom edit twig:
services:
xx_brand.admin.brand_brand:
calls:
- [ setTemplate, [edit, xxBrandBundle:Admin:base_edit.html.twig]]
On this case, base_edit is an exact copy of the sonata base_edit
, but it loads my custom base_edit_form
:
{% use 'xxBrandBundle:Admin:base_edit_form.html.twig' with form as parentForm %}
From here I can exclude the default rendering of the retailers
, but can't find a way to then render it as I want, as I am not sure how the retailers
entity is managed here:
{% if admin.formfielddescriptions[field_name] is defined and field_name != 'retailers' %}
{{ form_row(form[field_name])}}
{% else %}
<ul>
<li>retailer1</li>
<li>retailer2</li>
</ul>
{% endif %}
2 - For the approach of a custom field type, I tried to follow the documentation
Bundle/Form/Type/ListType.php
/BrandBundle/Resources/views/form/list.html.twig
Using the ListType
in configureFormFields
:
use XX\BrandBundle\Form\Type\ListType;
...
->add('retailers', 'ListType');
But I then get an error XX\BrandBundle\Form\Type\ListType
So basically, because I couldn't get it to work, are any of these two options good to solve my issue ? If so, could anyone please advice on what I am missing there ?? Any help will be very much appreciated :)
You can use sonata_type_model_list
: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html#example
Like so:
class BrandAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('retailer', 'sonata_type_model_list', array(
'btn_add' => false,
'btn_delete' => false,
));
}
}