Add a custom action with custom view in Sonata Admin Bundle ?

7,923 views
Skip to first unread message

LouWii

unread,
Aug 20, 2013, 1:41:01 PM8/20/13
to sonat...@googlegroups.com
Hi everyone !

I'm quite lost, I tried to search but I didn't find any answers.

I created a bundle for custom admin actions that don't exist in the admin bundle.
For example, I have one to list files from a specific folder. So it needs a specific methods in a controller, and a specific view.

I want to use the same "base" template so that all the admin part of my project has the same style, same menus... I tried {% extends 'SonataAdminBundle::standard_layout.html.twig' %} but the menus aren't there...

The first question in my mind is : am I doing it right ?
I'm not sure that it's the right way to do it.

If yes, how can I render the exact same template of Admin Bundle with its menu ?

Thanks :)

Cassiano Tartari

unread,
Aug 20, 2013, 1:45:54 PM8/20/13
to sonat...@googlegroups.com
Search a little in the group that you will find it, I've answered this once or twice.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


--
You received this message because you are subscribed to the Google Groups "sonata-devs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonata-devs...@googlegroups.com.
To post to this group, send email to sonat...@googlegroups.com.
Visit this group at http://groups.google.com/group/sonata-devs.
For more options, visit https://groups.google.com/groups/opt_out.

LouWii

unread,
Aug 20, 2013, 2:22:45 PM8/20/13
to sonat...@googlegroups.com
Thanks for your answer.

I can't find anything relevant, that's why I posted here :/

Cassiano Tartari

unread,
Aug 20, 2013, 2:38:04 PM8/20/13
to sonata-devs
ok, let's try to resume:

in the admin class:
    protected function configureRoutes(RouteCollection $collection) {
        $collection->add(''action_name', $this->getRouterIdParameter() . '/path-to-action-name');
    }

protected function configureListFields(ListMapper $listMapper) {
        $listMapper
                ->add('_action', 'actions', array(
                    'actions' => array(
                        ''action_name' => array('template' => YourBundle:action_name':your_template.html.twig'),
                    )
                ))
        ;
    }


in the controller:

public function action_nameAction($id = NULL) {
  //your code
}

Don't forget to set your controller in the services.yml

And your template should extend {% extends 'SonataAdminBundle::standard_layout.html.twig' %} like you said but if you write inside a block don't forget to call {{ parent() }} before or after your code to include the code of the father template.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


LouWii

unread,
Aug 21, 2013, 3:20:44 AM8/21/13
to sonat...@googlegroups.com
Thanks a lot !
That's a great help !

LouWii

unread,
Aug 21, 2013, 12:37:19 PM8/21/13
to sonat...@googlegroups.com
OK, I just tried and I don't think that this is what I wanted.

By putting

'action_name' => array('template' => 'MyBundle:ViewFolder:not-added.html.twig'),

Inside the configureListFields method of my existing EntityAdmin class, the view 'not-added.html.twig' is trying to be rendered directly inside the entity list. It can't because a variable is missing. Of course it's missing, because it doesn't go through my Controller.
But I don't want this to be displayed in the list view,

Maybe i missed something ?

What I want to do is :

- display a custom link in the dashboard : done
- nicely extend the sonata standard_layout.html.twig to get all the menus working in an external view with a custom controller : not done
--- as I told, the template is displayed, but totally empty (no navbar menus)

I hope to be understandable. I'm french and it's quite hard to explain my problem in English.

Cassiano Tartari

unread,
Aug 21, 2013, 1:24:53 PM8/21/13
to sonata-devs
I'm Brazilian, don't worry about your English, it is fine for me.

Ok, you almost did it.

Let's detail a little bit more.


    protected function configureRoutes(RouteCollection $collection) {
        $collection->add(''action_name', $this->getRouterIdParameter() . '/path-to-action-name');
    }

====================================================================================================

    protected function configureListFields(ListMapper $listMapper) {
          $listMapper
                  ->add('_action', 'actions', array(
                      'label' => 'Actions',
                      'actions' => array(
                          'action_name' => array('template' => 'YourBundle:action_name:action_template.html.twig'),
                      )
                  ))
          ;
      }

====================================================================================================

{# action_template.html.twig => this template will add a button in your list with the link to the controller action #}

{% if admin.hasRoute(''action_name') %}
    <a href="{{ admin.generateObjectUrl(''action_name', object) }}" class="btn" title="{{ ''action_name'|trans({}, 'SonataAdminBundle') }}">
        <i class="icon-remove"></i>
        {{ ''action_name'|trans({}, 'SonataAdminBundle') }}
    </a>
{% endif %}
    
====================================================================================================


public function action_nameAction($id = NULL) {
      $id = $this->get('request')->get($this->admin->getIdParameter());

      $object = $this->admin->getObject($id);

      if (!$object) {
         throw new NotFoundHttpException(sprintf('not found object id : %s', $id));
      }

      //your code

       $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

        return $this->render('YourBundle:action_name:list_action_template.html.twig', array(
            'action'   => 'action_name',
            //more objects...
        ));


}

====================================================================================================


{# list_action_template.html.twig #}

{% extends 'SonataAdminBundle::standard_layout.html.twig' %} {# You could extend base_list too #}

{% block stylesheets %}
{{ parent() }}
//you can add more css here for example
{% endblock %}

{% block list_table %}
//codeeeee
 {% endblock %}

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


LouWii

unread,
Aug 21, 2013, 1:52:54 PM8/21/13
to sonat...@googlegroups.com

OK, I understand now the way to do it. Thank you !

Everything is OK

But I don't have navbar menus in the list_action_template.html.twig

I've attached a screenshot to show it :







In the dashboard, there are the Administration title and all menus about the admin entities, like this :



Sans titre.png

Cassiano Tartari

unread,
Aug 21, 2013, 1:59:05 PM8/21/13
to sonata-devs
The way I wrote is: you make some default list action in an admin class, and in each item of this list you can add an action triggered by the button to your controller that can render your template that extends the sonata admin standard.

If you want to make different you can override just the listAction() and add your template.

I hope you understood.

Cassiano Valle Tartari
MSc. Computer Engineer

Tel: +55.48.84474818
Email: fal...@cassianotartari.eng.br
Site: http://www.cassianotartari.eng.br

QR Code


LouWii

unread,
Aug 21, 2013, 2:10:00 PM8/21/13
to sonat...@googlegroups.com
Yes I get it. This is working, I get a new button on my entity list, and the button is leading me to the custom controller, with my custom view.

Thank you very much ! :)
Reply all
Reply to author
Forward
0 new messages