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 ?

Gfra54
  • 113
  • 5

3 Answers3

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]);
Nick
  • 141
  • 4
  • 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); – Gfra54 Jan 31 '20 at 11:40
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);
Oli
  • 7,495
  • 9
  • 17
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

Cole Henley
  • 1,741
  • 11
  • 20
  • 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. – Gfra54 Jan 29 '20 at 09:25