Saving future/pending entries to field in custom plugin #9831
-
I am having a few issues with updating an entries field with pending (entries with a future post date) within a custom plugin. I'm not sure if the issue is in the setFieldValue or saveElement function. I have had a good look online, in the docs and asked the community without any luck so far. I was also trying to look for a way to simply add one more entry to a field without having to re-add any existing entries assigned to the field. Is this possible? Condensed code below for a short reference: $videos = $subscription->subVideos->ids();
$video = Entry::findOne(['section' => 'videos', 'relatedTo' => $channel->id, 'after' => $dateTomorrow, 'status' => 'enabled', 'orderBy' => 'postDate asc']);
if (!in_array($video->id, $videos)) {
array_push($videos, $video->id);
}
$subscription->setFieldValue([
'subVideos' => $videos
]);
$saveSubscription = Craft::$app->elements->saveElement($subscription); Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Your $subscription->setFieldValue('subVideos', $videos); You would only pass an array of field handle/value mappings if you were calling $subscription->setFieldValues([
'subVideos' => $videos,
// ...
]); |
Beta Was this translation helpful? Give feedback.
Your
setFieldValue()
syntax is wrong. The first argument should be the field handle, and the second argument should be the array of IDs:You would only pass an array of field handle/value mappings if you were calling
setFieldValues()
(plural) instead: