vendor/sonata-project/block-bundle/src/Block/Service/ContainerBlockService.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\BlockBundle\Block\Service;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Sonata\BlockBundle\Block\BlockContextInterface;
  14. use Sonata\BlockBundle\Form\Type\ContainerTemplateType;
  15. use Sonata\BlockBundle\Meta\Metadata;
  16. use Sonata\BlockBundle\Model\BlockInterface;
  17. use Sonata\Form\Type\CollectionType;
  18. use Sonata\Form\Type\ImmutableArrayType;
  19. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  20. use Symfony\Component\Form\Extension\Core\Type\TextType;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\OptionsResolver\OptionsResolver;
  23. /**
  24.  * Render children pages.
  25.  *
  26.  * @final since sonata-project/block-bundle 3.0
  27.  *
  28.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  29.  */
  30. class ContainerBlockService extends AbstractAdminBlockService
  31. {
  32.     public function buildEditForm(FormMapper $formMapperBlockInterface $block)
  33.     {
  34.         $formMapper->add('enabled');
  35.         $formMapper->add('settings'ImmutableArrayType::class, [
  36.             'keys' => [
  37.                 ['code'TextType::class, [
  38.                     'required' => false,
  39.                     'label' => 'form.label_code',
  40.                 ]],
  41.                 ['layout'TextareaType::class, [
  42.                     'label' => 'form.label_layout',
  43.                 ]],
  44.                 ['class'TextType::class, [
  45.                     'required' => false,
  46.                     'label' => 'form.label_class',
  47.                 ]],
  48.                 ['template'ContainerTemplateType::class, [
  49.                     'label' => 'form.label_template',
  50.                 ]],
  51.             ],
  52.             'translation_domain' => 'SonataBlockBundle',
  53.         ]);
  54.         $formMapper->add('children'CollectionType::class, [], [
  55.             'admin_code' => 'sonata.page.admin.block',
  56.             'edit' => 'inline',
  57.             'inline' => 'table',
  58.             'sortable' => 'position',
  59.         ]);
  60.     }
  61.     public function execute(BlockContextInterface $blockContextResponse $response null)
  62.     {
  63.         return $this->renderResponse($blockContext->getTemplate(), [
  64.             'block' => $blockContext->getBlock(),
  65.             'decorator' => $this->getDecorator($blockContext->getSetting('layout')),
  66.             'settings' => $blockContext->getSettings(),
  67.         ], $response);
  68.     }
  69.     public function configureSettings(OptionsResolver $resolver)
  70.     {
  71.         $resolver->setDefaults([
  72.             'code' => '',
  73.             'layout' => '{{ CONTENT }}',
  74.             'class' => '',
  75.             'template' => '@SonataBlock/Block/block_container.html.twig',
  76.         ]);
  77.     }
  78.     public function getBlockMetadata($code null)
  79.     {
  80.         return new Metadata($this->getName(), (null !== $code $code $this->getName()), false'SonataBlockBundle', [
  81.             'class' => 'fa fa-square-o',
  82.         ]);
  83.     }
  84.     /**
  85.      * Returns a decorator object/array from the container layout setting.
  86.      *
  87.      * @param string $layout
  88.      *
  89.      * @return array
  90.      */
  91.     protected function getDecorator($layout)
  92.     {
  93.         $key '{{ CONTENT }}';
  94.         if (false === strpos($layout$key)) {
  95.             return [];
  96.         }
  97.         $segments explode($key$layout);
  98.         $decorator = [
  99.             'pre' => $segments[0] ?? '',
  100.             'post' => $segments[1] ?? '',
  101.         ];
  102.         return $decorator;
  103.     }
  104. }