Closed plexus77 closed 7 months ago
I assume when you are saying you add it programmatically you mean you are using parent in the data() ?
If so, thats not the way - see https://github.com/statamic/cms/issues/5457 for context.
You need to append it to the collection structure, something like:
$collection = Collection::findByHandle('handle');
$collectionStructure = $collection->structure()->in('default');
// make a new entry
$entry = Entry::make()
->collection('handle')
->locale('default')
->slug($termSlug)
->published(true)
->data([...]);
// parent of entry
$entryParent = Entry::newQuery()->where('x', 'y');
// after save, update tree
$entry->afterSave(function ($entry) use ($entryParent, $collectionStructure) {
if (!$entryParent) {
$collectionStructure->append($entry);
} else {
$collectionStructure->appendTo($entryParent->id(), $entry->id());
}
$collectionStructure->save();
});
Thanks for the response and context… and yes I was just using the data array… I did have a look in the entry repository class but wasn’t sure how to add it into the structure. Where and how is that information stored? I expected more than just a parent value in order to store the entry’s position in the structure but that’s not present in the MD. Also why should an aftersave be needed? How is this information saved via the admin?
The parent entry in the MD is a legacy thing and a mistake (as Jason mentioned in the linked issue).
The tree structure is held in the collection tree, which is seperate from entries.
Oh the trees directory!! Ok thanks, I think I under stand now....
Yep the trees directory
Bug description
I am programatically creating ordered entries and while the entry MD looks fine the parent/child relationships are not visible on the tree list screen or when editing the child
Collection config
Parent
Child
Is this a bug or is there something I'm missing here?
How to reproduce
When I look at the entries in the tree view there is no parent child relationship, when I edit the child entry there is no parent visible and when I try to add one and save it doesn't work ie there is no relationship visible on the list page and when I return to the edit page it is unset again (even though the parent field is still present in the MD). I drag the child into position press save and then everything seems to work from there on (note that there is no change to the parent field in the md).
If I manually remove the parent field from the md and then use the admin ui to link the entry to it's parent it creates markdown exactly the same as the above.
Logs
No response
Environment
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
None
Additional details
No response