public function createEntry(string $sectionHandle): JsonResponse
    {
        $request = $this->requestStack->getCurrentRequest();

        $optionsResponse = $this->preFlightOptions($request, self::ALLOWED_HTTP_METHODS);
        if ($optionsResponse) {
            return $optionsResponse;
        }

        $this->dispatcher->dispatch(
            ApiCreateEntry::NAME,
            new ApiCreateEntry($request, $sectionHandle)
        );

        if ($abort = $this->shouldAbort($request)) {
            return $abort;
        }

        try {
            $responseData = [ 'code' => JsonResponse::HTTP_OK ];

            /** @var \Symfony\Component\Form\FormInterface $form */
            $form = $this->form->buildFormForSection(
                $sectionHandle,
                $this->requestStack,
                null,
                false
            );
            $form->submit($request->get($form->getName()));

            $jsonResponse = new JsonResponse(
                $responseData,
                $responseData['code'],
                $this->getDefaultResponseHeaders($request)
            );

            if ($form->isValid()) {
                $this->dispatcher->dispatch(
                    ApiBeforeEntrySavedAfterValidated::NAME,
                    new ApiBeforeEntrySavedAfterValidated($request, $responseData, $jsonResponse, $form->getData())
                );
                $abortCode = $request->get('abort');
                if ($abortCode) {
                    return new JsonResponse(
                        $request->get('abortMessage'),
                        $abortCode,
                        $this->getDefaultResponseHeaders($request)
                    );
                }

                $responseData = $this->save($form, $jsonResponse, $request);
                $jsonResponse->setData($responseData);
                $this->dispatcher->dispatch(
                    ApiEntryCreated::NAME,
                    new ApiEntryCreated($request, $responseData, $jsonResponse, $form->getData())
                );

                if ($abort = $this->shouldAbort($request)) {
                    return $abort;
                }
            } else {
                $responseData['errors'] = $this->getFormErrors($form);
                $responseData['code'] = JsonResponse::HTTP_BAD_REQUEST;
                $jsonResponse->setData($responseData);
                $jsonResponse->setStatusCode($responseData['code']);
            }

            return $jsonResponse;
        } catch (\Exception $exception) {
            return $this->errorResponse($request, $exception);
        }
    }
public function createEntry(string $sectionHandle): JsonResponse
    {
        $request = $this->requestStack->getCurrentRequest();

        $optionsResponse = $this->preFlightOptions($request, self::ALLOWED_HTTP_METHODS);
        if ($optionsResponse) {
            return $optionsResponse;
        }

        $this->dispatcher->dispatch(
            ApiCreateEntry::NAME,
            new ApiCreateEntry($request, $sectionHandle)
        );

        if ($abort = $this->shouldAbort($request)) {
            return $abort;
        }

        try {
            $responseData = [ 'code' => JsonResponse::HTTP_OK ];

            /** @var \Symfony\Component\Form\FormInterface $form */
            $form = $this->form->buildFormForSection(
                $sectionHandle,
                $this->requestStack,
                null,
                false
            );
            $form->submit($request->get($form->getName()));

            $jsonResponse = new JsonResponse(
                $responseData,
                $responseData['code'],
                $this->getDefaultResponseHeaders($request)
            );

            if ($form->isValid()) {
                $this->dispatcher->dispatch(
                    ApiBeforeEntrySavedAfterValidated::NAME,
                    new ApiBeforeEntrySavedAfterValidated($request, $responseData, $jsonResponse, $form->getData())
                );
                $abortCode = $request->get('abort');
                if ($abortCode) {
                    return new JsonResponse(
                        $request->get('abortMessage'),
                        $abortCode,
                        $this->getDefaultResponseHeaders($request)
                    );
                }

                $responseData = $this->save($form, $jsonResponse, $request);
                $jsonResponse->setData($responseData);
                $this->dispatcher->dispatch(
                    ApiEntryCreated::NAME,
                    new ApiEntryCreated($request, $responseData, $jsonResponse, $form->getData())
                );

                if ($abort = $this->shouldAbort($request)) {
                    return $abort;
                }
            } else {
                $responseData['errors'] = $this->getFormErrors($form);
                $responseData['code'] = JsonResponse::HTTP_BAD_REQUEST;
                $jsonResponse->setData($responseData);
                $jsonResponse->setStatusCode($responseData['code']);
            }

            return $jsonResponse;
        } catch (\Exception $exception) {
            return $this->errorResponse($request, $exception);
        }
    }
public function createEntry ( string $ sectionHandle ) : JsonResponse { $ request = $ this -> requestStack -> getCurrentRequest ( ) ; $ optionsResponse = $ this -> preFlightOptions ( $ request , self :: ALLOWED_HTTP_METHODS ) ; if ( $ optionsResponse ) { return $ optionsResponse ; } $ this -> dispatcher -> dispatch ( ApiCreateEntry :: NAME , new ApiCreateEntry ( $ request , $ sectionHandle ) ) ; if ( $ abort = $ this -> shouldAbort ( $ request ) ) { return $ abort ; } try { $ responseData = [ 'code' => JsonResponse :: HTTP_OK ] ; /** @var \Symfony\Component\Form\FormInterface $form */ $ form = $ this -> form -> buildFormForSection ( $ sectionHandle , $ this -> requestStack , null , false ) ; $ form -> submit ( $ request -> get ( $ form -> getName ( ) ) ) ; $ jsonResponse = new JsonResponse ( $ responseData , $ responseData [ 'code' ] , $ this -> getDefaultResponseHeaders ( $ request ) ) ; if ( $ form -> isValid ( ) ) { $ this -> dispatcher -> dispatch ( ApiBeforeEntrySavedAfterValidated :: NAME , new ApiBeforeEntrySavedAfterValidated ( $ request , $ responseData , $ jsonResponse , $ form -> getData ( ) ) ) ; $ abortCode = $ request -> get ( 'abort' ) ; if ( $ abortCode ) { return new JsonResponse ( $ request -> get ( 'abortMessage' ) , $ abortCode , $ this -> getDefaultResponseHeaders ( $ request ) ) ; } $ responseData = $ this -> save ( $ form , $ jsonResponse , $ request ) ; $ jsonResponse -> setData ( $ responseData ) ; $ this -> dispatcher -> dispatch ( ApiEntryCreated :: NAME , new ApiEntryCreated ( $ request , $ responseData , $ jsonResponse , $ form -> getData ( ) ) ) ; if ( $ abort = $ this -> shouldAbort ( $ request ) ) { return $ abort ; } } else { $ responseData [ 'errors' ] = $ this -> getFormErrors ( $ form ) ; $ responseData [ 'code' ] = JsonResponse :: HTTP_BAD_REQUEST ; $ jsonResponse -> setData ( $ responseData ) ; $ jsonResponse -> setStatusCode ( $ responseData [ 'code' ] ) ; } return $ jsonResponse ; } catch ( \ Exception $ exception ) { return $ this -> errorResponse ( $ request , $ exception ) ; } }
POST a new entry
@param string $sectionHandle
@return JsonResponse
POST a new entry