I want to provide this option in Drupal that users be able to put comment on each paragraph of an article beside putting comment on the whole article.

I use explode command to make an array which contains paragraphs:

$paragraphs = explode("<p>",render($content));

In order to put comment for the whole article I write:

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

I am trying to find the file where $content is defined, so I can add another array like $content[]['paragraphcomments'] for comments for each paragraph of an article. Does anyone know in which file $content is defined. Also does anyone have any suggestion?

  • FYI Never hack core drupal.org/best-practices/do-not-hack-core
    – niksmac
    Commented Jan 18, 2014 at 9:01
  • Thank you. So as I understood the only files I can change are the files inside Sites folder, Am I right? Commented Jan 19, 2014 at 17:27
  • Generally speaking, you should only change files you have created yourself. Changing files from contrib modules can create more or less the same problems as hacking core. Anyway, in most cases it's just not necessary because of the modular architecture of drupal. It's a good practice to create separate subfolders for contrib modules and for your own custom code inside sites/all/modules (or sites/YOURSITE/modules).
    – berliner
    Commented Jan 19, 2014 at 17:58

2 Answers 2


$contentis defined in node.module in the function template_preprocess_node(). You can add elements to the nodes content using hook_node_view()(D7) or hook_nodeapi() (D6).

See the accepted answer of this question for more details.

  • Thank you so much. So do you think I should write a module with hook_node_view function and add element to the node content or I can do that by changing node.module? Commented Jan 19, 2014 at 17:24
  • Don't hack core! Especially if it's not necessary. Create a module and use hook_node_view().
    – berliner
    Commented Jan 19, 2014 at 17:53
  • I checked template_preprocess_node() function in node.module file. but I did not find what I am looking for. I want to know how $content['comments'] is defined, so with hook_node_view I can create a new field like $node->content['comment for each paragraph'] and define it the same way that $node->content['comments] is defined.but make it a 2 dimensional array, so users can put comment on each paragraph of a node body. I would really appreciate it if you can give me some info. Thank you Commented Jan 30, 2014 at 1:38
  • @mery This deserves a new question. I'm not quite sure about your approach. Anyway, this question already has an accepted answer, so you will probably get more attention when you create a new question.
    – berliner
    Commented Jan 30, 2014 at 1:48

You should create a module and can use jquery to shown & insert comments within paragraphs. If you use jquery then you'd need to use Ajax. You should show comment paragraphs in accordion style and allow visitor to comment after expanding it.

You can see simple Drupal developer examples in the example module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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