简体   繁体   中英

how to create custom list with sonata admin

I can't find (understand) how to add items and cathegories to Left Side Admin Menu. There is no detailed tutorial how to do this. I can add items to sonata.admin.block.admin_list but to aside Admin Menu enter image description here

Since the menu is automatically built by traversing the registered admins there is no (to me known) simple solution for that by adding just an admin service entry. But it's quite easy yet. You could register a MenuBuilderListener and add new items as many as you want ...

In your services.yml

app.menu_listener:
    class: AppBundle\Listener\MenuBuilderListener
    tags:
        - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }

In you MenuBuilderListener class

public function addMenuItems(ConfigureMenuEvent $event)
{
    $event->getMenu()->addChild($event->getFactory()->createItem('Test entry', ['route' => 'custom_route']));
    ...
}

The event is called after your menu is build ... you can change existing menu entries afterwards or just add new entries. Note that the admin service usually will take responsibility for creating routes, if you don't have an admin you have to build routes by yourself.

Yes you can do sth like that:

sonata_admin:
title:      Supplier Portal
title_logo: bundles/supplierportalbackend/img/logo.png
show_mosaic_button: false
security:
    handler: sonata.admin.security.handler.role
templates:
    layout:              SonataAdminBundle::layout.html.twig
    search:              SonataAdminBundle:Core:search.html.twig
    search_result_block: SonataAdminBundle:Block:block_search_result.html.twig
    base_list_field:     SonataAdminBundle:CRUD:base_list_field.html.twig

dashboard:
    blocks:
        - { position: left, type: sonata.admin.block.admin_list }
    # Customize user portal menu by setting links
    groups:
        sonata.admin.group.purchase_order_item.open:
            on_top:          true
            label:           Open Items
            label_catalogue: messages
            icon:            '<i class="fa fa-square-o"></i>'
            items:
                - supplier_portal_backend.admin.purchase_open_order_item

        sonata.admin.group.purchase_order_item.confirmed:
            on_top:          true
            label:           Confirmed Items
            label_catalogue: messages
            icon:            '<i class="fa fa-check-square-o"></i>'
            items:
                - supplier_portal_backend.admin.purchase_confirmed_order_item

        sonata.admin.group.purchase_order_item.shipped:
            on_top:          true
            label:           Shipped Items
            label_catalogue: messages
            icon:            '<i class="fa fa-truck"></i>'
            items:
                - supplier_portal_backend.admin.purchase_shipped_order_item

        sonata.admin.group.purchase_order_item.all:
            on_top:          true
            label:           All Items
            label_catalogue: messages
            icon:            '<i class="fa fa-th-list"></i>'
            items:
                - supplier_portal_backend.admin.purchase_order_item

        sonata.admin.group.administration:
            on_top:          true
            label:           Users
            label_catalogue: messages
            icon:            '<i class="fa fa-users"></i>'
            items:
                - supplier_portal_backend.admin.user
            roles: [ ROLE_SUPER_ADMIN ]

You need to add a route to your routing:

admin.blog_post:
    class: db306\CoreBundle\Admin\BlogPostAdmin
    arguments: [~, db306\CoreBundle\Entity\BlogPost, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, group: Content, label: Blog post }
    public: true

admin.category:
    class: db306\CoreBundle\Admin\CategoryAdmin
    arguments: [~, db306\CoreBundle\Entity\Category, ~]
    tags:
        - { name: sonata.admin, manager_type: orm, group: Content, label: Category }
    public: true

This will display a "Content" tab on that menu displayed on your screenshot with 2 options: 'Blog post' and 'Category'

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