简体   繁体   English

SONATA_TYPE_COLLECTION选项以不显示所有字段

[英]SONATA_TYPE_COLLECTION choice to display not all fields

I am using sonata admin Bundle and other Bundle from Sonata. 我正在使用Sonata admin捆绑包和Sonata的其他捆绑包。

I am trying to display some messages. 我正在尝试显示一些消息。 It's working good but I don't want to display all the field when I use the type SONATA_TYPE_COLLECTION. 它工作正常,但是当我使用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',
            ))

And a photo to show my result actually : 还有一张照片可以显示我的结果: 在此处输入图片说明

I would like to display only : 我只想显示:
- delete -删除
- id - ID
- id_user -id_user
- créer le -créerle
- message but whithout the WYSIWYG -消息,但没有所见即所得

Has someone had the same issue. 是否有人遇到过同样的问题。 Thank you for your advices. 感谢您的建议。

You can use the getRoot in the child admin for example: 您可以在子管理员中使用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
        ;
    }
}

What I've done to solve this problem is passing a link-parameter to the add button: 解决此问题的方法是将链接参数传递给添加按钮:

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

So, from the child admin, you can retrieve these arguments from the request and create the admin as you want to for each case: 因此,您可以通过子管理员从请求中检索以下参数,并根据需要为每种情况创建管理员:

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

If what you want to change it's the list of fields already added (not the new ones), you can use: 如果要更改的是已添加的字段列表(不是新字段),则可以使用:

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

I use this construction often to view differend fields in differend places: 我经常使用这种结构来查看不同地方的不同领域:

/**
 * @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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM