Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Currently the hook_comment only allows to alter the values when it is called with op 'form'. When view is called you do not have the opportunity to add fields to the comment object. The patch enables this behavior. Now when you want to add information to a comment you can load it when hook_comment is called with 'view', you no longer have to do it in template.php.
The object_merge function should be put somewhere else if there is support for this functionality. I need it though.
Comment | File | Size | Author |
---|---|---|---|
#5 | comment_45.patch | 522 bytes | Jax |
#4 | comment_44.patch | 509 bytes | Jax |
#1 | comment_43.patch | 885 bytes | Jax |
comment_42.patch | 889 bytes | Jax | |
Comments
Comment #1
Jax CreditAttribution: Jax commentedThe first patch doesn't actually work. This one does.
Comment #2
RobRoy CreditAttribution: RobRoy commentedCode style needs work, using TABs. Also, you should be able to omit that helper function.
Comment #3
dmitrig01 CreditAttribution: dmitrig01 commentedComment #4
Jax CreditAttribution: Jax commentedHere's one without the helper function and without tabs. By the way, the reason this cannot be done with an array_merge like for the 'form' hook is that in the case the $comment variable is an object is stead of an assoc array.
Comment #5
Jax CreditAttribution: Jax commentedYet another solution to the problem. Here I use casts to transform the object to an array and back so that we can use array_merge to combine the old and new comment. Could someone please review this?
Comment #6
killes@www.drop.org CreditAttribution: killes@www.drop.org commentedThis looks like a good idea.
The implementation shows that somebody should go through all the comment hooks and standardize all their arguments and return values to arrays...
Comment #7
Jax CreditAttribution: Jax commentedI started converting everything to assoc arrays and came across the drupal_unpack() function (line 1016) which requires an object. Is there an alternative that takes an array?
Because if I need to convert the $comment to an object before passing it to unpack() and then back to an array it is no better than my patch. Any suggestions?
Comment #8
Jax CreditAttribution: Jax commentedThis view hook is called inside theme_comment_view. So that means that this function receives an object. If we want to convert the object to an assoc array we must look higher up the chain.
The function
is called from
It is in this last function that right before theme('comment_view', $comment) is called there is the call to
which expects an object. So if I change everything to assoc I will have to do the array -> object cast here in stead of in the theme_comment_view function.
Currently drupal_unpack is used in not too many places:
The only way I currenlty see how to fix this is to either ban objects or assocs altogether. Or I can see to modify drupal_unpack to accept an array and modify the session.inc and user.module files as well. Since this is not a small work I would appreciate some feedback on this.
Comment #9
Jax CreditAttribution: Jax commentedcasetracker solves the problem by defining the hook's signature as
Then, wehen comment is passed as an object and no merge happens you can modify the object directly. I noticed this because my patch breaks the casetracker module.
So when $comment is passed as an array and the hook puts it back together with array_merge you cannot modify the array directly (your changes will be lost) but you need to return the new and modified values as an array. When $comment is passed as an object you can modify it directly to make your changes available.
api.drupal.org defines the signature with the ampersand so this bug/feature might break anywhere in the future. I really would like to suggest banning db_fetch_object from the drupal api and use assoc arrays everywhere, or the other way around. But mixing those two together everywhere isn't the brightest idea either.
Comment #10
catchStill applies with fuzz and offset, but looks like it breaks APIs so moving to D6.
Comment #11
catchComment #12
Jax CreditAttribution: Jax commentedA lot of this will go away once PDO is in use with D7. So, for my part this can be closed.
Comment #13
catchNow you can. http://api.drupal.org/api/function/hook_comment_view/7