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
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:
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
I had troubles with method execute (I am using Sonata 2.3.x ). Here is the code that works for me.
Note BlockContextInterface and $blockContext->getBlock() :
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = array_merge($this->getDefaultSettings(), $blockContext->getSettings());
return $this->renderResponse('bundleName:Block:templateName.html.twig', array(
'block' => $blockContext->getBlock(),
'settings' => $settings
), $response);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With