3

How can I add custom option to formmMapper in sonata admin class?

I have form related to entity in admin class. For some reason I want to add my own options to one of the field

    $formMapper
        ->with('tab.dimension')
            ->add('dimension', 'collection', array(
                'type' => 'dimension_product',

                'allow_add'    => true,
                'allow_delete' => true,
                'required' => false,
                'my_custom_options' => false,
            ))
        ->end();

Unfortunatly it is not possible in this way,because this options isn't defined in resolver. But I have no access to resolver in "normal way". Sonata defined form builder in two methods:

public function getFormBuilder()
{
    $this->formOptions['data_class'] = $this->getClass();

    $formBuilder = $this->getFormContractor()->getFormBuilder(
        $this->getUniqid(),
        $this->formOptions
    );

    $this->defineFormBuilder($formBuilder);

    return $formBuilder;
}


public function defineFormBuilder(FormBuilder $formBuilder)
{
    $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this);

    $this->configureFormFields($mapper);

    foreach ($this->getExtensions() as $extension) {
        $extension->configureFormFields($mapper);
    }

    $this->attachInlineValidator();
}

Allowed options are defined in this object:

 new FormMapper($this->getFormContractor(), $formBuilder, $this); 

Could somebody give me advice how can i add my own option?

Piotr Galas
  • 4,518
  • 2
  • 21
  • 30

1 Answers1

1

Little bit late to the party, but it sort of depends on what you want to do with this option.

If you need to add a real custom form option, it is not very much different from working directly with Symfony forms. You can add extra options and functionality to a given form type using a form extension . You could even add functionality to the sonata form types this way.

If you simply need to pass an option from one Admin to a child Admin (which I think you might want to do), you can use the field description options rather than the actual form options:

$formMapper
        ->with('tab.dimension')
            ->add('dimension', 'collection', array(
                'type' => 'dimension_product',

                'allow_add'    => true,
                'allow_delete' => true,
                'required' => false,
            ), array(
                'my_custom_options' => false,
            ))
->end();

Now in your child Admin you can retrieve these options using

$this->getParentFieldDescription()->getOptions();

To be used to configure your child Admin.

Hydde87
  • 689
  • 6
  • 6
  • Thank you for answer, but i can't check out is it solve my problem. I didn't use sonata for about one year. So I can't assign your answer as correct. – Piotr Galas Aug 03 '16 at 05:42