渲染模板期间的异常:“块服务`app.service.block.custom`不存在”

时间:2017-09-12 10:15:27

标签: symfony sonata-admin

我正在尝试使用Sonata构建一个自定义块,目前我只是尝试一个只显示文本的简单块,但我无法渲染模板,因为似乎无法找到该服务。 / p>

我确实在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>"} }

我也可以看到我的服务已经启动:

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

这是我使用的代码:

<?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',
    ));
}

}

任何人都有线索?

此致 于连

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案,我错过了服务中的标签。

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

但我不确定为什么你需要这个标签。如果有人知道原因,我会很高兴来到这里。

干杯, 于连