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:
- create new block class that implements BlockBundleInterface
- create new block template
- create block service (read and understand what are services in Symfony 2 library)
- add newly created service to Sonata Block Bundle configuration
- add newly created service to Sonata Admin Bundle configuration
- 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