Thursday, August 17, 2017 09:36 am EDT

Creating Drupal Template Suggestions

Bri's picture

Chances are that you have wanted to create a new template as you work on your Drupal sites. Perhaps the blog page needs to have some markup that varies from your typical page.tpl.php template. Adding some lines of code to the template.php file inside the theme you are using can help you achieve this. By default Drupal will look for templates that match the name of a content type. For example, if you created a template called node-blog.tpl.php, Drupal would recognize it and load it on pages that contained blog content. This is because drupal will look for templates that are of a certain content type by matching them to the following:

node-[content_type_name].tpl.php

If you want to take this a step further and be able to create a template for your blog post teasers, and another for your full page posts, try adding the following to your template.php page.


function phptemplate_preprocess_page(&$vars)
{
//code block from the Drupal handbook
//the path module is required and must be activated
if(module_exists('path'))
{
//gets the "clean" URL of the current page
$alias = drupal_get_path_alias($_GET['q']);

$suggestions = array();
$template_filename = 'page';
foreach(explode('/', $alias) as $path_part)
{
$template_filename = $template_filename.'-'.$path_part;
$suggestions[] = $template_filename;
}
$vars['template_files'] = $suggestions;
}
//echo '<pre>';
//print_r($vars['template_files']);
//echo '</pre>';
}
function phptemplate_preprocess_node(&$vars)
{
//default template suggestions for all nodes
$vars['template_files'] = array();
$vars['template_files'][] = 'node';

//individual node being displayed
if($vars['page'])
{
$vars['template_files'][] = 'node-page';
$vars['template_files'][] = 'node-'.$vars['node']->type.'-page';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-page';
}
//multiple nodes being displayed on one page in either teaser
//or full view
else
{
//template suggestions for nodes in general
$vars['template_files'][] = 'node-'.$vars['node']->type;
$vars['template_files'][] = 'node-'.$vars['node']->nid;

//template suggestions for nodes in teaser view
//more granular control
if($vars['teaser'])
{
$vars['template_files'][] = 'node-'.$vars['node']->type.'-teaser';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-teaser';
}
}

//echo '<pre>';
//print_r($vars['template_files']);
//echo '</pre>';
}

function phptemplate_preprocess_block(&$vars)
{
//the "cleaned-up" block title to be used for suggestion file name
$subject = str_replace(" ", "-", strtolower($vars['block']->subject));

$vars['template_files'] = array('block', 'block-'.$vars['block']->delta, 'block-'.$subject);

//echo '<pre>';
//print_r($vars['template_files']);
//echo '</pre>';
}

This code will go through and add to the array of template suggestions. If you simply add this to your template.php file and clear the cache from the performance page, your new suggestions will be ready to go. Drupal will use them from most specific to least specific all the way down to page.tpl.php, node.tpl.php, and block.tpl.php. If you would like to see the exact names of the templates that are being suggested, in the respective function (for node, page or block) uncomment the following lines:


//echo '<pre>';
//print_r($vars['template_files']);
//echo '</pre>';

Also as a word of caution, some theme's may already have a function that is using a preprocess for a page, block, or node. If this is the case, simply add the code to the bottom of the function before the closing "}" and you will get the same functionality.

Comments

Anonymous's picture

Nice Post

Many thanks for the changes on this http://www.assignmenthelperuk.co.uk website. Yes, this really happens sometimes that we want to view only 5 or less than 5 posts but there is no option of configuration then we can’t do anything.

Anonymous's picture

Thanks for the wonderful

Thanks for the wonderful suggestions and the coding techniques as well to create the Drupal template. That would be a useful technique to enhance the website appearance the people must visit this https://www.bestessays.com.au/ review website for the development of their website.