I work with Craft 3.
I have an Entries field in a global set, I am able to get the global set, and then "read" this field programmatically (in a plugin) to fetch the entries that where added to it by looping on the EntryQuery
data.
I want to be able to add another entry to this field via a php function in my plugin (so not in a Twig template), and then save the it, in the global set. The documentation is not very concrete on this part. Thanks
here is my code :
$reglages = \craft\elements\GlobalSet::find()
->handle('reglages')
->one();
$entries = [$entry->id];
foreach($reglages->curatedHomepageArticles as $tmpEntry) {
$entries[] = $tmpEntry->id;
}
$entries = array_unique($entries);
$entries = array_slice($entries, 0, 10);
$reglages->curatedHomepageArticles = $entries;
$return = Craft::$app->getGlobals()->saveSet($reglages);
var_dump($return);
The point is to add the $entry
to the entry field called curatedHomepageArticles
with no duplicates. I am able to read the entries that are already in the entry field, and then build the array of ids called $entries
but nothing happens when I try to update the field with a new list of entries id $entries
. $return
is true
and there are no exceptions thrown. Can you help me understand what is going on here ?