1

I'm building a cinema program, and I'm trying to build entries for movies and shows loading external data via a plugin.

I'm getting close, but now I've hit a roadblock trying to save the entries. I build an array of the data to set as content that looks something like this:

$movie = [
  'movieID'       => (int)$ctx_movie['MOVIE_ID'],
  'title'         => $ctx_movie['VERANSTALTUNGSTITEL'],
  'audioLanguage' => $ctx_movie['SPRACHVERSION'],
  'country'       => $ctx_movie['COUNTRY'],
  'director'      => $ctx_movie['DIRECTOR']
]

Then I make an entry model, and try to set the content to the above array:

$entry = new EntryModel();
$entry->sectionId = craft()->sections->getSectionByHandle('movies')->id;
$entry->typeId    = craft()->sections->getSectionByHandle('movies')->type('movie')->id;
$entry->authorId  = craft()->userSession->getUser()->id;
$entry->enabled   = true;

// This doesn't work:
$entry->setContentFromPost($movie);

// This seems to work, creating an entry and populating fields:
// $entry->setContent($movie);

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

I keep getting this error for every entry:

array ( 'title' => array ( 0 => 'Name cannot be blank.', ), )

The value I set to the 'title' element definitely has a value, and even if I hard code it to be a dummy string, it still fails in the same way.

It seems to work when using setContent($movie) instead of setContentFromPost($movie), but from what I've read the latter is the correct (or best practice) way to do it.

I'm guessing it has something to do with the extra validation, but I can't for the life of me understand why a simple string like 'Dummy title' fails the same way as my variable. The name setContentFromPost throws me off a bit, as I'm unsure if it means the data should come via a post request, but it seems to be fine with my other variables, and from the documentation they seem to be used in the same way, passing an array of values.

(On a side note, I got a similar error for a field that is of type dropdown. I guess here I just haven't understood what value to pass to a dropdown fieldType, so if someone has any tips here too, that would also be great, but I assume it may be two different issues.)

6

I believe that setContentFromPost can only be used to set the content of custom fields on an EntryModel, not standard fields such as title.

Instead, you need to use $entry->getContent()->title to set the title on the EntryModel.

So your code would look like this:

$movie = [
  'movieID'       => (int)$ctx_movie['MOVIE_ID'],
  'audioLanguage' => $ctx_movie['SPRACHVERSION'],
  'country'       => $ctx_movie['COUNTRY'],
  'director'      => $ctx_movie['DIRECTOR']
];

$entry = new EntryModel();
$entry->sectionId = craft()->sections->getSectionByHandle('movies')->id;
$entry->typeId    = craft()->sections->getSectionByHandle('movies')->type('movie')->id;
$entry->authorId  = craft()->userSession->getUser()->id;
$entry->enabled   = true;

// Set the title directly on the $entry
$entry->getContent()->title = $ctx_movie['VERANSTALTUNGSTITEL'];

// This should now work:
$entry->setContentFromPost($movie);

$success = craft()->entries->saveEntry($entry);
|improve this answer|||||
  • Thanks, Steve! I actually tried this, but then it fails with an exception, as the title fields is read-only. It actually works to buypass that with $entry->getContent()->title, but from what I read, this is basically the hack workaround of setting variables that was discouraged, and it was recommeded to use $entry->setContentFromPost() instead. It's weird, then, that the mandatory title element can't be set neither directly nor via these functions. – KSP Oct 24 '16 at 13:13
  • Yeah, you're right, you do need to use $entry->getContent()->title to set the title. I'll edit my answer to reflect this. It's still best to set the other fields using setContentFromPost(). – Steve Rowling Oct 24 '16 at 13:16
  • Great, I've marked it as answered, and will use this in my code for now. Thanks! If anyone has any additional input as to why it is like that, I'd still be interested to learn more, or if there are alternative ways. Just seems weird that a mandatory field cannot use either of the two methods, and must use a method that is otherwise labeled as "hack" or "workaround". – KSP Oct 24 '16 at 13:26

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.