Networking\InitCmsBundle\Controller\LayoutBlockController::createAction PHP Method

createAction() public method

public createAction ( ) : Response
return Symfony\Component\HttpFoundation\Response
    public function createAction()
    {
        if (!$this->isXmlHttpRequest()) {
            return new Response('cannot load external of page module', 403);
        }
        if (false === $this->admin->isGranted('CREATE')) {
            throw new AccessDeniedException();
        }
        $layoutBlock = $this->admin->getNewInstance();
        /** @var Request $request */
        $request = $this->get('request_stack')->getCurrentRequest();
        $this->admin->setSubject($layoutBlock);
        $elementId = $request->get('elementId');
        $code = $request->get('code');
        $objectId = $request->get('objectId');
        $uniqid = $request->get('uniqid');
        $classType = $request->get('classType');
        /** @var \Sonata\AdminBundle\Admin\Admin Interface $pageAdmin */
        $pageAdmin = $this->container->get($code);
        $pageAdmin->setRequest($request);
        if ($uniqid) {
            $pageAdmin->setUniqid($uniqid);
        }
        $page = $this->get('networking_init_cms.page_manager')->find($objectId);
        if ($objectId && !$page) {
            throw new NotFoundHttpException();
        }
        if (!$page) {
            throw new NotFoundHttpException();
        }
        $request->attributes->add(array('objectId' => $objectId));
        $request->attributes->add(array('page_locale' => $page->getLocale()));
        $pageAdmin->setSubject($page);
        $layoutBlock->setZone($request->get('zone'));
        $layoutBlock->setSortOrder($request->get('sortOrder'));
        $layoutBlock->setClassType($classType);
        $layoutBlock->setPage($page);
        /** @var $form \Symfony\Component\Form\Form */
        $form = $this->admin->getForm();
        $form->setData($layoutBlock);
        if ($this->getRestMethod() == 'POST') {
            $form->submit($this->get('request'));
            $isFormValid = $form->isValid();
            try {
                // persist if the form was valid and if in preview mode the preview was approved
                if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
                    $this->admin->create($layoutBlock);
                    if ($this->isXmlHttpRequest()) {
                        return $this->renderJson(array('result' => 'ok', 'status' => 'success', 'message' => $this->translate('message.layout_block_created'), 'layoutBlockId' => $this->admin->getNormalizedIdentifier($layoutBlock), 'zone' => $layoutBlock->getZone(), 'sortOder' => $layoutBlock->getSortOrder(), 'html' => $this->getLayoutBlockFormWidget($objectId, $elementId, $uniqid)));
                    }
                }
            } catch (ModelManagerException $e) {
                $formError = new FormError($e->getMessage());
                $form->addError($formError);
            }
        }
        $view = $form->createView();
        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
        return $this->render('NetworkingInitCmsBundle:PageAdmin:layout_block_edit.html.twig', array('action' => 'create', 'form' => $view, 'object' => $layoutBlock, 'code' => $code, 'classType' => $request->get('classType'), 'objectId' => $objectId, 'uniqid' => $uniqid, 'elementId' => $elementId));
    }