Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

symfony - How to add custom link or button to SonataAdminBundle Dashboard in Symfony2

I am new with symfony2 and SonataAdminBundle.

I have added 3 entities to the SonataAdminBundle Dashboard and they appear successfully.

The Entities appear with the default links - "Add new" and "List" buttons.

I want to be able to do the following

  1. I want to add a third link to one of the entities on the dashboard.
  2. I want to be able to add a link to the buttons located above the grid in the default list page.
  3. I want to be able to add/customize links appearing under the form on Edit or Create new page

I have not been able to find a way to do any of the above, been searching for hours, any help will be highly appreciated.

thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To display custom elements in the dashbord Sonata Admin uses Sonata Block Bundle. To add custom link or button you need to create a new block using Sonata Block Bundle. The core template (dashboard.html.twig) of the Admin Bundle iterates blocks that is configured to run (in config.yml of the application). Still, Admin Bundle iterates all your entity blocks in the template block_admin_list.html.twig. Creating your custom block template it is from here that you can take layout to wrap your custom groups (sections) and buttons so they have same feel as those of the entities groups.

Ok, it was introduction.

For example we want to make custom newsletter section.

There are steps:

  1. create new block class that implements BlockBundleInterface
  2. create new block template
  3. create block service (read and understand what are services in Symfony 2 library)
  4. add newly created service to Sonata Block Bundle configuration
  5. add newly created service to Sonata Admin Bundle configuration
  6. enter dashboard and enjoy new group/button/link/whatever-stuff-you-put-in-your-block-template :)

Ad1) Create new block class

General instruction under: http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html

My file looks like this:

<?php

namespace InstitutoStoricoBundleNewsletterBundleBlock;

use SymfonyComponentHttpFoundationResponse;

use SonataAdminBundleFormFormMapper;
use SonataAdminBundleValidatorErrorElement;

use SonataBlockBundleModelBlockInterface;
use SonataBlockBundleBlockBaseBlockService;

class NewsletterBlockService extends BaseBlockService
{
    public function getName()
    {
        return 'My Newsletter';
    }

    public function getDefaultSettings()
    {
        return array();
    }

    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
    }

    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
    }

    public function execute(BlockInterface $block, Response $response = null)
    {
        // merge settings
        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());

        return $this->renderResponse('InstitutoStoricoNewsletterBundle:Block:block_my_newsletter.html.twig', array(
            'block'     => $block,
            'settings'  => $settings
            ), $response);
    }
}

I added some lines reading Sonata Media Bundle code files.

Ad2) Create new block template

Layout I took from block_admin_list.html.twig of Sonata Admin bundle.

My file looks like this:

{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}

{% block block %}
<table class="table table-bordered table-striped sonata-ba-list">
    <thead>
        <tr>
            <th colspan="3">Newsletter - inviare</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>
                <div class="btn-group" align="center">
                    <a class="btn btn-small" href="#">Servizio Newsletter</a>
                </div>
            </td>
        </tr>
    </tbody>
</table>
{% endblock %}

Ad3) Create block service

In your bundle there's a file where you declare services (services.yml or admin.yml). Whatever. But it's important that it is plugged into config.yml of your application in "imports" section.

My service declaration looks like this:

sonata.block.service.newsletter:
    class: InstitutoStoricoBundleNewsletterBundleBlockNewsletterBlockService
    arguments: [ "sonata.block.service.newsletter", @templating ]
    tags:
        - { name: sonata.block }

Ad4) Add newly created service to Sonata Block Bundle configuration

This configuration is put into config.yml of your application.

My config looks like this:

#Sonata Block Bundle
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        sonata.block.service.text: ~
        sonata.block.service.action: ~
        sonata.block.service.rss: ~
        sonata.block.service.newsletter: ~

Ad5) Add newly created service to Sonata Admin Bundle configuration

This configuration is put into config.yml of your application.

My config looks like this:

# Sonata Admin Generator
sonata_admin:
    ...
    dashboard:
        blocks:
            # display a dashboard block
            - { position: left, type: sonata.admin.block.admin_list }
            - { position: left, type: sonata.block.service.newsletter}

Ad6) Enter dashboard and enjoy

That's all. Looks like complicated, but to be sincere it is not so much. It's important it's a clean way of modifying your dashboard page without overriding whole templates without great need. All those Il learned reading source code of Admin Bundle :) Whole day


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...