0

I'm developing my first Drupal site, having built my CMS experience on WordPress. I've managed to wrap my head around many of Drupal's paradigms, but I can't figure out how Drupal accomplishes the functionality of page templates in WordPress.

By "page templates" I mean the option in WordPress to set a specific template (written in PHP) to use for a given page:

WordPress Page Attributes

For reference, I use the templates in multi-site deployments to create standardized pages for subsites (e.g. "contact", "about us"), but a subsite has the ability to disable the template and create their own content.

What would be the best practice to accomplish this?

1 Answer 1

0

There are several ways to suggest/override theme of a page in Drupal.

1. Override core theme files:

Firstly there are core themes defined by Drupal, which can be overridden from your theme. For example, the default user profile page uses user-profile.tpl.php. Though this is defined in Drupal Core, you can override it by copying it to your theme folder and modifying. The list of core file can be found here.

2. Default Theme Suggestions in Core

Drupal has implemented some default theme suggestion patterns in the core. It helps us write a specific theme for a page, path or even a particular article without having to write code. For example, if your about page has a node id 1. You write a template for just that page by creating a template file page--node--1.tpl.php in your theme folder. Similarly, you can suggest a theme for a node, user, block etc. You can see available core suggestions in this page under respective heading.

3. From UI using context module:

Context module allows you to manage contextual conditions and reactions for different portions of your site. You can, for example, choose 1 or more conditions like path, user type etc and then add theme suggestion as a reaction to that condition/s.

  1. Using context module.
  2. Also, see ThemeKey module.

4. From code: You can also write custom theme suggestion based on your own logic from code, either in template.php file of your template from a custom module.

An example from documentation:

// Add a single suggestion for nodes that have the "Promoted to front page" box checked.

function drop_preprocess_node(&$variables) {
  if ($variables['promote']) {
    // looks for node--promoted.tpl.php in your theme directory
    $variables['theme_hook_suggestions'] = 'node__promoted';
  }
}

A lot of code example can be seen if you scroll a bit down on this page.

Additionally, change the theme debug settings in setting.php to get additional information in HTML markup with respect to the theme. The setting to change is $conf['theme_debug'] = TRUE;. Once done, you'll get a lot of valuable suggestion in the HTML markup of each page.

2
  • In D8, there isn't a 'theme_hook_suggestions' key in the THEME_preprocess_node argument, and adding it as done in your code sample doesn't seem to have any effect.
    – isUsername
    Commented Jun 14, 2017 at 18:06
  • Figured it out for D8. THEME_theme_suggestions_HOOK_alter() has to be used to change the suggestion for a node (i.e. mytheme_theme_suggestions_node_alter())
    – isUsername
    Commented Jun 14, 2017 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.