编辑自定义块的内容SonataPageBundle

时间:2016-02-23 21:09:17

标签: php symfony twig sonata-admin

我已按照文档中的说明安装并配置了SonataPageBundle。我正在试图弄清楚如何在SonataAdmin中呈现我的自定义块的内容。我在config.yml中声明了我的块,我创建了一个服务,控制器,路由和我想要编辑内容块的页面。我已经运行了以下命令:

php app/console sonata:page:update-core-routes --site=all
php app/console sonata:page:create-snapshots --site=all

不幸的是,我的块内部的内容不会在后端呈现。我有两张图片向您展示我的问题:

我的后端:

picture1

这就是我希望它在我的块中的内容当然。 索纳塔沙箱:

picture2

这就是我到目前为止所做的:

路由:

block:
    path: index/block
    defaults:  {  _controller: FLYBookingsBundle:Default:myblock }

DefaultController:

public function myblockAction()
    {
        return $this->render('FLYBookingsBundle:Default:myblock.html.twig');
    }

Service.yml

sonata.block.service.myblock:
  class: FLY\BookingsBundle\Block\MyBlockService
  arguments: [ "sonata.block.service.myblock", @templating, @doctrine.orm.entity_manager ]
  tags:
    - { name: sonata.block }

MyBlockService

<?php
namespace FLY\BookingsBundle\Block;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Sonata\BlockBundle\Block\BlockContextInterface;
class MyBlockService extends BaseBlockService
{
    protected $em;

    public function __construct($type, $templating, $em)
    {
        $this->type = $type;
        $this->templating = $templating;
        $this->em = $em;
    }

    public function getName()
    {
        return 'MyBlock';
    }

    public function getDefaultSettings()
    {
        return array();
    }

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

    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
    }

    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        $settings = array_merge($this->getDefaultSettings(), $blockContext->getBlock()->getSettings());

        $data = count($this->em->getRepository("ApplicationSonataPageBundle:Page")->findAll());

        return $this->renderResponse('FLYBookingsBundle:Default:myblock.html.twig', array(
            'block' => $blockContext->getBlock(),
            'settings' => $settings,
            'data' => $data,
        ), $response);
    }
}

myblock.html.twig

{% extends 'SonataPageBundle:Block:block_base.html.twig' %}
{% block block %}
    <p> test test test </p>
{% endblock %}

sonata_block:
    default_contexts: [sonata_page_bundle]
    context_manager: sonata.page.block.context_manager
    blocks:
        sonata.user.block.menu:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        sonata.block.service.myblock: ~
        sonata.block.service.container:
        sonata.page.block.container:

etc....

0 个答案:

没有答案