[BlockBundle] Custom block creation

798 views
Skip to first unread message

Remote Shelter

unread,
May 24, 2012, 12:09:54 PM5/24/12
to sonata-users
Hi all, i'm following directions from
http://sonata-project.org/bundles/block/2-0/doc/reference/your_first_block.html
to create a new block.

I've defined a TwigBlockService class that extends BaseBlockService in
my bundle as follows:

namespace OB\TwigBlockBundle\Block\Service;

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 TwigBlockService extends BaseBlockService {

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

return
$this->renderResponse('OBTwigBlockBundle:Twig:block_twig.html.twig', array(
'block' => $block,
'settings' => $settings
), $response);
}

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

/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface
$block) {
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('content', 'text', array()),
)
));
}

/**
* {@inheritdoc}
*/
public function getName() {
return 'Twig';
}

/**
* {@inheritdoc}
*/
function getDefaultSettings() {
return array(
'template' => 'Insert your custom content here',
);
}

}

and i've defined a service like this in the services.xml of my bundle:

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<!-- <service id="ob.block.service.twig"
class="Sonata\BlockBundle\Block\Service\RssBlockService" public="false">-->
<services>
<service id="ob.block.service.twig"
class="OB\TwigBlockBundle\Block\Service\TwigBlockService" public="false">
<tag name="sonata.block" />
<argument>ob.block.service.twig</argument>
<argument type="service" id="templating" />
</service>
</services>
</container>

I've also added the following line in my app config.yml:
sonata_block:
default_contexts: [cms]
blocks:
sonata.admin.block.admin_list:
contexts: [admin]
sonata.block.service.text:
sonata.block.service.rss:
# ADDED
ob.block.service.twig:


I'm getting problems with the service that is not created, as i can see
from the output of:
app/console container:debug
and the service isn't listed

Also
app/console sonata:block:debug
states that the service does not exist, and gives the following error:
You have requested a non-existent service "ob.block.service.twig".

The problem seems to be like this one:
https://github.com/sonata-project/SonataBlockBundle/issues/8

What's wrong with the service definition ?
Any ideas about why the service isn't created ?

Any help is apreciaded,
Bye

rshelter

unread,
May 24, 2012, 12:44:01 PM5/24/12
to sonata...@googlegroups.com

I've found the solution to the service not created problem, and i publish it as it can be helpful for someone else.
The documentation at:
http://sonata-project.org/bundles/block/2-0/doc/reference/your_first_block.html
states the definition of the service like this:


    <services>
        <service id="ob.block.service.twig"
class="OB\TwigBlockBundle\Block\Service\TwigBlockService" public="false">
            <tag name="sonata.block" />
            <argument>ob.block.service.twig</argument>
            <argument type="service" id="templating" />
        </service>
    </services>  
</container>

 The problem is that the service is defined as public="false".

Once removed the service is visible and everything goes well...
Bye all
Reply all
Reply to author
Forward
0 new messages