Hi there,
is it possible to add more extension hooks to Comment module in one of upcoming versions?
Specifically, I need to be able to modify the ‘where’ condition of ‘main’ SQL query in entries() (around lines 573-586 in mod.comment.php)
Currently entries() contains only one extension hook which does not give much freedom in add-ons development.
Additionally (not as much important, but would be useful) it would be nice to have comment tagdata parser (the code after comment_entries_tagdata hook) moved to separate public function or even library, so it could be re-used
Hi Kevin,
thanks for attention to this.
This is for my Threaded Comments module.
In EE2 version I’ve wrote my own function to fetch comments, but I’m doing complete re-write for EE3 and I think it would be more effective to turn that into extension which will just add extra data to what is returned by {exp:comment:entries} and perform some manipulations. That way, people could be sure that all tags and data provided by Comment module will be also available in Threaded Comments.
This is how I do it EE2 versions: I fetch all comment data, build them into an object, then perform custom sorting on then object and then perform tagdata parsing.
I was thinking about modify the query to include root level comment only, and deal with 2nd level comments with custom code, but what you suggest regaring ability to modify the comments result array might work even better. I would fetch the extra data, perform custom sorting and then parse tagdata (I would be able to modify tagdata as I need depending on comment level, as there is already a hook there). So I only need a new hook to modify the $results array, but it would be perfect if I could also modify $pagination from within it, as as I need pagination to be based on number of root comments only, not total comments.
Thanks!
Yuri
Thanks for the explanation, Yuri. Do you want to try adding this hook to about line 600 (right after we get the result array) and see how it works for you?
if (ee()->extensions->active_hook('comment_entries_query_result') === TRUE)
{
$results = ee()->extensions->call('comment_entries_query_result', $results, $pagination);
if (ee()->extensions->end_script === TRUE) return ee()->TMPL->tagdata;
}Your extension method will take two parameters, the first being the result array, the second being the pagination object which should be passed by reference for you to manipulate. Your method should then return the modified array.
Hi Kevin,
the hook works well, but I figured out it is not sufficient for me. I really need to only fetch root level comments within the first query, otherwise I might get undesired results.
So in addition to this hook, can we add also something like
if (ee()->extensions->active_hook('comment_entries_comment_ids_query_sql_where') === TRUE)
{
$results = ee()->extensions->call('comment_entries_comment_ids_query_sql_where');
if ($results!==NULL)
{
ee()->db->where($results, NULL, FALSE);
}
if (ee()->extensions->end_script === TRUE) return ee()->TMPL->tagdata;
}before the first query at line 537
$query = ee()->db->get();That way I would be able to get only comments with the exact comment_ids that I need, and so modifying $pagination will be not needed.
Btw, passing by reference apparently does not work in extensions, so I wasn’t able to modify pagination.
I got this error:
Parameter 2 to Threaded_comments_ext::modify_comment_results() expected to be a reference, value given
C:/OpenServer/domains/ee3.dev/system/ee/legacy/libraries/Extensions.php, line 209 hide details
Severity: E_WARNINGYou shouldn’t need to use & in front of the parameter name for an object, objects are always passed by reference. Either way, sounds like we need to move the hook just for the sake of an efficient query.
Let’s try this. It looks like you actually need to have the hook around line 508 before the pagination total is set. I’ve implemented this hook for testing:
if (ee()->extensions->active_hook('comment_entries_comment_ids_query') === TRUE)
{
ee()->extensions->call('comment_entries_comment_ids_query', ee()->db);
if (ee()->extensions->end_script === TRUE) return ee()->TMPL->tagdata;
}There, you can see we’re passing in the DB object. Then in your hook implementation, you can do this:
function comment_entries_comment_ids_query($db)
{
$db->where_in('comment_id', array(...));
}That way you don’t have to send back a specially-formatted string and the hook is a bit more flexible for others to use. Let us know how that works out for you.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.