简体   繁体   中英

SONATA_TYPE_COLLECTION choice to display not all fields

I am using sonata admin Bundle and other Bundle from 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.

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_user
- créer le
- 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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