Piotr Galas
Piotr Galas

Reputation: 4776

Add form option in sonata admin class

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?

Upvotes: 3

Views: 2518

Answers (1)

Hydde87
Hydde87

Reputation: 719

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.

Upvotes: 1

Related Questions