0

I'd like when a user comments, to display his information.

Let's assume I do have in my user profile 2 fields, Job title and Biography.

When a user comments, I would like to get those 2 fields and display them as comment fields.(same for existing comments) It will then display the comment + job title + bio

What the best solution for that ?

I was thinking about a preprocess to load the $user but maybe there is some configuration which could do it instead. In this example I added 2 fields in the comment section of my content type then I'm trying to get the user profile infos to the current fields.

function template_preprocess_comment(&$vars) {
      if($vars['comment']->uid !== 0){
        $user = user_load($vars['comment']->uid);
        $vars['field_job_title'] = $user->field_job_title;
        $vars['field_bio'] = $user->field_bio;
      }
}

Final approach was: For the existing one that what I came out with:

function hook_user_presave(&$edit, $account, $category){
    //get all node we need
    $results = db_select('node', 'n')
                  ->fields('n', array('nid'))
                  ->condition('type', 'my_content_type', 'LIKE')
                  ->execute();

    foreach ($results as $result) {
        $nids[] = $result->nid;
    }
    foreach ($nids as $key => $nid) {
        //get all comment ids
        $result = db_select('comment')
            ->fields('comment', array('cid'))
            ->condition('nid', $nid, '=')
            ->condition('uid', $account->uid, '=')
            ->execute();

        $cids = $result->fetchCol();
        $comments=comment_load_multiple($cids);
        foreach($comments as $comment) {
            //load comment
            // $user = user_load($comment->uid);
            if(isset($edit['field_job_title']) && isset($edit['field_bio']) && $account->uid !== 0){
                //change data of the comment
                $comment->field_job_title = $edit['field_job_title'];
                $comment->field_bio = $edit['field_bio'];
                //save it
                comment_save($comment);
            }
        }
    }
}

function hook_comment_presave($comment) {
    //if not user, otherwise it does it when you do the comment_save($comment) from the user_presave, then it won't work
    if(arg(0) !== 'user'){
        $user = user_load($comment->uid);
        if($user->uid != 0){
            $comment->field_job_title = isset($user->field_job_title) ? $user->field_job_title : NULL;
            $comment->field_bio = isset($user->field_bio) ? $user->field_bio : NULL;
        }
    }
}

and this one to hide the current fields when you comment:

function template_form_comment_form_alter(&$form, &$form_state) {
    $form['field_bio']['#access'] = FALSE;
    $form['field_job_title']['#access'] = FALSE;
}
2
  • Do you want to display the info on the comment form or the comment it self?
    – Robin
    Feb 13, 2015 at 14:44
  • on the comment!
    – GwenM
    Feb 13, 2015 at 14:52

2 Answers 2

0

Your solution is ok. But you could also

  • Add a field on the comment fields of your content type that uses a reference to the user
  • On hook_comment_presave fill out the user reference field with the uid of the logged in user
  • On manage display of your comment fields, just render the user field as a rendered entity
  • You could use a custom view mode on user that only shows the fields "job title" and "bio".
9
  • My solution didn't work actually, nothing changed. I'll try the one with the rendered entity
    – GwenM
    Feb 13, 2015 at 15:20
  • Let me know if it does the job :)
    – Robin
    Feb 13, 2015 at 15:22
  • I used field reference but it just didn't want to display the reference...
    – GwenM
    Feb 13, 2015 at 16:03
  • Can you show me a screenshot of your hook_comment_insert?
    – Robin
    Feb 13, 2015 at 17:15
  • 1
    For the existing ones you will have to write a bulk edit function. You can use this query db_query('SELECT * FROM {comments} WHERE nid = %d', $nid); and then set the uid and save the comment: drupal.org/node/1030676
    – Robin
    Feb 16, 2015 at 11:27
0

Here is my solution:

function hook_user_presave(&$edit, $account, $category){
    //get all node we need
    $results = db_select('node', 'n')
                  ->fields('n', array('nid'))
                  ->condition('type', 'my_content_type', 'LIKE')
                  ->execute();

    foreach ($results as $result) {
        $nids[] = $result->nid;
    }
    foreach ($nids as $key => $nid) {
        //get all comment ids
        $result = db_select('comment')
            ->fields('comment', array('cid'))
            ->condition('nid', $nid, '=')
            ->condition('uid', $account->uid, '=')
            ->execute();

        $cids = $result->fetchCol();
        $comments=comment_load_multiple($cids);
        foreach($comments as $comment) {
            //load comment
            // $user = user_load($comment->uid);
            if(isset($edit['field_job_title']) && isset($edit['field_bio']) && $account->uid !== 0){
                //change data of the comment
                $comment->field_job_title = $edit['field_job_title'];
                $comment->field_bio = $edit['field_bio'];
                //save it
                comment_save($comment);
            }
        }
    }
}

function hook_comment_presave($comment) {
    //if not user, otherwise it does it when you do the comment_save($comment) from the user_presave, then it won't work
    if(arg(0) !== 'user'){
        $user = user_load($comment->uid);
        if($user->uid != 0){
            $comment->field_job_title = isset($user->field_job_title) ? $user->field_job_title : NULL;
            $comment->field_bio = isset($user->field_bio) ? $user->field_bio : NULL;
        }
    }
}

and this one to hide the current fields when you comment:

function template_form_comment_form_alter(&$form, &$form_state) {
    $form['field_bio']['#access'] = FALSE;
    $form['field_job_title']['#access'] = FALSE;
}

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.