1

I usually override a form template this way.

/**
 * Implementation of hook_theme().
 */
function MODULE_theme() {
  return array(
    'user_login' => array(
      'render element' => 'form',
      'template' => 'user_login',
      'path' => drupal_get_path('module', 'MODULE').'/templates',
    ),
  );
}

I create templates/user_login.tpl.php in my module folder; then, I can override the form.

I tried overriding a node comment form this way, but it didn't work: The node comment form didn't use my custom template file.

I have googled a lot, but I got nothing about overriding a node comment form template.

How can I force Drupal to use a template file for the node comment form?

1

In Drupal 7, and in the case of the user login form, your trick works because the form doesn't set the #theme property; in that case, Drupal will use the theme function whose machine name is equal to the form ID. (For the login for, that is user_login.)
That is not true for the comment form, for which its form build (comment_form()) sets the #theme property.

$form['#theme'] = array('comment_form__node_' . $node->type, 'comment_form');

There isn't a match between the machine name of the theme the form builder wants to use, and the comment-edit form ID; the latter is comment_node_CONTENT_TYPE_form, not comment_form__node_CONTENT_TYPE. If you were defining a comment_form theme function, that should work.

In Drupal 6, there is no #theme property for comment_form, which is the form ID of the comment edit form.

  • first, thanks your edit my question to chang some english grammar problem. My english is so terrible. I have read your reply many times. Your idea is : the form_id for node comment form , I should use comment_form ? – Rylyn Aug 3 '13 at 1:51
  • theme_comment_form() should be the theme function that Drupal would use for the comment edit form. – kiamlaluno Aug 3 '13 at 9:05
0

Following code will allow you to use comment-form--node-forum.tpl.php template:

/**
 * Implements hook_theme().
 */
function hook_theme($existing, $type, $theme, $path) {
  return array(
    "comment_form__node_forum" => array(
      'render element' => 'form',
      'template' => "comment-form--node-forum"
    )
  );
}

Replace 'forum' to your content type.

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.