-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathContainerBlockService.php
111 lines (98 loc) · 3.47 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
106
107
108
109
110
111
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\BlockBundle\Block\Service;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Form\Mapper\FormMapper;
use Sonata\BlockBundle\Form\Type\ContainerTemplateType;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Form\Type\CollectionType;
use Sonata\Form\Type\ImmutableArrayType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Render children pages.
*
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
final class ContainerBlockService extends AbstractAdminBlockService
{
public function buildEditForm(FormMapper $formMapper, BlockInterface $block): void
{
$formMapper->add('enabled');
$formMapper->add('settings', ImmutableArrayType::class, [
'keys' => [
['code', TextType::class, [
'required' => false,
'label' => 'form.label_code',
]],
['layout', TextareaType::class, [
'label' => 'form.label_layout',
]],
['class', TextType::class, [
'required' => false,
'label' => 'form.label_class',
]],
['template', ContainerTemplateType::class, [
'label' => 'form.label_template',
]],
],
'translation_domain' => 'SonataBlockBundle',
]);
$formMapper->add('children', CollectionType::class, [], [
'admin_code' => 'sonata.page.admin.block',
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
]);
}
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
return $this->renderResponse($blockContext->getTemplate(), [
'block' => $blockContext->getBlock(),
'decorator' => $this->getDecorator($blockContext->getSetting('layout')),
'settings' => $blockContext->getSettings(),
], $response);
}
public function configureSettings(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'code' => '',
'layout' => '{{ CONTENT }}',
'class' => '',
'template' => '@SonataBlock/Block/block_container.html.twig',
]);
}
public function getBlockMetadata($code = null)
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataBlockBundle', [
'class' => 'fa fa-square-o',
]);
}
/**
* Returns a decorator object/array from the container layout setting.
*/
protected function getDecorator(string $layout): array
{
$key = '{{ CONTENT }}';
if (false === strpos($layout, $key)) {
return [];
}
$segments = explode($key, $layout);
$decorator = [
'pre' => $segments[0] ?? '',
'post' => $segments[1] ?? '',
];
return $decorator;
}
}