1

I am building a drupal site using drupal 7 and the adaptivetheme.

I created some a content type called recipes. Than I created a content pane in views to present recipes in panels. I add the comments and comments form to the same panel.

Now I want to present beneath any recipe a comment form and all other comments. My problem is that I don't know how to edit the presentation of the comment form. When I checked the comment fields on my content type, I saw only the field "comment_body".

How can I edit the fields and the way they will show on Comments & Comments form?

Below is a screenshot.
I want to remove the field "your name", and change the button "save" to "post comment". How can I do that?

enter image description here

Thanks!

3

You can copy the file called comment.tpl.php from the folder where base AT theme is located (something like adaptivetheme\at_core\templates) and paste it into sites\all\themes\YOUR_AT_SUBTHEME\templates.

Assuming recipes is the machine name of your content type, you can also name it comment--recipes.tpl.php if you want to theme specifically comments for recipes.

Then with the help of comment.tpl.php API (you can also find a lot of information in the php file itself), you can edit the way your comments will be displayed.

I.e instead of

print render($content);

You can display specific fields :

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

or hide specific fields

hide($content['author']);
print render($content);

To alter the form itself, you need to use hook_form_alter() in a custom module.

/**
 * Implements hook_form_alter().
 */
function YOUR_CUSTOM_MODULE_form_alter(&$form, &$form_state, $form_id) {
  //get the form id
  drupal_set_message("Form ID is : " . $form_id);
  $print = '<pre>' . print_r($form, TRUE) . '</pre>';
  drupal_set_message($print);

  switch($form_id){
    case 'YOUR_COMMENT_FORM_ID':
      //alter your form (with the complete correct fields names)
      $form['YOUR_FIELD_YOUR_NAME']['#type'] = 'hidden';
      $form['YOUR_FIELD_SUBMIT']['#value'] = t('Post comment');
    break;
  }
}

You can also use dpm($form) if you have Devel installed, or make a

$print = '<pre>' . print_r($form, TRUE) . '</pre>';
drupal_set_message($print);

in order to retrieve the exact form fields

  • Thanks! How can I access the comment form and edit the way the form appear (I want to remove the "your name" field)? – EB84 Jan 17 '14 at 21:29
  • I managed to do it! Thank you! I have one questions regarding this process - is it will cause my site performance to be slower, since on every-page the PHP will scan all ID etc..? – EB84 Jan 18 '14 at 11:01
  • If I purchased a theme that has regular updates, where is the best place to copy template files to so that my changes aren't overwritten by updates? Thanks for your answer above. – Christia Aug 4 '17 at 20:32
  • With above answer, your changes wouldn't be overwritten – Kojo Aug 5 '17 at 7:08
  • @Christia I meant putting your tpl files in your own subtheme of the bought one. This is always a good practice to create a subtheme of any one your installed. So that you can update the main theme anytime. – Kojo Aug 9 '17 at 18:06
1

When using the solution form @Kojo, if you don't want to use your own module, create a function in template.php:

function THEMENAME_form_FORMID_alter(&$form, &$form_state) {
    //...
}

For example

function bartik_form_comment_form_alter(&$form, &$form_state) {
    //...
}

This code will change all comment forms, you can be more specific and provide an id of the form of some type. To get the FORMID, open the rendered page and find the following field in html:

<input type="hidden" value="HERE-WILL-BE-THE-FORMID" name="form_id">
0

you can do this by loading the comment form.

$comment = comment_load($cid); // $cid is comment id $form = drupal_get_form('comment_form', $comment); print drupal_render($form);

url: https://api.drupal.org/api/drupal/modules!comment!comment.module/function/comment_load/7.x

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.