Created
December 27, 2012 21:46
-
-
Save anonymous/4392323 to your computer and use it in GitHub Desktop.
How to add a custom block in sonata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends 'SonataBlockBundle:Block:block_base.html.twig' %} | |
{% block block %} | |
<h2>{{ settings.title }}</h2> | |
<div class="sonata-feeds-container"> | |
{% for meeting in meetings %} | |
<div> | |
<div>{{ meeting.date }}, {{ meeting.time }}</div> | |
<div>{{ meeting.location }}</div> | |
</div> | |
{% else %} | |
No upcoming meetings scheduled. | |
{% endfor %} | |
</div> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sonata_admin: | |
dashboard: | |
blocks: | |
- { position: right, type: my.block.service.meetings, settings: { title: Meetings } } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Bundle\BlockBundle\Block; | |
use Symfony\Component\HttpFoundation\Response; | |
use Sonata\BlockBundle\Model\BlockInterface; | |
use Sonata\BlockBundle\Block\BaseBlockService; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Sonata\AdminBundle\Validator\ErrorElement; | |
class MeetingBlock extends BaseBlockService | |
{ | |
function getDefaultSettings() | |
{ | |
return array( | |
'title' => 'My Title' | |
); | |
} | |
/** | |
* @param FormMapper $form | |
* @param BlockInterface $block | |
* | |
* @return void | |
*/ | |
function buildEditForm(FormMapper $form, BlockInterface $block) | |
{ | |
$form->add('settings', 'sonata_type_immutable_array', array( | |
'keys' => array( | |
array('title', 'text', array('required' => false)), | |
) | |
)); | |
} | |
/** | |
* @param BlockInterface $block | |
* @param null|Response $response | |
* | |
* @return Response | |
*/ | |
function execute(BlockInterface $block, Response $response = null) | |
{ | |
$settings = array_merge($this->getDefaultSettings(), $block->getSettings()); | |
$meetings = array(); | |
return $this->renderResponse('MyBlockBundle:Block:block.html.twig', array( | |
'settings' => $settings, | |
'meetings' => $meetings, | |
), $response); | |
} | |
/** | |
* @param ErrorElement $errorElement | |
* @param BlockInterface $block | |
* | |
* @return void | |
*/ | |
function validateBlock(ErrorElement $errorElement, BlockInterface $block) | |
{ | |
$errorElement | |
->with('settings.title') | |
->assertNotNull(array()) | |
->assertNotBlank() | |
->assertMaxLength(array('limit' => 50)) | |
->end(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
my.block.service.meetings: | |
class: My\Bundle\BlockBundle\Block\MeetingBlock | |
tags: | |
- { name: sonata.block } | |
arguments: ['nameofblock?', @templating ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment