如何在 Symfony2 中向 SonataAdminBundle Dashboard 添加自定义链接或按钮 [英] How to add custom link or button to SonataAdminBundle Dashboard in Symfony2

查看:21
本文介绍了如何在 Symfony2 中向 SonataAdminBundle Dashboard 添加自定义链接或按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 symfony2 和 SonataAdminBundle 的新手.

我已将 3 个实体添加到 SonataAdminBundle 仪表板,并且它们成功显示.

实体显示时带有默认链接 - 添加新"和列表"按钮.

我希望能够执行以下操作

  1. 我想添加指向仪表板上实体之一的第三个链接.
  2. 我希望能够添加指向位于网格上方的按钮的链接默认列表页面.
  3. 我希望能够添加/自定义出现在编辑或创建新页面上的表单下的链接

我一直无法找到一种方法来执行上述任何操作,已经搜索了数小时,任何帮助将不胜感激.

谢谢.

解决方案

在仪表板中显示自定义元素 Sonata Admin 使用 Sonata Block Bundle.要添加自定义链接或按钮,您需要使用 Sonata Block Bundle 创建一个新块.Admin Bundle 的核心模板 (dashboard.html.twig) 迭代配置为运行的块(在应用程序的 config.yml 中).尽管如此,Admin Bundle 会迭代模板 block_admin_list.html.twig 中的所有实体块.创建您的自定义块模板,您可以从这里开始布局来包装您的自定义组(部分)和按钮,以便它们与实体组的感觉相同.

好的,这是介绍.

例如,我们要制作自定义时事通讯部分.

有步骤:

  1. 创建实现 BlockBundleInterface 的新块类
  2. 创建新的块模板
  3. 创建块服务(阅读并理解 Symfony 2 库中的服务是什么)
  4. 将新创建的服务添加到 Sonata Block Bundle 配置中
  5. 将新创建的服务添加到 Sonata Admin Bundle 配置
  6. 进入仪表板并享受新的组/按钮/链接/whatever-stuff-you-put-in-your-block-template :)

Ad1) 创建新的块类

一般说明如下:http://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html

我的文件如下所示:

getDefaultSettings(), $block->getSettings());return $this->renderResponse('InstitutoStoricoNewsletterBundle:Block:block_my_newsletter.html.twig', array('块' =>$块,'设置' =>$设置), $响应);}}

我添加了一些读取 Sonata Media Bundle 代码文件的行.

Ad2) 创建新的块模板

我从 Sonata Admin 包的 block_admin_list.html.twig 中获取的布局.

我的文件如下所示:

{% extends 'SonataBlockBundle:Block:block_base.html.twig' %}{% 块块 %}<table class="table table-bordered table-striped sonata-ba-list"><头><tr><th colspan="3">时事通讯 - inviare</th></tr></thead><tr><td><div class="btn-group" align="center"><a class="btn btn-small" href="#">Servizio Newsletter</a>

</td></tr></tbody>{% 结束块 %}

Ad3) 创建块服务

在您的包中有一个文件,您可以在其中声明服务(services.yml 或 admin.yml).任何.但重要的是将其插入到导入"部分中应用程序的 config.yml 中.

我的服务声明如下所示:

sonata.block.service.newsletter:类:InstitutoStorico\Bundle\NewsletterBundle\Block\NewsletterBlockService参数:[sonata.block.service.newsletter",@templating]标签:- { 名称:奏鸣曲.block }

Ad4) 将新创建的服务添加到 Sonata Block Bundle 配置

这个配置被放入你的应用程序的 config.yml 中.

我的配置如下:

#Sonata Block Bundle奏鸣曲块:default_contexts: [cms]块:奏鸣曲.admin.block.admin_list:上下文:[管理员]奏鸣曲.block.service.text:~奏鸣曲.block.service.action:~奏鸣曲.block.service.rss:~奏鸣曲.block.service.newsletter:~

Ad5) 将新创建的服务添加到 Sonata Admin Bundle 配置

这个配置被放入你的应用程序的 config.yml 中.

我的配置如下:

# Sonata Admin Generator奏鸣曲管理员:...仪表盘:块:# 显示仪表板块- { 位置:左,输入:sonata.admin.block.admin_list }- {位置:左,类型:sonata.block.service.newsletter}

Ad6) 进入仪表板并享受

仅此而已.看起来很复杂,但说实话并没有那么复杂.重要的是,它是一种修改仪表板页面的干净方式,无需在不需要的情况下覆盖整个模板.所有这些我都学会了阅读 Admin Bundle 的源代码 :) 一整天

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.

解决方案

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 InstitutoStorico\Bundle\NewsletterBundle\Block;

use Symfony\Component\HttpFoundation\Response;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;

use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;

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: InstitutoStorico\Bundle\NewsletterBundle\Block\NewsletterBlockService
    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

这篇关于如何在 Symfony2 中向 SonataAdminBundle Dashboard 添加自定义链接或按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆