3

I have created a front-end form to facilitate editorial review of user submitted content. I have created a controller action to receive the post.

My question, how do I locate the email address of the author tied to this entry? My objective is to be able to email the author with the results of the editorial review. In order to accomplish this I need to know the user (author) email address etc. Here is my code.

$this->requirePostRequest();

$entryId = craft()->request->getRequiredPost('entryId');
$editorialNotesInternal = craft()->request->getPost('fields.editorialNotesInternal');
$editorialScore = craft()->request->getPost('fields.editorialScore');
$editorialNotes = craft()->request->getPost('fields.editorialNotes');
$sectionId = craft()->request->getPost('sectionId');

$entry = new EntryModel();

$entry->id = $entryId;
$entry->sectionId = $sectionId;

if ( $editorialScore >= 3) {
    $entry->enabled = true;
} else {
    $entry->enabled = false;
}   

$entry->getContent()->editorialScore = $editorialScore;
$entry->getContent()->editorialNotes  = $editorialNotes;
$entry->getContent()->editorialNotesInternal  = $editorialNotesInternal;

$success = craft()->entries->saveEntry($entry);
3

I assume you're talking about getting the email address of the currently logged in user? That's what EntryModel's authorId property will get set to, if one isn't explicitly supplied.

If so, you can do that with:

$email = craft()->userSession->getUser()->email;

If you want to get the email address of the user that is the author of the entry, then you can do:

$email = $entry->getAuthor()->email;
|improve this answer|||||
  • No, I want to get the email address of the user that created the entry. – David A McInnis Sep 22 '14 at 22:20
  • Updated the answer. – Brad Bell Sep 22 '14 at 22:23
  • Given my code above. I keep getting a message. "Trying to get property of non-object" which leads me to believe that I do not have the current entry loaded. If true, how do I load the entry? – David A McInnis Sep 22 '14 at 22:58
  • If you're getting that error with your code above, then you haven't explicitly set an author on the entry, yet. If you call saveEntry, without doing that, then craft()->userSession->getUser() will be assigned as the author and you can access that user via $entry->getAuthor() after you've called saveEntry(). – Brad Bell Sep 23 '14 at 0:12
1

I think I found my solution. To recap, I have a front-end form where a user that is not the author is permitted to make changes to an existing entry. I was creating a new Entry model instead of retrieving the Entry that I wanted to update. The answer getEntryById().

$this->requirePostRequest();

$entryId = craft()->request->getRequiredPost('entryId'); 
//entryId passed to controller from front-end form
$entry = craft()->entries->getEntryById($entryId, $localeId = null);

Then reset values that I am changing.

if ( $editorialScore >= 3) {
$entry->enabled = true;
} else {
$entry->enabled = false;
    }   
    $entry->getContent()->editorialScore = $editorialScore;
    $entry->getContent()->editorialNotes  = $editorialNotes;

Follow that with

$success = craft()->entries->saveEntry($entry);

I am then able to get Author's email address using Brad's suggested

$email = $entry->getAuthor()->email;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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