Search code examples
sonata-adminsonata

Sonata Block Bundle Edit form dont save EntityType


This is my Block,

When i save - title is stored buy article is null, where is error ?

class ArticleBlock extends AbstractAdminBlockService
{


    /**
     * {@inheritdoc}
     */
    public function configureSettings(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'article' => null,
            'title' => null,
            'template' => '@MeaArticleBundle/Sonata/Templates/article_block.html.twig',
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
    {
        $formMapper->add('settings', ImmutableArrayType::class, [
            'keys' => [
                ['article', EntityType::class , [
                    'class' => Article::class,
                    'required' => true,
                    'property' => 'title',
                    'label' => 'Article',
                ]],
                ['title', TextType::class, [
                    'label' => 'form.label_title',
                    'required' => false,
                ]],
            ],
            'translation_domain' => 'SonataBlockBundle',
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
        //var_dump($block);

        $errorElement
            ->with('article[article]')
            ->assertNotNull([])
            ->assertNotBlank()
            ->end()
            ->with('title[title]')
            ->assertNotNull([])
            ->assertNotBlank()
//            ->assertLength(['max' => 50])
            ->end();
    }

    /**
     * {@inheritdoc}
     */
    public function execute(BlockContextInterface $blockContext, Response $response = null)
    {
        // merge settings
        $settings = $blockContext->getSettings();

        var_dump([$blockContext,$settings]);

i get after save

 "use_cache" => true
    "extra_cache_keys" => array:2 [▶]
    "attr" => []
    "template" => "@MeaArticleBundle/Sonata/Templates/article_block.html.twig"
    "ttl" => 86400
    "manager" => "snapshot"
    "page_id" => 1
    "article" => []
    "title" => "test2"
  ]

Solution

  • I created a method that maps the ids of the chosen entities to an array:

        private function mapTestimonialsToIds(array $testimonials): array
        {
            $ids = [];
            /** @var Testimonial $testimonial */
            foreach ($testimonials as $testimonial) {
                $ids[] = $testimonial->getId();
            }
    
            return $ids;
        }
    

    and I call this method in both the prePersists and preUpdate methods and set this to my block testimonials setting as value in this case.

    Then, on the execute method, I fetch all entities by using the saved id's and return these found entities to my template.

        public function execute(BlockContextInterface $blockContext, Response $response = null)
        {
            return $this->renderResponse($blockContext->getTemplate(), [
                'testimonials' => $this->testimonialRepository->findByIds($blockContext->getSetting('testimonials')),
                'block' => $blockContext->getBlock(),
            ], $response);
        }