3

I have custom content type called books that represents a book that can be reviewed by using a custom comment type called reviews.

My problem is the comment form title above the comment form shows as "Add New comment", which I need to change as "review the book".

How can I change this to read as "review the book"?

3

The Add new comment header is a part of theme layer.

You can copy your theme's existing comment template (e.g. core/themes/classy/templates/field/field--comment.html.twig if you're using a theme that utilizes the base classy theme) over to your theme's template dir and rename it to match your specified field (e.g. field--field-my_custom_comment_machinename.html.twig). If you're not familiar with this, enable Twig template debugging.

Once you've create a copy of the template, you can then edit template header as you see fit.

  • This works when "Show reply form on the same page as comments" is checked. If i want the form in a separate page, everything from the link to title goes back to add new comment. Any suggestion on what template i should edit? – jack Aug 23 '16 at 0:19
  • 2
    Twig debugging can help here again. It looks like the template hints are page--comment--reply--node--field-my_custom_comment_machinename.html.twig & html--comment--reply--node--field-my_custom_comment_machinename.html.twig. That might be overkill for what you are doing and you might get by with something more granular (perhaps .theme preprocess functions). – Shawn Conn Aug 23 '16 at 0:31
  • lol... now i've to learn preprocess functions... Thanks for your valuable time. – jack Aug 23 '16 at 0:35
  • 1
    This will change in a good direction in Drupal 8.3: drupal.org/node/2546116 – Adrian Cid Almaguer Nov 25 '16 at 22:45
2

You can implement the template_preprocess_field__comment() hook in your custom module:

/**
 * Implements hook_preprocess_field__comment().
 */
function MY_MODULE_preprocess_field__comment(&$variables) {
  switch ($variables['field_name']) {
    //field_message_custom is your custom comment field name
    case 'field_message_custom':
      $variables['add_new_comment'] = t('Review the book');
      break;
    default:
      $variables['add_new_comment'] = t('Add new comment');
  }
}

And now you change use the add_new_comment variable in your field--comment.html.twig this line:

<h2 class="title comment-form__title">{{ 'Add new comment'|t }}</h2>

for this line:

<h2 class="title comment-form__title">{{ add_new_comment }}</h2>

After this clean your cache.

It seems that we will be able to change this message in the interface in Drupal 8.3. Read: You can add a review, opinion, greeting, etc. comment type but not change the "Add new comment" text

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