3

I am trying to create a form that anyone can submit regardless if they are logged in or not. I found the Pixel & Tonic Guest Entries plugin for this. Now that I've installed and configured the plugin updating entries doesn't work. Whenever I want to update an entry a new one is created instead.

Here is the code I use:

<form method="post" accept-charset="UTF-8" id="loginForm">
    {{ getCsrfInput() }}    

    <input type="hidden" name="entryId" value="88">        

    <input type="hidden" name="action" value="guestEntries/saveEntry">
    <input type="hidden" name="redirect" value="entryform?hash={{ hash }}">
    <input type="hidden" name="sectionId" value="3">
    <input type="hidden" name="enabled" value="1">

    <input type="hidden" name="title" />

    <label for="text-naamSite">Wat is de naam van uw organisatie/onderneming?</label>
    <input type="text" id="text-naamSite" name="fields[naamSite]" />

    <button type="reset" style="float:left;">Reset</button>

    <button id="send-btn" type="submit">Opslaan</button>

    <div class="clear"></div>
</form>

Thanks!

2 Answers 2

6

In Craft 3 / Guest Entries 2, you could pull this off without hacking Guest Entries by writing a module that listens to the beforeSaveEntry event, and sets the id on the entry.

use Craft;
use craft\elements\Entry;
use craft\guestentries\controllers\SaveController;
use craft\guestentries\events\SaveEvent;
use yii\base\Event;
use yii\web\BadRequestHttpException;

// ...

public function init()
{
    // ...

    Event::on(SaveController::class, SaveController::EVENT_BEFORE_SAVE_ENTRY, function(SaveEvent $event) {
        $request = Craft::$app->getRequest();

        // Get the entry being edited
        $entryId = $request->getValidatedBodyParam('entryId');
        $currentEntry = Entry::find()->id($entryId)->anyStatus()->one();

        if (!$currentEntry) {
            throw new BadRequestHttpException('Invalid entry ID: ' . $entryId);
        }

        // Populate the guest submission with the entry ID, UID, and content ID
        $event->entry->id = $currentEntry->id;
        $event->entry->uid = $currentEntry->uid;
        $event->entry->contentId = $currentEntry->contentId;
    });
}

Then in your form, add an entryId input set to the hashed entry ID.

<input type="hidden" name="entryId" value="{{ entry.id|hash }}">

It’s important that you hash the entry ID and use getValidatedBodyParam() instead of getBodyParam() so users can’t repurpose the form to edit any entry on your site.

4
  • I tried this solution but it throws an "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry" error. I thought maybe it was because Craft 3.1 moved to UID's but changing "$event->entry->id" to "$event->entry->uid" and submitting based on that doesnt seem to help. Any ideas? Commented Mar 8, 2019 at 2:44
  • Thanks to Robin for helping me fix this and updating the answer! Commented Mar 20, 2019 at 15:40
  • This was working beautifully until a Craft update and now it's freezing the site. Any idea why?
    – fxfuture
    Commented Nov 5, 2021 at 18:44
  • @fxfuture Can’t think of any reason it would stop working. Commented Nov 12, 2021 at 15:13
4

I've figured it out

Go to GuestEntriesController.php and add this line of code on the 182nd line

$entry->id = craft()->request->getPost('entryId');

Then add an hidden input field with the id you wish to edit like so:

<input type="hidden" name="entryId" value="{{ entry.id }}">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.