1

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 ?

3 Answers 3

4

If you use Craft::$app->getGlobals()->saveSet($reglages); you are saving the Global's itself (its settings), not its content.

Here, you should use Craft::$app->elements->saveElement($reglages); See below:

$reglages = \craft\elements\GlobalSet::find() ->handle('reglages') ->one(); $entries = [$entry->id]; foreach($reglages->curatedhomepagearticles->all() as $tmpEntry) { $entries[] = $tmpEntry->id; } $entries = array_unique($entries); $entries = array_slice($entries, 0, 10); $reglages->setFieldValue('curatedhomepagearticles',$entries); Craft::$app->elements->saveElement($reglages); 
4

If you are running on Craft 3, as of 3.4.0 (and really since the release of craft 3), setting custom field values directly like

$entry->customField = [id1, id2, id3]; 

is broken. You should instead use the setFieldValue or setFieldValues methods.

in your case, a relationship field can be set like so:

$reglages->setFieldValue(curatedHomepageArticles, [<ARRAY OF ENTRY IDs]); 
1
  • This is a good answer. I marked @oli's post as the official answer for this because I also had a problem with the way I was saving the Global set fields. I should use Craft::$app->elements->saveElement($reglages); Commented Jan 31, 2020 at 11:40
1

Although this response is specific to assets the same principles should apply to any field based on relationships (eg categories, tags, entries, etc):

https://craftcms.stackexchange.com/a/28499/992

1
  • Thank you. I found this thread already and I am not sure why, but it doesn't work. Either the saveSet is not working, or the line juste before. I will make some more tests. Commented Jan 29, 2020 at 9:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.