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.

CommentFileSizeAuthor
#5 comment_45.patch522 bytesJax
#4 comment_44.patch509 bytesJax
#1 comment_43.patch885 bytesJax
comment_42.patch889 bytesJax
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jax’s picture

FileSize
885 bytes

The first patch doesn't actually work. This one does.

RobRoy’s picture

Code style needs work, using TABs. Also, you should be able to omit that helper function.

dmitrig01’s picture

Version: 5.1 » 6.x-dev
Jax’s picture

Status: Needs work » Needs review
FileSize
509 bytes

Here'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.

Jax’s picture

FileSize
522 bytes

Yet 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?

killes@www.drop.org’s picture

This 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...

Jax’s picture

I 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?

Jax’s picture

This 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

function theme_comment_view($comment, $links = array(), $visible = 1)

is called from

function theme_comment_flat_collapsed($comment)
function theme_comment_flat_expanded($comment)
function theme_comment_thread_collapsed($comment)
function theme_comment_thread_expanded($comment)
function comment_reply($nid, $pid = NULL)

It is in this last function that right before theme('comment_view', $comment) is called there is the call to

 $comment = drupal_unpack($comment);

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:

$ grep drupal_unpack * -r
includes/bootstrap.inc:function drupal_unpack($obj, $field = 'data') {
includes/session.inc:    $user = drupal_unpack($user);
modules/user/user.module:    $user = drupal_unpack($user);
modules/comment/comment.module:  $comment = drupal_unpack($comment);
modules/comment/comment.module:          $comment = drupal_unpack($comment);
modules/comment/comment.module:        $comment = drupal_unpack($comment);
modules/comment/comment.module:    $comment = drupal_unpack($comment);
$

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.

Jax’s picture

casetracker solves the problem by defining the hook's signature as

function casetracker_comment(&$comment, $op) {

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.

catch’s picture

Status: Needs review » Needs work

Still applies with fuzz and offset, but looks like it breaks APIs so moving to D6.

catch’s picture

Version: 6.x-dev » 7.x-dev
Jax’s picture

A lot of this will go away once PDO is in use with D7. So, for my part this can be closed.

catch’s picture

Status: Needs work » Closed (works as designed)