0

I'm trying to build a custom block with Sonata, for the moment i'm just trying a simple block that just display text, but I cannot render the template as it seems that the service can't be found.

I do have the block declared in config.yml

sonata_block:
    default_contexts: [cms]
    blocks:
        app.service.block.portfolio:

dashboard:
    blocks:
        - { position: right, type: app.service.block.portfolio, settings: { content: "<h2>This is a test block</h2>"} }

I also can see my service is up:

app/console debug:container | grep app.service.block.portfolio
  app.service.block.portfolio     App\CoreBundle\Block\BlockPortfolio

This is the code I used:

<?php

namespace App\CoreBundle\Block;

use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\CoreBundle\Model\Metadata;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use JMS\DiExtraBundle\Annotation as DI;


/**
 * @DI\Service("app.service.block.portfolio")
 */
class BlockPortfolio extends AbstractAdminBlockService
{
/**
 * {@inheritdoc}
 */
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
    return $this->renderResponse($blockContext->getTemplate(), array(
        'block' => $blockContext->getBlock(),
        'settings' => $blockContext->getSettings(),
    ), $response);
}

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

/**
 * {@inheritdoc}
 */
public function configureSettings(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'content' => 'Insert your custom content here',
        'template' => 'SonataBlockBundle:Block:block_core_text.html.twig',
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockMetadata($code = null)
{
    return new Metadata($this->getName(), (!is_null($code) ? $code : $this->getName()), false, 'SonataBlockBundle', array(
        'class' => 'fa fa-file-text-o',
    ));
}

}

anyone has a clue ?

Regards, Julien

JulienCoo
  • 1,128
  • 3
  • 13
  • 24

1 Answers1

0

Ok, I found the answer, I was missing the tag in the service.

/**
 * @DI\Service("app.service.block.portfolio")
 * @DI\Tag(name="sonata.block")
 */

I'm not sure why you need this tag, though. If someone knows the reason, I'd be happy to here it.

Cheers, Julien

JulienCoo
  • 1,128
  • 3
  • 13
  • 24