This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
ContainerBlockService.php
105 lines (89 loc) · 3.07 KB
/
ContainerBlockService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Symfony\Cmf\Bundle\BlockBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sonata\BlockBundle\Block\BlockServiceInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Block\BaseBlockService;
use Sonata\BlockBundle\Block\BlockRendererInterface;
class ContainerBlockService extends BaseBlockService implements BlockServiceInterface
{
protected $blockRenderer;
protected $template = 'SymfonyCmfBlockBundle:Block:block_container.html.twig';
/**
* @param string $name
* @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
* @param \Sonata\BlockBundle\Block\BlockRendererInterface $blockRenderer
* @param string|null $template
*/
public function __construct($name, EngineInterface $templating, BlockRendererInterface $blockRenderer, $template = null)
{
parent::__construct($name, $templating);
$this->blockRenderer = $blockRenderer;
if ($template) {
$this->template = $template;
}
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $form, BlockInterface $block)
{
throw new \RuntimeException('Not used at the moment, editing using a frontend or backend UI could be changed here');
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
throw new \RuntimeException('Not used at the moment, validation for editing using a frontend or backend UI could be changed here');
}
/**
* @return string
*/
protected function getTemplate()
{
return $this->template;
}
/**
* @param BlockInterface $block
* @param null|Response $response
*
* @return Response
*/
public function execute(BlockInterface $block, Response $response = null)
{
if (!$response) {
$response = new Response();
}
if ($block->getEnabled()) {
// merge settings
$settings = is_array($block->getSettings()) ? array_merge($this->getDefaultSettings(), $block->getSettings()) : $this->getDefaultSettings();
$childBlocks = array();
foreach ($block->getChildren()->getValues() as $childBlock) {
$childBlocks[] = $this->blockRenderer->render($childBlock)->getContent();
}
return $this->renderResponse($this->getTemplate(), array(
'block' => $block,
'childBlocks' => $childBlocks,
'settings' => $settings
), $response);
}
return $response;
}
/**
* Returns the default settings link to the service
*
* @return array
*/
public function getDefaultSettings()
{
return array(
'divisibleBy' => false,
'divisibleClass' => '',
'childClass' => '',
);
}
}