Add custom Sonata page route to the navbar
Asked Answered
J

2

10

I've created a custom Sonata page

Simple route

medapp_adminStreamCommands:
    path:     /admin/stream
    defaults: { _controller: MedAppBundle:VideoChat/VideoChat:adminStreamCommands }

Controller that returns the admin pool

  public function adminStreamCommandsAction(Request $request)
    {

        return $this->render('@MedApp/AdminSonata/Stream/stream_commands.html.twig', array(
            'admin_pool' => $this->get('sonata.admin.pool')));
    }

Plain view template

{% extends '@MedApp/AdminSonata/standard_layout.html.twig' %}

{% block content %}
foobar
{% endblock content 

This works, I can access it on my website with /admin/foo and I get a page which has the Sonata admin template with my 'foobar' content.

My question is, how can I add this route to the left and top navbar without having to modify the default template? That is because the left menu is rendered by a KNP menu:

{% block side_bar_nav %}
    {% if app.user and is_granted('ROLE_SONATA_ADMIN') %}
        {{ knp_menu_render('sonata_admin_sidebar', {template: admin_pool.getTemplate('knp_menu_template')}) }}
    {% endif %}
{% endblock side_bar_nav %}

And I somehow need to add my new page to be rendered by this menu.

Normally, a page is added through a service, but these are built on top of an entity:

servicename:
     class: Bundle\Class
     arguments: [~, Bundle\Entity\Entityname, ~]
     tags:
         - { name: sonata.admin, manager_type: orm, group: admin, label: CustomName}

My page is not using an entity, though, just static content or content that is not dependant on an entity.

I know already that I can modify the blocks that generate the menus, but I was thinking that the best way would be to add my class as a service tagged as sonata.admin that doesn't have an orm manager_type, in other words, is not an Entity. How can that be done?

Jonasjonathan answered 17/11, 2015 at 12:15 Comment(0)
A
2

You should override standard_layout and modify content of side_bar_nav block. This is simple and fast way. Or you can dig into sonata code to find how to inject something into admin_pool.dashboardgroups - have fun :)

Astomatous answered 24/11, 2015 at 1:16 Comment(1)
I know, that's what the other answer said. I was looking for ways to use entities that are not tied to the ORM. That way I can define my own custom data for the admin panel, but the menu would still include it because it will be a service with the sonata.admin tag.Jonasjonathan
E
1

I don't think that's possible, you have to create a new layout, copy the sonata admin layout and customize it to your need.

You can change the layout used by changing the yml configuration for sonata_admin (templates -> layout) or extending the SonataAdmin bundle and creating your own layout.html.twig.

Endoblast answered 19/11, 2015 at 9:56 Comment(3)
I did that already. However, I'd need to generate the menu based on both the admin pool routes and my own custom page(s). I was thinking sonata has a way of doing it through the yml file or some other easier way rather than having me to create my own menu. I imagine I'm not the first one that wants a similar functionality, but I couldn't find any example that does this.Jonasjonathan
Don't know if there is another way than create your own menu, you could also set your own links by checking the current route of your page and add your own html, that's a dirty way to do it but will work.Endoblast
Indeed. Another way would've been to set an Admin class for a Model that is not an Entity tied to the ORM. I've seen discussion about that way but again, couldn't find any example on how to do it. That would work for sure as I can add routes to the routecollection of the current Model and implement custom actions. Unfortunately it's something I couldn't find anything about or if it's even possible.Jonasjonathan

© 2022 - 2024 — McMap. All rights reserved.