Sonata Admin: how to change the final view of a field inside a form

280 Views Asked by At

let's say I have a Product with ManyToOne relation with Category entity. in the configureFormFields(FormMapper $form) of ProductAdmin we will do the following:

$formMapper->add('category');

And Sonata will render a select field.
But what I want to do is to customize the final view of the select, because I want to add some specific attributes to the option tags of the select (not the select itself).
I did this before in symfony by overriding the finishView() function inside the FormType like so:

public function finishView(FormView $view, FormInterface $form, array $options)
{
    $field = 'category';
    $choices = $view->children[$field]->vars['choices'];
    foreach ($choices as $choice) {
        // I can add any attribute to the options like so
        $choice->attr['new-attribute'] = 'attribute_value';
    }
    $view->children[$field]->vars['choices'] = $choices;
}

But I can't find such function in AbstractAdmin provided by Sonata !
Is there a way to achieve this goal ?

0

There are 0 best solutions below