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;
}