By frdesign on
So I figured out how to use hook_comment to do things to each comment. For instance I'm using the Vote up/down module. Using hook_comment I check for content type so I can activate Vote Up Down only on the module types of my choosing, something that the module doesn't offer out of the box. Now I want the comments with the highest Vote Up Down rating to float to the top. Is there a hook or something I can use to change how comments are ordered when viewed?
Thanks
Comments
_
If the comments are displayed using Views, just edit the view and change the sort criteria...
Pete.
Possible solution
Hey Pete,
I'm actually trying to alter the way comments are shown in a node. It doesn't look like it uses a view. But it does seem to use a query however, which when you think about it is really the same thing.
After some digging I found hook_db_rewrite_sql() which seems promising so far. I looked at the comments module and I think I found the query that dictates how the comments are ordered. Here's the snippet from the core comments module.
I think if I can alter the query by adding a LEFT JOIN to the "votingapi_cache" table which has the total votes record and ORDER BY VOTE COUNT might do the trick. I would add a conditional statement so it only makes the change to my custom node type.
Does anyone see any potential issues with this approach, it I get it to work that is.
_
Looks OK to me; I've done similar things when Views 1 couldn't quite do what I wanted it to...
Pete.
So close yet so far!
So I was able to capture the query before rendering by using hook_db_rewrite_sql(). This is the query being executed on my node to show comments.
Using hook_db_rewrite_sql() I was able to add a join to the {votingapi_cache} table which has the voting data I want to order by. Excuse the messy code, just starting to get the hang of php.
Now that I have the join I want to change the order by statement from the original query
TO
Is this possible? (fingers crossed)
Thanks in advance to anyone who can help!!!
Alternate solution
In case anyone ever runs into this, after more digging I found that hook_db_rewrite_sql() doesn't let me add ORDER BY statements which is rather surprising. I guess I've been spoiled by the amount of flexibility in other hooks. For anyone that might be interested I had to come up with a custom solution which is probably not ideal but it gets the job done. I wrote a small module which clones the query core uses to build the comment objects, I then changed it as desired, and forced the results of the new query to override the core results by using hook_comment().
Here's the code, I've been coding less than a year so I'm sure it could be better.