0

I have added a new field to comment table, called "paragraph_id", I have also added a field to comment form that ask users which paragraph they want to put comment on and what they enter will be saved in "paragraph_id" field.

So now I want to show the value of "paragraph_id" in the comments, beside showing the subject and the body of the comment.

There is a hook called "hook_comment_view" , and it can be used to add additional data to the comment before theming.

This is the hook:

function hook_comment_view($comment, $view_mode, $langcode) {
  // how old is the comment
  $comment->time_ago = time() - $comment->changed;
}

I tested this hook by changing it to the following:

function Annotations_comment_view($comment, $view_mode, $langcode) {
  // how old is the comment
  $comment->subject= $comment->paragraph_id;
}

and instead of subject it showed the data in paragraph_id field. But how can I add a new field to comment display form, for example called : Comment on Paragraph

and then write this:

function Annotations_comment_view($comment, $view_mode, $langcode) {
  // how old is the comment
  $comment->Comment on Paragraph = $comment->paragraph_id;
}
0

First, PHP is case sensitive - don't start your function names with capital letters, it's bad practice, capitals are generally reserved for Classes.

Secondly, if you haven't already, install the devel module, it's really useful.

Then you can look at the structure of the $comment object that is passed to the hook by putting dpm($comment); inside your hook. Make sure you give yourself the permission to view devel information.

Once you can see the structure of the variable it should make sense what drupal is doing with it. You can add a new element to the form simply by editing the $comment variable:

$comment[#myNewElement] = array(
  'property1' => 'value1',
  'property2' => 'value2',
  'property3' => 'value3',
);

You can learn about the various form elements and their properties at the Drupal form API.

You will probably need to query the database to get your display value before you set the element.

Happy coding!

3
  • Thanks for your help. I have already used this hook and added a new field to comment form that asks the user which paragraph he wants to put comment on. So when a user wants to put a comment , he has to enter Subject, and write the comment and also enter the number of the paragaraph he is putting comment on, But when the comment is displayed, he can only see the Subject and the body of the comment, I want him to also see the number he entered as the paragraph number.So how can I show that? – user3530082 Oct 27 '14 at 6:14
  • But as I mentioned if I write function Annotations_comment_view($comment, $view_mode, $langcode) { // how old is the comment $comment->subject= $comment->paragraph_id; } then instead of subject it will show that paragraph number.So I need to add a new field to the comment display form. and then write : function Annotations_comment_view($comment, $view_mode, $langcode) { // how old is the comment $comment->new_added_field= $comment->paragraph_id; } so there is no need to query database.@Darvanen – user3530082 Oct 27 '14 at 6:20
  • If that's all that's required, one way to do it would be to edit the template file in your theme. – Darvanen Oct 27 '14 at 22:40

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.