Zuhayer Tahir
Zuhayer Tahir

Reputation: 1916

How to add a button to nav-bar?

How can I add another button/dropdown to the navbar in sonata admin listing template for my MapAdmin class?

Add button in sonata admin

I just want this button in one admin class.

Upvotes: 3

Views: 1712

Answers (3)

Capfer
Capfer

Reputation: 901

I have just come across with the same problem. I am using Symfony 3.4.6 and Sonata Admin Bundle 3.9.1.These are the steps I've followed:

 1. Find the standard template which lives in:/vendor/sonata-project/admin-bundle/src/Resources/views/standard_layout.html.twig.

 2. Go to /app/config/config.yml and under the key sonata_admin, you just override that template as shown below
sonata_admin:
templates:
    # Layout
    layout: '@MyBundle/Admin/Default/Layout/standard_layout.html.twig'
 3. Within your newly created template (standard_layout.html.twig) make sure you have extended the sonata standard template file like so : {% extends '@SonataAdmin/standard_layout.html.twig' %}. Now, all you need to do is override any block you want from the original sonata template file as I described in point 1, in my case I've just overridden the block tab_menu_navbar_header  and Added my custom button like so:

{% block tab_menu_navbar_header %}
  {% if _navbar_title is not empty %}
    <div class="navbar-header">
      <a class="navbar-brand" href="#">{{ _navbar_title|raw }}</a>
      {% if object.state is defined and object.state is not null and object.state is same as('finished') %}
        <button type="button" class="btn btn-info" style="margin-top: 10px;">
          <i class="fa fa-check-square" aria-hidden="true"> History</i>
        </button>
      {% endif %}
    </div>
  {% endif %}
{% endblock %}

Upvotes: 0

Neodork
Neodork

Reputation: 126

It requires adding a custom action and overriding a certain template. You can follow the documentation on symfony.com.

Read up to the following code block:

{# src/AppBundle/Resources/views/CRUD/list__action_clone.html.twig #}

<a class="btn btn-sm" href="{{ admin.generateObjectUrl('clone', object)}}">clone</a>

Upvotes: 0

BENARD Patrick
BENARD Patrick

Reputation: 30975

You have to override the default template (layout: 'SonataAdminBundle::standard_layout.html.twig') with your own in coding your logique here

Here is an extract of existing code :

                                        {% block sonata_admin_content_actions_wrappers %}
                                            {% if _actions|replace({ '<li>': '', '</li>': '' })|trim is not empty %}
                                                <ul class="nav navbar-nav navbar-right">
                                                {% if _actions|split('</a>')|length > 2 %}
                                                    <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>
                                                {% else %}
                                                    {{ _actions|raw }}
                                                {% endif %}
                                                </ul>
                                            {% endif %}
                                        {% endblock sonata_admin_content_actions_wrappers %}

Upvotes: 2

Related Questions