I am new developing my first ever site in drupal. Along the line, due to the requirements of the project I had to create custom node pages (template files) instead of the default ones in drupal. For example my site has content type 'Article' and the template file for that is page--node--article.tpl.php. In page preprocess, I switch to the right page template with this code

if (isset($variables['node']->type) && !empty($variables['node']->type)) {
  $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type;
}

Now, my problem is how to display comments and comments form. The drupal print render($content['comments']); is not working. I have installed Devel and I can see the comments and comments_form variables in the back end under the Render tab but I do not know how to access and display comments on the front end either by using these variables or another means.

Please help.

EDIT After digging into variables available on the page I was able to get the form to show using

print render($page['content']['system_main']['nodes'][$node->nid]['comments']) 

but when I try to submit a comment an alert comes up saying OK and the comments cannot be found when i check in the back end to approve. Any thoughts pls

Add your theme hook suggestion in Node preprocess hook instead of Page Preprocess

if (isset($variables['node']->type) && !empty($variables['node']->type)) {   $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->type; }

Now $content variable should be available for you.. Please remember Clear Cache

  • I did that but doesn't change a thing. Guess that is because $content variable is not available in this custom node template approach I have taken. However after digging into variables available on the page I was able to get the form to show using print render($page['content']['system_main']['nodes'][$node->nid]['comments']) but anytime I submit an alert comes up saying OK and the comments cannot be found when i check in the back end to approve. Any thoughts pls – swedge218 Nov 10 '14 at 14:39
  • If it is page template then you can only get $page variable – Pravin Ajaaz Nov 10 '14 at 14:44
  • Please check my updated answer – Pravin Ajaaz Nov 10 '14 at 14:46
  • Ya that does some good, I can now use $content. the comment form shows up and comments are submitted except that this alert saying OK still pops up when I submit instead of the page just refreshing straight off. – swedge218 Nov 10 '14 at 15:52

If you are new in Drupal, one of the best tool for building site in Drupal is Panels.

With this module you can create custom pages and override existing pages, like node view, user view pages etc. Best thing with this module is that you don't need to touch code, you can easily build layouts just via UI.

For your problem, for example, with Panels (you need also to enable Page manager module), you can override Node view page, create layout that you need, add variant just for Article content type and in Panel block/region you can add Node content in view mode that you want (you can choose) and below you can add Recent comments and Comment form.

If you need more information, just ask, I will attach screenshots without problem.

Hope this will be helpful.

  • ok, thanks i will look into this next time. I had actually gone far before I found out there was something called panels. however, one question is that is the markup generated with panels responsive? Or can it be made responsive with little effort? I use twitter Bootstrap for my responsive css and would love solutions that can tie to it easily. – swedge218 Nov 10 '14 at 15:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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