简体   繁体   中英

How to add link to show action of the relation entity on relation field in SonataAdminBundle

Im making admin panel in SonataAdminBundle. In User show action i have field companies which return array of companies assigned to the user. It is a OneToMany relation. UserCompany has user_id and company_id. I want to create link on each returned company name, which points to it's entity show action. This is code from configureShowFields() function in UserAdmin class:

                ->with('Assigned organizers',['class' => 'col-md-6'])
                    ->add('companies', null, [
                        'label' => 'Organizers',
                    ])
                ->end()

I managed to create a link on a string field pointing to show action of an entity, but the id property is taken from the current entity view:

            ->with('Address', ['class' => 'col-md-6'])
                ->add('userProfile.locality', 'url', [
                    'route' => [
                        'name' => 'admin_app_employee_show',
                        'identifier_parameter_name' => 'id'
                    ],
                    'label' => 'Localiy',
                ])

What's more Sonata Admin create links on related fields, when the relation is direct, for example: Company has many Employee. Then in Company show action on employees field I see array with links already heading to edit action of Employee entty.

Maybe there is a possibility to override template for this field, but it seems unclear for me, as the documentation lacks of more advanced examples. This is how I tried to test overriding the template of a field:

                ->add('userProfile.street', null, array(
                    'label' => 'Street',
                    'template' => 'custom-field.html.twig',
                ))

Location of the template: App/templates/Admin/

Any help appreciated

SonataAdmin automatically creates links to related entities once it has all of them configured and added to services. Then you can just change the route action of the link on the relation field as following:

            ->with('Assigned events', ['class' => 'col-md-6'])
                ->add('events', null, [
                    'route' => [
                        'name' => 'show'
                    ],
                    'label' => 'Events',
                ])
            ->end()

You can also change the type of relation field eg.'many_to_one' instead of null which might help in some cases.

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