-
Hi, I am kinda going down a rabbit hole in Craft 5 doing an upgrade. In Craft 3 I had this code working just fine:
Now in Craft 5 none of the relations are stored. After some digging around I came to this layout but that is not working either but perhaps it is closer to the truth:
Basically what I am trying to do is store an Entry programmatically. This entry contains URL relations. Once I constructed the urlRelations I set them as field values like this:
After that I save the Entry. Upon saving I get no errors or anything just that the entry saved successfully but my URL relations are not there. Is there an easy fix to this? Some more digging seems to tell me that I need the UIDs for each field in the Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I don't think the requirement is new in Craft 5, but one thing missing from this is the $urlRelations = [
'entries' => [],
'sortOrder' => [],
];
foreach ($urls as $i => $url) {
$key = "new-$i";
// Push data into `entries`:
$urlRelations['entries'][$key] = [
'type' => 'urlRelations',
'enabled' => true,
'fields' => [
'href' => [
'type' => 'url',
'value' => $url,
],
'alias' => $relationType,
],
];
// Add to the `sortOrder` array:
$urlRelations['sortOrder'][] = $key;
} See the Matrix field documentation for more info! |
Beta Was this translation helpful? Give feedback.
-
Hi @AugustMiller, Thank you for your reply. That put me in the right direction of ending up to make it work 🥳 There were 3 things I had to change:
My final code is: foreach ($urls as $url) {
$urlRelations['entries']['new:' . $key] = [
'type' => 'urlRelations2',
'enabled' => true,
'fields' => [
'href' => [
'type' => 'url',
'url' => [
''value' => $url
]
],
'alias' => $relationType,
]
];
$urlRelations['sortOrder'][] = 'new:' . $key;
$key++;
} Finally, thank you for the documentation link. I had read a lot of it but did not come across that one, perhaps also not searching for the right phrases. You made my day very happy. Thank you. |
Beta Was this translation helpful? Give feedback.
Hi @AugustMiller,
Thank you for your reply. That put me in the right direction of ending up to make it work 🥳
There were 3 things I had to change:
sortOrder
is required now otherwise it will not save the changestype
value has changed during the migration, not something I noticed. The new name is nowurlRelations2
instead ofurlRelations
. One of those things that can take hours to spot 😅new
placeholder should be changed tonew:
. Notice the colon at the end.My final code is: