1

I know I can pull individual form elements from the type's builder into the form mapper as described in the documentation:

You can add Symfony FormBuilderInterface instances to the FormMapper. This allows you to re-use a model form type. When adding a field using a FormBuilderInterface, the type is guessed.

Given you have a PostType like this:

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\AbstractType;

class PostType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('author', EntityType::class, [
                'class' => User::class
            ])
            ->add('title', TextType::class)
            ->add('body', TextareaType::class)
        ;
    }
}

you can reuse it like this:

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use App\Form\PostType;

class Post extend AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $builder = $formMapper->getFormBuilder()->getFormFactory()->createBuilder(PostType::class);

        $formMapper
            ->with('Post')
                ->add($builder->get('title'))
                ->add($builder->get('body'))
            ->end()
            ->with('Author')
                ->add($builder->get('author'))
            ->end()
        ;
    }
}

However, this feels clunky if all you want is for Sonata to use that exact type as is.

I was therefore wondering if there is a shorthand that tells sonata to simply use the entire form type as is.

Something like:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->mapForm((new PostType()));
}
2
  • I'm not sure to 100% understand what you want, do you mean, you have a form FooType and want to use in in another form ?
    – Cid
    Feb 6, 2020 at 8:34
  • Sort of - Sonata Admin lets you define what form fields you want to provide to a user of the CMS when adding / editing a given Entity. This is done via configureFormFields() and carries a certain amount of duplication in that you need to re-list all the fields that you have already defined in your FormType that's used for the public facing form. If this is a longer more complex form this leads to a fair amount of duplicate code that I am looking to avoid. It feels like you should be able to say "Hey Sonata, use this FormType as is!" Feb 6, 2020 at 8:55

1 Answer 1

1

Maybe the shorter version using inherit_data might be what you are looking for.

https://symfony.com/doc/current/form/inherit_data_option.html

From: How to inherit FormType in Sonata Admin?

With the Post example

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use App\Form\PostType;

class PostAdmin extend AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add(
                'post',  // name does not seem to matter
                PostType::class, 
                [
                    'inherit_data' => true,
                ]
            );
    }
}

Using PostAdmin (and PostType) to map a child record in a 1:m relationship works too, but has problems with inline options and the table view does not seem to render it in separate columns as expected.

// Thread 1:m Post

/**
 * @ORM\Entity
    ...
 */
class Thread
{
    protected $title = '';

    /**
     * @var Post[]|ArrayCollection
     *
     * @ORM\OneToMany(...)
     */
    protected posts;
}
class Post extend AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->
            ->with('Thread')
            ->add('title')
            ->add(
                'posts',
                'sonata_type_collection',
                [
                    'label'        => 'Posts',
                    'required'     => false,
                    'by_reference' => false,
                ],
                [
                    'edit'     => 'inline', // does not seem to work
                    'inline'   => 'table',  // takes up one column in table
                    'multiple' => true,     // one row per entry
                ]
            );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.