SONATA_TYPE_COLLECTION 选择不显示所有字段

SONATA_TYPE_COLLECTION choice to display not all fields

我正在使用 Sonata 的 sonata admin Bundle 和其他 Bundle。

我正在尝试显示一些消息。它运行良好,但我不想在使用 SONATA_TYPE_COLLECTION.

类型时显示所有字段
protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('messages', 'sonata_type_collection', array(
                'label' => 'Liste des messages',
                'required' => false,
                'type_options' => array(
                    // Prevents the "Delete" option from being displayed
                    'delete' => true,
//                    'idPackage' => false,
//                    'delete_options' => array(
//                        // You may otherwise choose to put the field but hide it
//                        'type'         => 'hidden',
//                        // In that case, you need to fill in the options as well
//                        'type_options' => array(
//                            'mapped'   => false,
//                            'required' => false,
//                        )
//                    )
                )
            ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable' => 'position',
            ))

还有一张照片来展示我的实际结果:

我只想显示:
- 删除
- 编号
- id_user
- 克雷尔
- 消息但没有所见即所得

有人遇到过同样的问题吗?谢谢你的建议。

我为解决此问题所做的工作是将 link 参数传递给添加按钮:

$formMapper->add('messages', 'sonata_type_collection', array('label' => 'whatever'), array( 'edit' => 'inline', 'inline' => 'table', 'link_parameters' => array('owner-id' => $this->getSubject()->getId())) );

因此,对于子管理员,您可以从请求中检索这些参数,并根据需要为每种情况创建管理员:

if ($ownerId = $this->getRequest()->query->get('owner-id')) { //create your admin differently }

如果您要更改的是已添加的字段列表(不是新字段),您可以使用:

$parent = $this->getParentFieldDescription(); if ($parent && ( ($entity = $parent->getAdmin()->getSubject()) instanceof MyInterface ) ) { //do sth here }

您可以在子管理员中使用 getRoot 例如:

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    if ($this->getRoot()->getClass() == 'AppBundle\Entity\MyAdmin') {
        $formMapper
            ->add('somefield') //this field will be only visible in the child admin form, the parent using sonata_type_collection will have a different class
        ;
    }
}

我经常使用这个结构来查看不同地方的不同领域:

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    // Find out which admin we should render for
    $admin = $this->getRequestParameter('code');
    if (empty($admin)) {
        $admin = $this->getRequestParameter('_sonata_admin');
    }

    // Show different fields based on the action you're in
    switch ($admin) {
        case 'app.admin.location': // this is the name in your yml file
            $this->configureFormFieldsForLocationAdmin($formMapper);
            break;
        default:
            $this->configureFormFieldsDefault($formMapper);
    }
}