vendor/sonata-project/admin-bundle/src/Action/AppendFormFieldElementAction.php line 46

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\AdminBundle\Action;
  12. use Sonata\AdminBundle\Admin\AdminHelper;
  13. use Sonata\AdminBundle\Exception\BadRequestParamHttpException;
  14. use Sonata\AdminBundle\Request\AdminFetcherInterface;
  15. use Symfony\Component\Form\FormRenderer;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Twig\Environment;
  20. final class AppendFormFieldElementAction
  21. {
  22.     private AdminHelper $helper;
  23.     private Environment $twig;
  24.     private AdminFetcherInterface $adminFetcher;
  25.     public function __construct(Environment $twigAdminFetcherInterface $adminFetcherAdminHelper $helper)
  26.     {
  27.         $this->helper $helper;
  28.         $this->twig $twig;
  29.         $this->adminFetcher $adminFetcher;
  30.     }
  31.     /**
  32.      * @throws NotFoundHttpException
  33.      */
  34.     public function __invoke(Request $request): Response
  35.     {
  36.         try {
  37.             $admin $this->adminFetcher->get($request);
  38.         } catch (\InvalidArgumentException $e) {
  39.             throw new NotFoundHttpException($e->getMessage());
  40.         }
  41.         $objectId $request->get('objectId');
  42.         if (null === $objectId) {
  43.             $subject $admin->getNewInstance();
  44.         } elseif (\is_string($objectId) || \is_int($objectId)) {
  45.             $subject $admin->getObject($objectId);
  46.             if (null === $subject) {
  47.                 throw new NotFoundHttpException(sprintf(
  48.                     'Unable to find the object id: %s, class: %s',
  49.                     $objectId,
  50.                     $admin->getClass()
  51.                 ));
  52.             }
  53.         } else {
  54.             throw new BadRequestParamHttpException('objectId', ['string''int''null'], $objectId);
  55.         }
  56.         $admin->setSubject($subject);
  57.         $elementId $request->get('elementId');
  58.         if (!\is_string($elementId)) {
  59.             throw new BadRequestParamHttpException('elementId''string'$elementId);
  60.         }
  61.         [, $form] = $this->helper->appendFormFieldElement($admin$subject$elementId);
  62.         $view $this->helper->getChildFormView($form->createView(), $elementId);
  63.         \assert(null !== $view);
  64.         // render the widget
  65.         $renderer $this->twig->getRuntime(FormRenderer::class);
  66.         $renderer->setTheme($view$admin->getFormTheme());
  67.         return new Response($renderer->searchAndRenderBlock($view'widget'));
  68.     }
  69. }