-
- 14 Posts
I need to add a "related pages" TV on the page so I wrote a custom snippet which lists all sibling resources (excluding the current one). I get the parent of the current resource via $modx->resource->get('parent'); and job done.
This works well when editing a resource, but when creating a new one it doesn't work, as I suspect the resource object is not yet available, even if all the fields in the form are populated. I have tried many different methods in my snippet including:
$resourceId = $modx->resource->get('id');
$resourcetObj = $modx->getObject('modResource',$resourceId);
$resourcetObj->getOne('Parent');
It seems like none of the resource fields (id, pagetitle, parent, etc) are available to get via a snippet before the resource is saved. Possibly what's happening is that the snippet is fired before the form fields are populated.
Does anybody know of a way I can achieve populating a list TV with an @EVAL call to a snippet with the siblings or the resource being created (not edited)? Maybe a plugin?
Thanks!
-
- 24,544 Posts
If you're creating the resource in your code, you can get the ID from the return value from the resource/create processor.
If you're creating the resource in the Manager, it should be fairly easy to do what you want in a plugin attached to the OnDocFormSave event.
The resource object will be available there as $resource. The $id might be populated (I can't remember), but if not you can do this:
$id = $resource->get('id');
-
- 24,544 Posts
It sounds like you're trying to populate the TV before the resource even exists. The resource won't ever have an ID until it's saved to the DB.
The parent field may be populated in a plugin attached to OnDocFormPrerender. You might be able to fill in the TV there. It might take some JavaScript to fill in the TV value, though.
Can I ask why you need a TV at all? It seems like you could use the code in your snippet to do the job on the fly whenever the page is visited. That way the results would always be current, as opposed to the TV, which might be out of date if there's a new sibling created.
-
- 24,544 Posts
When you say "user" do you mean the Manager user who is creating the resource?
-
- 14 Posts
Yes, the manager user manually creating the resource. Sorry for the lack of clarity.
-
- 24,544 Posts
Have you tried just using this:
$parentId = $modx->resource->get('parent');
You don't really need the resource object or its ID to get the siblings, just the parent's ID. The parent field should be filled in since it's known before the form is displayed.
-
- 24,544 Posts
Erm ... how can you tell that the parent field is filled if you can't get it?