-2

I would like to have a custom template for links (edit, reply, deleted) for my custom Comment Type. (Drupal 9)

Twig Debugging of course enable. I have tried to use hook_theme_suggestions_link_alter and hook_theme_suggestions_link, and I see my suggestion, but it doesn't apply my custom twig for links.

I spent a lot of time looking for a solution and did not find. Please, could you help me?

function answer_theme_suggestions_links_alter(array &$suggestions, array $variables)
    {
      if (isset($variables['links']['comment-custom-action'])) {
        array_splice($suggestions, 1, 0, 'links__comment__answer');
    
      }
    }

function answer_theme()
{
  return [
    'links__comment__answer' => [
      'template' => 'links--comment--answer',
      'base hook' => 'links'
    ]
  ];
}

enter image description here

5
  • First: Enable Twig debuggin to see the template that's currently used as HTML comments when inspecting the page. Next, copy that template to your custom theme's template folder and adjust the markup to your needs. Flush cache.
    – leymannx
    Sep 7 at 17:28
  • Sorry, it is not the answer for my question
    – Julia
    Sep 7 at 17:49
  • Yes, it's a comment after which you added crucial information to your question. So you tried hook_theme_suggestions_comment_alter and could not retrieve the comment type to add a template suggestion based on the comment type? Can you please post the current code that's not working?
    – leymannx
    Sep 7 at 18:48
  • The question is improved however I do not understand it fully.
    – cilefen
    Sep 7 at 23:04
  • 1
    I edited my question one more time, maybe now it is clearer
    – Julia
    Sep 7 at 23:15

1 Answer 1

0

It happens because Drupal first collect all the theme suggestions following this order.

  • The suggestions returned by hook_theme_suggestions_HOOK() implementations done by modules, the base themes for the active theme, and the active theme (in that order)
  • The suggestions returned by hook_theme_suggestions_alter() implementations done by modules, the base themes for the active theme, and the active theme (in that order)
  • The suggestions returned by hook_theme_suggestions_HOOK_alter() implementations done by modules, the base themes for the active theme, and the active theme (in that order)

Inside the group of modules (or base themes) that implement the same hook, the invocation order is determined by system weight, then by module (or theme) name.

Once Drupal collects all the theme suggestions in an array, it reverts the order of those suggestions, which means the last suggestion becomes the first one.

This is done by ThemeManager::render(), for example, which uses the following code. (Notice the code following the "// >>> * <<<" comment I added.)

  // Invoke hook_theme_suggestions_HOOK().
  $suggestions = $this->moduleHandler->invokeAll('theme_suggestions_' . $base_theme_hook, [
    $variables,
  ]);

  // If the theme implementation was invoked with a direct theme suggestion
  // like '#theme' => 'node__article', add it to the suggestions array before
  // invoking suggestion alter hooks.
  if (isset($info['base hook'])) {
    $suggestions[] = $hook;
  }

  // Invoke hook_theme_suggestions_alter() and
  // hook_theme_suggestions_HOOK_alter().
  $hooks = [
    'theme_suggestions',
    'theme_suggestions_' . $base_theme_hook,
  ];
  $this->moduleHandler->alter($hooks, $suggestions, $variables, $base_theme_hook);
  $this->alter($hooks, $suggestions, $variables, $base_theme_hook);

  // Check if each suggestion exists in the theme registry, and if so,
  // use it instead of the base hook. For example, a function may use
  // '#theme' => 'node', but a module can add 'node__article' as a suggestion
  // via hook_theme_suggestions_HOOK_alter(), enabling a theme to have
  // an alternate template file for article nodes.
  // >>> * <<<
  foreach (array_reverse($suggestions) as $suggestion) {
    if ($theme_registry->has($suggestion)) {
      $info = $theme_registry->get($suggestion);
      break;
    }
  }

It then search for the first existing template file that matches the suggestions and uses that template file.

In your case, the suggested template files have been checked following this order. (This is shown in the debugging output shown in the screenshot.

  • links--comment.twig.html
  • links--inline.twig.html
  • links--comment--answer.twig.html

If links--inline.twig.html has been suggested by the active theme, or one of its base themes, and the code shown in the question is implemented by a module, there isn't anything you can do, as a theme suggestion always take precedence over a module suggestion.

If links--comment.html.twig suggestion was done by module, and the code shown in the question is implemented by a module too, you need to increment your module's weight, for example calling module_set_weight() inside hook_install() or hook_update_N().

This is doesn't seem your case, but notice that if you implement answer_theme_suggestions_links_alter() and answer_theme_suggestions_alter() (or answer_theme_suggestions_links_alter() and answer_theme_suggestions_links()) the suggestions given by answer_theme_suggestions_links_alter() takes the precedence.

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.