Score:0

How can I add a node template suggestion by node title?

in flag

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.

MrD avatar
cn flag
MrD
You can use hook_theme_suggestions_node_alter to add more suggestion template.
in flag
@LesLim I've updated the question to reflect the fact I'm using Drupal 9, thank you!
in flag
@MrD I'm also using the Twig Template Suggester and it's not coming up with anything useful in this regard either.
MrD avatar
cn flag
MrD
@johnny_n You can enable alias for node and add suggestion by url same with page suggestion.
sonfd avatar
in flag
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!
in flag
@sonfd - Good call. And thank you MrD for the additional advice. This has been unbelievably helpful, thank you!
Score:1
de flag

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.

in flag
Thank you @misterdidi - you're absolutely right. Appreciate the clarification, I misunderstood the nature of the hook.
Score:0
us flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.