public function actionIndex()
    {
        $this->requirePostRequest();

        // Only allow front end requests
        $request = Craft::$app->getRequest();
        if (!$request->getIsSiteRequest()) {
            throw new NotFoundHttpException();
        }

        // Make sure the section exists
        $sectionId = $request->getBodyParam('sectionId');
        $sectionUid = $request->getBodyParam('sectionUid');
        $sectionHandle = $request->getBodyParam('sectionHandle');

        $sectionService = Craft::$app->getSections();

        if ($sectionHandle) {
            $section = $sectionService->getSectionByHandle($sectionHandle);
        } else if ($sectionUid) {
            $section = $sectionService->getSectionByUid($sectionUid);
        } else {
            $section = $sectionService->getSectionById((int) $sectionId);
        }

        if (!$section) {
            throw new BadRequestHttpException('Section does not exist.');
        }

        $sectionUid = $section->uid;

        // Make sure the section allows guest submissions
        $settings = Plugin::getInstance()->getSettings();
        $sectionSettings = $settings->getSection($sectionUid);

        if (!$sectionSettings->allowGuestSubmissions) {
            throw new BadRequestHttpException('Section '.$section->handle.' does not allow guest submissions.');
        }

        // Populate the entry
        $entry = $this->_populateEntryModel($section, $sectionSettings, $request);

        // Fire an 'onBeforeSave' event
        $event = new SaveEvent(['entry' => $entry]);
        $this->trigger(self::EVENT_BEFORE_SAVE_ENTRY, $event);

        if (!$event->isValid) {
            return $this->_returnError($settings, $entry);
        }

        if ($event->isSpam) {
            Craft::info('Guest entry submission suspected to be spam.', __METHOD__);
            // Pretend it worked.
            return $this->_returnSuccess($entry, true);
        }

        // Try to save it
        if ($sectionSettings->runValidation) {
            $entry->setScenario(Element::SCENARIO_LIVE);
        }

        if (!Craft::$app->getElements()->saveElement($entry)) {
            return $this->_returnError($settings, $entry);
        }

        return $this->_returnSuccess($entry);
    }
public function actionIndex()
    {
        $this->requirePostRequest();

        // Only allow front end requests
        $request = Craft::$app->getRequest();
        if (!$request->getIsSiteRequest()) {
            throw new NotFoundHttpException();
        }

        // Make sure the section exists
        $sectionId = $request->getBodyParam('sectionId');
        $sectionUid = $request->getBodyParam('sectionUid');
        $sectionHandle = $request->getBodyParam('sectionHandle');

        $sectionService = Craft::$app->getSections();

        if ($sectionHandle) {
            $section = $sectionService->getSectionByHandle($sectionHandle);
        } else if ($sectionUid) {
            $section = $sectionService->getSectionByUid($sectionUid);
        } else {
            $section = $sectionService->getSectionById((int) $sectionId);
        }

        if (!$section) {
            throw new BadRequestHttpException('Section does not exist.');
        }

        $sectionUid = $section->uid;

        // Make sure the section allows guest submissions
        $settings = Plugin::getInstance()->getSettings();
        $sectionSettings = $settings->getSection($sectionUid);

        if (!$sectionSettings->allowGuestSubmissions) {
            throw new BadRequestHttpException('Section '.$section->handle.' does not allow guest submissions.');
        }

        // Populate the entry
        $entry = $this->_populateEntryModel($section, $sectionSettings, $request);

        // Fire an 'onBeforeSave' event
        $event = new SaveEvent(['entry' => $entry]);
        $this->trigger(self::EVENT_BEFORE_SAVE_ENTRY, $event);

        if (!$event->isValid) {
            return $this->_returnError($settings, $entry);
        }

        if ($event->isSpam) {
            Craft::info('Guest entry submission suspected to be spam.', __METHOD__);
            // Pretend it worked.
            return $this->_returnSuccess($entry, true);
        }

        // Try to save it
        if ($sectionSettings->runValidation) {
            $entry->setScenario(Element::SCENARIO_LIVE);
        }

        if (!Craft::$app->getElements()->saveElement($entry)) {
            return $this->_returnError($settings, $entry);
        }

        return $this->_returnSuccess($entry);
    }
public function actionIndex ( ) { $ this -> requirePostRequest ( ) ; // Only allow front end requests $ request = Craft :: $ app -> getRequest ( ) ; if ( ! $ request -> getIsSiteRequest ( ) ) { throw new NotFoundHttpException ( ) ; } // Make sure the section exists $ sectionId = $ request -> getBodyParam ( 'sectionId' ) ; $ sectionUid = $ request -> getBodyParam ( 'sectionUid' ) ; $ sectionHandle = $ request -> getBodyParam ( 'sectionHandle' ) ; $ sectionService = Craft :: $ app -> getSections ( ) ; if ( $ sectionHandle ) { $ section = $ sectionService -> getSectionByHandle ( $ sectionHandle ) ; } else if ( $ sectionUid ) { $ section = $ sectionService -> getSectionByUid ( $ sectionUid ) ; } else { $ section = $ sectionService -> getSectionById ( ( int ) $ sectionId ) ; } if ( ! $ section ) { throw new BadRequestHttpException ( 'Section does not exist.' ) ; } $ sectionUid = $ section -> uid ; // Make sure the section allows guest submissions $ settings = Plugin :: getInstance ( ) -> getSettings ( ) ; $ sectionSettings = $ settings -> getSection ( $ sectionUid ) ; if ( ! $ sectionSettings -> allowGuestSubmissions ) { throw new BadRequestHttpException ( 'Section ' . $ section -> handle . ' does not allow guest submissions.' ) ; } // Populate the entry $ entry = $ this -> _populateEntryModel ( $ section , $ sectionSettings , $ request ) ; // Fire an 'onBeforeSave' event $ event = new SaveEvent ( [ 'entry' => $ entry ] ) ; $ this -> trigger ( self :: EVENT_BEFORE_SAVE_ENTRY , $ event ) ; if ( ! $ event -> isValid ) { return $ this -> _returnError ( $ settings , $ entry ) ; } if ( $ event -> isSpam ) { Craft :: info ( 'Guest entry submission suspected to be spam.' , __METHOD__ ) ; // Pretend it worked. return $ this -> _returnSuccess ( $ entry , true ) ; } // Try to save it if ( $ sectionSettings -> runValidation ) { $ entry -> setScenario ( Element :: SCENARIO_LIVE ) ; } if ( ! Craft :: $ app -> getElements ( ) -> saveElement ( $ entry ) ) { return $ this -> _returnError ( $ settings , $ entry ) ; } return $ this -> _returnSuccess ( $ entry ) ; }
Saves a guest entry.

@return Response|null
@throws BadRequestHttpException if it's not a post request or the requested section doesn't exist/allow guest submissions
@throws NotFoundHttpException if it's not a front end request
Saves a guest entry .