1

there is no template suggestion for a comment reply form in Drupal 8, how can i create one? i am not talking about the reply form displayed in the node for which Drupal suggest a template :

field--node--comment--article.html.twig

I'am talking about the form in the redirection page after we click "reply" in a comment. there is no template suggestion, only this template is suggested

form.html.twig

Can you please tell me how can i create a specific template in my .theme file of my theme.

1

It's a bit unclear what exactly you want to achieve. But you can easily add another template suggestion based on the form ID by implementing hook_theme_suggestions_HOOK_alter for forms. The form ID can be found in the $variables then. But no matter if you are on the node route or the reply route this will simply give you comment_comment_form.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  $form_id = $variables['element']['#form_id'];
  // Add template suggestion based on form ID.
  $suggestions[] = 'form__' . $form_id;
}

If you want to do something different on the reply route you could check the current path for the /reply and /comment arguments OR check if the route parameters contain a PID, which would be the parent comment ID.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYTHEME_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  // Add template suggestion based on form ID.
  $suggestions[] = 'form__' . $variables['element']['#form_id'];
  // Add template suggestion based on form ID and parent comment ID.
  if ($pid = \Drupal::routeMatch()->getParameter('pid')) {
    $suggestions[] = 'form__' . $variables['element']['#form_id'] . '__reply';
    $suggestions[] = 'form__' . $variables['element']['#form_id'] . '__reply__' . $pid;
  }
}

Which will give you the following theme suggestions.

<!-- FILE NAME SUGGESTIONS:
   * form--comment-comment-form--reply--1.html.twig
   * form--comment-comment-form--reply.html.twig
   * form--comment-comment-form.html.twig
   x form.html.twig
-->
| improve this answer | |
  • Thank you for your support. It works, but why you've put rebuild_cache_access at the beginning of the function? The proper function name for a theme is THEMENAME_theme_suggestions_form_alter(). – octogone.dev Jun 1 at 19:33
  • Ah sorry, I just forgot to anonymize the module/theme name. – leymannx Jun 1 at 20:41

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.