Skip to main content

Post categories

  • Drupal Tips & Tricks
  • Drupal Howtos
  • Drupal Themes
  • X Twitter
  • youtube
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact

Breadcrumb

  1. Home
  2. HowTos
  3. Drupal 7 PHP templates .tpl.php for fields, blocks, views, content types, pages, etc.

Drupal 7 PHP templates .tpl.php for fields, blocks, views, content types, pages, etc.

HowTos
December 08, 2014
Profile picture for user drupal
By drupal
  • facebook-f
  • twitter
  • envelope
  • print
213

Core templates: https://www.drupal.org/node/190815
Drupal 7 Template (Theme Hook) Suggestions: https://www.drupal.org/node/1089656

Content Types

A common override that is not included in the default list is the page.tpl.php override based on the content type being displayed. There is a node.tpl.php override based on the same condition which leads to confusion as to where the page override exists. On top of that, themes like Zen add this type of override to the Template Suggestions, which leads those using Zen to believe that this is part of the default list. Check the theme documentation to see if this override has been added to the Template Suggestions by the theme. If it hasn’t, you need to add it manually.

The process is straightforward. We can create additional Template Suggestions simply by adding them to the ‘theme_hook_suggestions array in our template.php file.

  1. Open the template.php file in your theme for editing.
  2. Look for a function called yourthemename_preprocess_page (replace the yourthemename with your theme’s name).
  3. If this function already exists, you will need to add the if statement to the end of the function just before the closing bracket. Otherwise you’ll need to create a new function to look like this:

function themeName_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}

Required modifications: themeName = the name of your theme; some themes with existing preprocess functions require you to modify the function. To add $hook or to modify $vars into $variables throughout the code of the function. In my case the following worked:

function themeName_preprocess_page(&$variables, $hook) {
if (isset($variables['node'])) {
// If the node type is "blog_madness" the template suggestion will be "page--blog-madness.tpl.php".
$variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;
}
(...)
}

The template file needs to be named as follows: page--name-node-type.tpl.php. Please note the double dash. The name of the node type needs to be spelled exactly as the machine name of the content type. (Example: machine name = tips_and_tricks, template file = page--tips_and_tricks.tpl.php.

See also: https://www.drupal.org/node/1142800#comment-5365466

Blocks

In Drupal 7 template files are searched in the following order:

  • block--block--[block-id].tpl.php
  • block--[module]--[delta].tpl.php
  • block--[module].tpl.php
  • block--[region].tpl.php
  • block.tpl.php

If the block delta key uses a hyphen, (-), replace this with an underscore (_).

For views:

  • block--views--{view's machine_name}-block
  • block--views--[view-name]-[display-name].tpl.php

See also: https://www.drupal.org/node/104319 and http://drupal.stackexchange.com/questions/11468/create-template-file-for...

Drupal 7 PHP templates .tpl.php for fields, blocks, views, content types, pages, etc.
PHP

Read more articles

Newer
Troubleshooting: Impossible to save settings for a new Multimedia asset field
Older
How to change the title of the Drupal Blog page?
  • Log in to post comments
Profile picture for user drupal
drupal
213
1
min read
A- A+
  • facebook-f
  • twitter
  • envelope
  • print

Contact Drupal 4U

Get in touch for questions and our Drupal development, design and SEO services.

Contact Us

We aim to reply the latest within 48h.

About Drupal 4U

We are a group of Drupal designers, developers and webmasters and we launched this website to share our experience and knowledge with you. Drupal is a free software package that allows you to easily organize, manage and publish your content, with an endless variety of customization. Drupal is open source software maintained and developed by a very large community of developers and users.

  • X Twitter
  • youtube

Drupal Services

  • Drupal Web Design
  • Drupal Development
  • Drupal SEO
  • Drupal Migration

Drupal 4U

  • Services
  • Showcases
  • Themes
  • Blog
  • About

Drupal Links

  • Official Drupal Website
  • Drupal Answers
  • More Than Themes
  • Theme Forest
  • DXPR: Drupal for Marketers
2012 - 2025, Drupal 4U

Footer menu

  • Contact
Home
Drupal 4U
Drupal Development, Drupal Web Design, Drupal SEO
  • Home
  • Showcases
  • Services
  • Blog
  • Themes
  • About
  • Contact
Clear keys input element