Invoking View function from another View handler

TiG
TiG
@tig
2 years ago
184 posts
I have a need to create a new comment as part of the functionality of a menu item handler. So, essentially, this is a view handler that needs to 'invoke' another view handler.

Logically, I want to use the comment_save functionality to create the new comment so as to reuse the official mechanics for creating a new comment (similarly for deleting a comment). Ideally I would call a jrComment_create_comment() function but this does not exist. The only encapsulation of create mechanics is in the view function.

What approach would be best to create a new comment from another view function?


--
TiG

updated by @tig: 04/14/22 09:57:44AM
michael
@michael
2 years ago
7,743 posts
Creation of most submitted forms is handled in a function of the same name but with _save() at the end. Its in those functions that the logic of creating what was submitted takes place.

Most of the logic in the view_jrComment_comment_save function/view revolves around figuring out which module to store the comment as a comment of. Probably in your view handler that will be simpler I suspect because you're going to have more specific parameters.

Once the parameters are figured this function is doing the saving:
 $id = jrCore_db_create_item('jrComment', $_tmp, array('_profile_id' => jrUser_get_profile_home_key('_profile_id')));

I think I'd be looking to figure out what the structure of the comment I wanted to arrive was then just create it in the datastore directly in my function.

The rest of the view_jrComment_comment_save figures out who to notify now that there is a new comment.
TiG
TiG
@tig
2 years ago
184 posts
Michael

I could do it that way but I prefer to future-proof by using extant functionality. Especially since I need to do this to delete a comment too.

Anyway, it seems I can invoke view_jrComment_comment_save() using jrCore_capture_module_view_function(). The jrCore_run_module_view_function() does not work because it cannot find the view_jrComment_comment_save function. Apparently the desired view function needs to be explicitly loaded first.

So what I plan to do is use the create functionality embedded within view_jrComment_comment_save by invoking it using jrCore_capture_module_view_function().

Is this appropriate?

Thanks,
TiG


--
TiG
michael
@michael
2 years ago
7,743 posts
Good job on locating that obscure function. I had to look it up. Its exactly appropriate for what you're trying to do. I did not realize we had that. Useful though, I'll use it now I know it exists if needed.
TiG
TiG
@tig
2 years ago
184 posts
Michael

Thanks for the confirmation. Makes me feel more comfortable that I am on the right track.

Thanks,
TiG


--
TiG
TiG
TiG
@tig
2 years ago
184 posts
Now if there were only a way to stop view_jrComment_comment_save from exiting. My calling view function does successfully invoke the functionality but never gets control back and thus it cannot clean up and return to its invoker. The call to jrCore_capture_module_view_function() completes successfully but never returns from comment_save.


--
TiG
michael
@michael
2 years ago
7,743 posts
Instead of firing it and waiting for it to complete before proceeding you could set up a queue to fire it then continue on perhaps.
TiG
TiG
@tig
2 years ago
184 posts
Michael

Okay, will keep that in mind as I continue. I have a potential workaround that lets me push the comment creation to the very last step and then deal with the JSON return comment_save provides.

Thanks,
TiG


--
TiG

Tags