0

I'd like to make some changes to a new comment programatically. I'm using the following code:

function MYMODULE_comment_presave($comment) {
    $user = user_load($comment->uid);
    $uid = $user->uid;
    // if ($comment->is_new) {
        $comment->field_image['und'][0]['fid'] = $user->field_image['und'][0]['fid'];
        }
    // }
}

I don't know how I can check whether the comment that's about to be saved is new or edited. $comment->is_new doesn't work.

How can I solve this?

0

Use hook_comment_insert() instead, that should work.

  • You'd have to let me know what your logic is inside the hooks, then :) – Ollie Feb 20 '14 at 23:16
  • So, basically, you're trying to display the user avatars on the comments? Try looking into the file_load function. You can probably do this in the comment template file, rather than saving it to the comment entity, as well. In order to do something to new comments only, you'd use hook_comment_insert(), though! – Ollie Feb 21 '14 at 12:05
  • Indeed, that's what I'm trying to do (see: drupal.stackexchange.com/questions/104123/… and drupal.stackexchange.com/questions/104158/…). Maybe file_load can help me in solving the second question. In this case, however, hook_comment_insert really doesn't save any edits to the comment: the field may be an image field, integer field or whatever. – Jeroen Feb 21 '14 at 12:28
  • hook_comment_insert doesn't execute on updates, it is only executed when you insert a new comment. To print the avatar, see my comment on your other question :) – Ollie Feb 21 '14 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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