I have a ModelAdmin
for a SilverStripe website and am a few levels deep in the creation through the CMS. For reasons in this model I need to get the paren->parent of this model being created before the object is created.
I could in theory get the URL and get the item id out of it by splitting the URL however this seems somewhat unreliable.
What is the best way to get the parent->parentID at model creation in the CMS ModelAdmin
?
2
Answers
Look at the problem from another angle:
If you have full access to the code, or can add a simple enhancement, create a session entry that stores the grandparent id at the time the grandparent is accessed, and then retrieve it that way.
It’s open-source so should be a simple task.
Create an observer that listens for when a grandparent ID is loaded, in the observer store that ID, then create a method in the observer to fetch the ID.
If that sounds like a lot of work, use the URL. The URL is safe to use as long as the links are solid. URLs are the cornerstone of routing.
HTH
The following is not a great idea, but frankly there is no good way to do this right now.
You could subclass
GridFieldDetailForm_ItemRequest
(or add an extension to it, perhaps). You can use something like the following logic to get the chain of records that lead to yours. Note that if you were managing your record in aGridField
on a page, this would need some tweaks – but forModelAdmin
it should be good as is (though I haven’t tested it)Then you can just pass that information, however you choose to do so, to
$this->getRecord()
.