1

My comments have a field called 'Extra'. I'm trying to hide it when the user who's viewing the comment wrote it. This is my custom module:

function mymodule_comment_view($comment) {
  global $user;
  if ($comment->uid == $user->uid){
    unset ($comment->field_extra);
  }
}

Why isn't this working and what's the correct way to achieve my goal?

2 Answers 2

1

Looking at the same kind of functionality in the user.module function user_comment_view() I would expect your code to work.

If your function is really called hook_comment_view then it won't work though. Either this is just a typo, or rename your function to MYMODULE_comment_view where MYMODULE is the name of your custom module.

Edit: After a little discussion with MPD it turns out, that this actually works:

$comment->content['field_extra']['#access'] = FALSE;

Respect to MPD for explanations and an inside for why that could work and especially for why the other solution doesn't work!

This is the phrase from MPD that lead to the solution:

If you read the source for comment_build_comment(), $comment->content[] has already been built up by the time the view hook gets invoked. So, messing with the field directly has no effect at this point.

Which means, that the field output has already been built and stored in $comment->content, which implies that this follows the same rules as in hook_node_view(). So accessing $comment->content[FIELD_NAME] should work.

2
  • Indeed, instead of "hook", I'm using "mymodule". I changed this in my question.
    – Jeroen
    Feb 8, 2014 at 18:51
  • ah, ok, I was just looking at drupal.stackexchange.com/questions/102730/… and figured from there that this was a typo ;) Then I'm baffled too about why it doesn't work. Can you add any information to the comment? Will it be displayed?
    – berliner
    Feb 8, 2014 at 18:53
4

hook_comment_view(), along with most of the other entity view hooks, are typically used to add properties onto the entity object.

If you read the source for comment_build_comment(), $comment->content[] has already been built up by the time the view hook gets invoked. So, messing with the field directly has no effect at this point.

You want to implement a template_process_comment() in your theme, or you can implement a module (which technically makes it an instance of hook_preprocess_comment().

Implement this, and then dpm($variables) inside it. Figure out what you don't want, and then unset() it. If the variable is printed directly from the comments template, this is is directly in $variables. If it is in the content section, then unset the proper key in $variables['content'], which is a render tree.

6
  • So then it should be possible to do $comment->field_extra = ''; as it is done in user_comment_view?
    – berliner
    Feb 8, 2014 at 18:55
  • @berliner Adding your own property, yes, but I am not sure if altering a true field this way is really safe. Mainly I am not sure how field caching comes into play. Also, an empty field is also normally the empty array.
    – mpdonadio
    Feb 8, 2014 at 19:01
  • ok, then your's is the way to go I guess. Thanks for explaining
    – berliner
    Feb 8, 2014 at 19:04
  • 1
    @berliner I filled out my explanation a bit. I had to do some field-level shenanigans on a project a few weeks ago, and ran into some of the same issues.
    – mpdonadio
    Feb 8, 2014 at 19:13
  • 1
    Shouldn't it be possible then to do $comment->content['field_extra']['#access'] = FALSE? That's the way I would do it in a hook_node_view().
    – berliner
    Feb 8, 2014 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.