Symfony 4:在 Sonata 的 configureFormFields 方法中使用现有的表单类型 class

Symfony 4: Use existing form type class in Sonata's configureFormFields method

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()
        ;
    }
}

但是,如果您只想让 Sonata 按原样使用该确切类型,那么这会感觉很笨拙。

因此我想知道是否有一个 shorthand 告诉 sonata 只需按原样使用整个表单类型。

类似于:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->mapForm((new PostType()));
}

也许使用 inherit_data 的较短版本可能就是您要找的。

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

发件人:How to inherit FormType in Sonata Admin?

Post为例

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,
                ]
            );
    }
}

使用 PostAdmin(和 PostType)映射 1:m 关系中的子记录也有效,但 inline 选项和 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
                ]
            );
    }
}