I am writing a module that gives this option to users to put comment on each paragraph.

So I have added a new field to my comment table, called "paragraph_id".Also in comment form I have added a new field which asks users which paragraph do they want to put comment on.

I have done some changes to node.tpl.php. This is part of it:

<div class="content clearfix"<?php print $content_attributes; ?>>
  // We hide the comments and links now so that we can render them later.
  $paragraphs = explode("<p>",render($content));
  $number = count($paragraphs);


   print render($content['links']); 



 print render($paragraphs[0]);

 for ($i=1;$i<$number;$i++) {

    print render($paragraphs[$i]);

      <a href="#commenting"><span>Put comment for Paragraph <?php echo $i?></span><br></a>



 <a id="commenting"><?php print render($content['comments']['comment_form']);

so you can see that there is a link under each paragraph that when they click on it, it will take them to the comment form. So now I want to show the comments that belong to each paragraph under that paragraph.

When I use this:


This is part of what I see:

   ...(Array, 11 elements)

  #sorted(Boolean ) TRUE
   22(Array, 13 elements)
   26(Array, 13 elements)
   25(Array, 13 elements)
   28(Array, 13 elements)

when I check the comment table, I find out that 22,26, 25, .. are cid numbers from comment table. And if I click on for example 22, this is part of what I will see:

    #pre_render(Array, 1 element)
    #entitly_type(String, 7 characters ) comment
    #bundle (String, 20 characters ) comment_node_article
    links (Array, 4 elements)
    #view_mode (String, 4 characters ) full
    #theme (String, 21 characters ) comment__node_article
    #comment (Object) stdClass
    #node (Object) stdClass
    #language (String, 2 characters ) en
    #prefix (String, 24 characters ) <a id="comment-22"></a>
    #weight (Integer) 0
    #children (String, 1520 characters ) <div class="comment comment-by-node-author comm...
    #printed (Boolean) TRUE

And if I click on #comment, this is what I will see:

     cid (String, 2 characters ) 22
      pid (String, 1 characters ) 0
     nid (String, 2 characters ) 22
     uid (String, 1 characters ) 1
     subject (String, 1 characters ) 1
      hostname (String, 9 characters )
      created (String, 10 characters ) 1410041425
   changed (String, 10 characters ) 1410041423
    status (String, 1 characters ) 1
    thread (String, 3 characters ) 01/
    name (String, 6 characters ) gbi222
    mail (String, 0 characters )
    homepage (String, 0 characters )
    language (String, 3 characters ) und
    **paragraph_id (String, 1 characters ) 1**

So for each paragraph , I need to check the paragraph_id and if the number of the paragraph number is equal to the paragraph_id, show that comment under that paragraph.

so after print render($paragraphs[$i]); in my code, write:

           show the comments that its cid is 22.

So now my questions are:

1) How can I say that if the above equality is true, show the comment subeject, author, body, ... for that comment?Because if I write:


it will show all the comments.

2) In this example I knew the cid number of that comment, which was 22, so what can I write instead of '22', in if($i==($content['comments']['comments']['22']['#comments']->paragraph_id))

so it means like any number, so it will do that comparison for all the comments(with their cid number)?

  • Why don't you try to achieve same with contributed module - drupal.org/project/annotator?
    – RajeevK
    Oct 6, 2014 at 19:31
  • so as I saw it was just said that this module makes the webpage annotatable, so you think but installing this module I can provide the option of putting comment on each paragraph? Or how does this module help me? @RajeevK Oct 10, 2014 at 1:21


Browse other questions tagged or ask your own question.