1

So, i'm trying to set a fields content via a plugin on an entry which already exists.

My code is like this:

$entry = craft()->entries->getEntryById($entryId);

$entry->setContentFromPost(array(
    'subTitle' => 'woot'
));

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

I get no error and the value of $success is false but it tells me nothing else. If I var_dump($entry) after the first line, the output shows it has pulled the correct entry.

  • I think setContentFromPost might be intended for when using forms with $_POST data (there are some underlying functions that trigger when using this method) . Have you tried just $entry->setContent(array())? – Alec Ritson Jan 10 '17 at 16:41
  • I tried this: $entry->getContent()->subTitle = "woot" but didn't try set content! Thanks! – John Jan 10 '17 at 22:09
  • 1
    Do you get anything if you call $entry->getErrors() after calling saveEntry()? – Brad Bell Jan 11 '17 at 3:41
  • @BradBell Aha! There are validation errors, clearly some fields have been made required after the initial entry was saved. Thanks very much Brad!!! – John Jan 11 '17 at 8:56
1

I haven't used getEntryById, and this isn't exactly an answer, but here is how I am doing pretty much the same thing:

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = 'mySection');
$criteria->status = null;
$criteria->limit = 1;
$entry = $criteria->first();

...
if ($entry) {
    $entry->setContentFromPost([
        'myFieldHandle' => 'woot'
    ]);

    craft()->entries->saveEntry($entry);
} else {
    // Could not find entry.
}

...

Unless you are setting the page title, then I think you need to use something like:

...
$entry->getContent()->title = 'woot';
| improve this answer | |
1

The problem was that there were validation failures that were silently failing. The mighty Brad's suggestion of $entry->getErrors() after calling saveEntry() let me see what the issue was.

Thanks Brad!

| 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.