简体   繁体   中英

Add new Button in the List view of sonata admin Bundle

Maybe the question is too easy to answer, but I searched in the documentation of sonata admin bundle and i didn't find what I need. When you navigate to the List view of a model in sonata admin Bundle you find the Action Button in the upper right and under it you find the add new action. In my case I need that the Add new action to be displayed directly In the View Like in this screenshot : 在此输入图像描述

Any one can help me please ?

I know this is an old question, but I'll leave this answer for future reference.

I had to do something similar, this is how I did it.

In your admin class override the configureActionButtons() method

class YourAdmin extends AbstractAdmin
{
    //...
    /**
     * Overriden from (AbstractAdmin)
     */
    public function configureActionButtons($action, $object = null)
    {
        $list = parent::configureActionButtons($action,$object);

        $list['custom_action'] = array(
                'template' =>  'AcmeBundle:YourAdmin:custom_button.html.twig',
        );

        return $list;
    }
    //...

}

And then creating your button in AcmeBundle/Resources/views/YourAdmin/custom_button.html.twig

{# custom_button.html.twig #}    
<a class="sonata-action-element" href="{{ admin.generateUrl('your_route') }}">
   <i class="fa fa-plus-circle"></i>Custom Action
</a>

Of course you can add permissions check afterwards (they were omitted for clarity)

Hope it helps somebody

Which Sonata version are you on ? And what is the version of your example?

Mine is 2.3, does not look like you can play on that from parameters, but you can override the layout to let only appear the create button.

If you need it for all your admins, you better override the layout from the config. If you need it only for list, or show, or remove only overrides those templates then. in config.yml:

sonata_admin:
    templates:
        layout:    AppBundle:Layouts:standard_layout_override.html.twig
        show:      AppBundle:Layouts:show.html.twig
        list:      AppBundle:Layouts:list.html.twig
        delete:    AppBundle:Layouts:delete.html.twig
        edit:      AppBundle:Layouts:edit.html.twig

in that file override that block:

{% block sonata_page_content_header %}
    {% block sonata_page_content_nav %}
        {% if _tab_menu is not empty or _actions is not empty %}
            <nav class="navbar navbar-default" role="navigation">
                {% block tab_menu_navbar_header %}
                    {% if _navbar_title is not empty %}
                        <div class="navbar-header">
                            <span class="navbar-brand">{{ _navbar_title|raw }}</span>
                        </div>
                    {% endif %}
                {% endblock %}
                <div class="container-fluid">
                    <div class="navbar-left">
                        {% if _tab_menu is not empty %}
                            {{ _tab_menu|raw }}
                        {% endif %}
                    </div>

                    {% if _actions|replace({ '<li>': '', '</li>': '' })|trim is not empty %}
                        <ul class="nav navbar-nav navbar-right">
                            <li class="dropdown sonata-actions">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ 'link_actions'|trans({}, 'SonataAdminBundle') }} <b class="caret"></b></a>
                                <ul class="dropdown-menu" role="menu">
                                    {{ _actions|raw }}
                                </ul>
                            </li>
                        </ul>
                    {% endif %}
                </div>
            </nav>
        {% endif %}
    {% endblock sonata_page_content_nav %}
{% endblock sonata_page_content_header %}

You should also be able to do it for some admins only overriding defining a specific template for those admins on service definitions like that:

app.admin.product:
        class: AppBundle\Admin\ProductAdmin
        arguments: [~, AppBundle\Entity\Product, AppBundle:Admin\Product]
        tags:
            - {name: sonata.admin, manager_type: orm, group: Products, label: Products}
        calls:
            - [ setTemplate, [edit, AppBundle:Product:edit.html.twig]]

But I can't get that "AppBundle:Product:edit.html.twig" template to delete the list action overriding the same block.

Hope this helps you.

In SonataAdmin ~3 you can configure the templates that display the template for your action directly in the YourEntityAdmin::configureListFields() method.

To do so you first

create new template anywhere, extend sonata layout and use sonata_admin_content block.

"Anywhere" really means "anywhere": this confused me a bit at first.

Basically, you create a template for example in Resources\\CRUD\\list__action_your_action.html.twig and then call it from the configuration in the YourEntityAdmin::configureListFields() :

class YourEntityAdmin
{
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper

             // other fields...

            ->add('_action', null, array(
                'actions' => array(

                    // ...

                    'clone' => array(
                        'template' => 'AppBundle:CRUD:list__your_action.html.twig'
                    )
                )
            ))
        ;
    }
}

REMEMBER TO WRITE SOMETHING IN THIS TEMPLATE, ALSO A SIMPLE STRING LIKE

{# Resources\CRUD\list__action_your_action.html.twig #}
your action button template

"your action button template" will be rendered in the "Actions" column and is basically the label of your button: using the template you can manipulate it as you like. This were really hard to understand as in my column there were no actions as I leaved the template empty so not rendering anything!

Anyway, you can find the full procedure to add a new button (and an associated route/action) in the documentation about CREATING A CUSTOM ADMIN ACTION .

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