2

I have the following page:

About us

Whose path is:

site.com/node/6

Theme suggestions for the template files are:

page--node--6.tpl.php
page--node.tpl.php

You should already know that it is too weird to create custom template files based on the node's id, thus I like to have my custom page template based on the page's title (of course, converted to machine name, for instance "About us" to be "about-s"). What should I do?

  • Have you tried Context or Panels modules. you can set displays of different pages based on different criteria. – Quentin Campbell Apr 25 '15 at 11:17
  • I have them installed. You mean it is not possible by default in Drupal? – Mostafa Talebi Apr 25 '15 at 11:18
  • Maybe somebody else can answer that. I don't bother going the template route with those modules around. I mainly use Panels. – Quentin Campbell Apr 25 '15 at 11:20
  • 1
    Correct @Mostafa, there's no default theme hook suggestions for the page template based on a node alias - just for the internal path (eg node/1). Given that the node id is far less ephemeral than the title, using that method would usually be preferred. Your "you should already know that it's too weird...based on the node id" suggestion is incorrect in my opinion – Clive Apr 25 '15 at 11:24
  • With title I mean a GUID in String than in vague Integer. – Mostafa Talebi Apr 25 '15 at 11:33
3

I don't think this is an optimal solution at all, because node titles can easily be changed (and duplicated), but here you go:

function MY_THEME_preprocess_node(&$vars) {
  // This assumes that you are using Pathauto for generating clean URLs.
  // Get the "clean" title.
  $title = pathauto_cleanstring($vars['node']->title);
  // Replace all dashes with underscores. This is necessary for recognizing the
  // template filenames.
  $title = str_replace('-', '_', $title);
  // Add new template variation.
  $vars['theme_hook_suggestions'][] = 'node__' . $title;
}

Note: to make this bulletproof for different languages, alphabets and special characters, make sure you have the Transliteration module installed and set to transliterate strings prior to creating URL aliases. This way if a node title has non-standard character (e.g. Č) it will be transliterated to Latin alphabet (in this case C).

  • Instead of using pathauto function, I'd suggest to use Drupal's core function: drupal_clean_css_identifier() api.drupal.org/api/drupal/includes!common.inc/function/… You could consider updating your answer as well. – Alexar Apr 25 '15 at 11:35
  • Thanks for the comment. The only downside to using Pathauto's function is that you need to have Pathauto installed, which most of the sites already do. drupal_clean_css_identifier() also does not transliterate the characters, so you would need another function call to clean the output. I can update it for future reference, but I believe the Pathauto approach makes more sense, because that's the module that will generate your URLs anyway, and it's shorter. – Aram Boyajyan Apr 25 '15 at 11:39
  • 1
    @Alexar that's not necessarily correct - if you were creating a CSS class, then sure, but in this case you need the URL alias. Since path auto produces that alias, you should use its functions to reproduce the clean string (I'm assuming path auto installed, I've yet to come across a site where it isn't) – Clive Apr 25 '15 at 11:41
  • 1
    After testing this, I don't think drupal_clean_css_identifier is a viable solution anymore because it does not make the string lowercase, and there could be other edge cases (two spaces are converted to -- for example) where this will provide quirky results. Hope this makes sense. – Aram Boyajyan Apr 25 '15 at 11:44
  • 1
    @Topsitemakers I agree that transliteration is totally helpful. Also I can see that drupal_clean_css_identifier() is not doing strtolower and I figured drupal_html_class() is the right one. But I would argue the assumption of having pathauto installed. Given all these, I would do something like this $title = module_exists('pathauto') ? pathauto_cleanstring($title) : drupal_html_class($title); – Alexar Apr 25 '15 at 12:01

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.