1

Twig debugging is suggesting node--4.html.twig as template file for a node whose ID is 4. Since the node ID changes when changing environment, I would need to use a template file whose name is based on the node title.

Is there a straightforward way to achieve this?

I am using Drupal 9.

6
  • You can use hook_theme_suggestions_node_alter to add more suggestion template.
    – MrD
    Jun 23 at 1:51
  • @LesLim I've updated the question to reflect the fact I'm using Drupal 9, thank you!
    – johnny_n
    Jun 23 at 2:09
  • @MrD I'm also using the Twig Template Suggester and it's not coming up with anything useful in this regard either.
    – johnny_n
    Jun 23 at 2:12
  • @johnny_n You can enable alias for node and add suggestion by url same with page suggestion.
    – MrD
    Jun 23 at 2:17
  • You may not want to use the title field for this. Maybe create a special field for defining this template suggestion. If you truly need it for only one page, you can set the field's value and then remove the field from the content type's form display so it can't be changed later. It'd be a real drag if a simple title change breaks your page layout!
    – sonfd
    Jun 23 at 14:15
1

Following MrD's comment, hook_theme_suggestions_hook_alter is the key (see documentation about this hook) or hook_theme_suggestions_alter() if you wish to add suggestions to several entity types.

It will allow you to add the template suggestions you are missing.

function yourTheme_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  // Use $variables variable to get data related to your node
  // e.g. $node = $variables["elements"]["#node"] to get your Node object

  // Use $suggestions array variable to add your custom suggestion(s) to the list
  // e.g. $suggestions[] = "node__" . "myawesomesuggestion";
}

Then, add your template file node--myawesomesuggestion.html.twig to your theme and you should be done with it.

1
  • Thank you @misterdidi - you're absolutely right. Appreciate the clarification, I misunderstood the nature of the hook.
    – johnny_n
    Jun 23 at 20:16
0

The Node module doesn't add template suggestions that include the node title, in node_theme_suggestions_node(). Modules and themes can alter the existing suggestions implementing hook_theme_suggestions_HOOK_alter().
A point to keep in mind is that the suggested template name should only contain characters allowed in a PHP function name.

For the node title, I would use the following code.

function mymodule_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  $node_title = \Drupal::transliteration()->transliterate($variables['elements']['#node']->getTitle());
  $suggestions[] = 'node__' . preg_replace('/[^a-z0-9_]+/', '_', mb_strtolower($node_title));
}

The code to replace characters that shouldn't be in a PHP function name is the same used from media_theme_suggestions_media() for $provider_id.

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.