craftcms / guest-entries

Accept anonymous entry submissions with Craft.
MIT License
106 stars 26 forks source link

Should author be set to logged-in user? #55

Closed timkelty closed 4 years ago

timkelty commented 4 years ago

It is unclear from the docs if the author should be set for new entries if the user is logged in. Doesn't look like it currently does.

timkelty commented 4 years ago

It doesn't look like it it listening for an author/authorId in the request either, so you'd have to do something like this:

        Event::on(
            GuestEntriesSaveController::class,
            GuestEntriesSaveController::EVENT_BEFORE_SAVE_ENTRY,
            function (GuestEntriesSaveEvent $event) {
                $entry = $event->entry;
                $currentUser = Craft::$app->getUser()->getIdentity();

                if ($currentUser) {
                    $entry->authorId = $currentUser->id;
                }
            }
        );
stursby commented 4 years ago

@timkelty Thank you for this! I was also trying to assign the currentUser to the author (with no luck from the front-end <form>). This did the trick!

brandonkelly commented 4 years ago

Yeah the plugin isn’t concerned with the logged in user – the whole point is to provide a save-entry endpoint for guest users; if you have an entry saving form for logged-in users, ideally that is going through the entries/save-entry controller, as demonstrated by the Entry Form example in the docs.

timkelty commented 4 years ago

@brandonkelly yep - I think the use-case here is having a form that can do either - submit as a user or anonymous.

So, you could either do as I did above, or toggle the action to entries/save-entry in your template based on currentUser.