Fri, 2015-08-21 15:24

Drupal is often intimidating when it comes to theming and often people get fatigued in an effort to understand how things work. Today I'll be going over an easy and understandable way to edit .tpls of a page you've created and implementing custom fields you've made into that .tpl so that you can easily customize things if need be on the rise.

First thing let's go into our theme (For this example I'm using AdaptiveThemes as my reference guide.) and edit our template.php. In this file, we'll simply need to add this snippet:

function YOURTHEMENAME_preprocess_page(&$vars) { // - page--example.tpl.php if (isset($vars['node'])) { $vars['theme_hook_suggestion'] = 'page__'.$vars['node']->type; // } }

Once you've copied this into the file you'll need to change "YOURTHEMENAME" to the relative name of your theme (The name of the parent folder your template.php file is in.). This provides a method in which drupal can communicate and find the content type through a hook suggestion; for more information regarding this method and more in-depth documentation on hook suggestions please refer to this page: https://www.drupal.org/node/1089656

If you've ever edited a node.tpl this is the exact same process but worry none if not; simply follow suit by going into your core-theme/templates/ folder and grab a copy of the page.tpl.php file from inside and paste this copy into your sub-theme/templates/ folder. If you're trying edit the content type for "Shoe Types" simply rename the "page.tpl.php" to "page--shoe-types.tpl.php" and edit the file with your favorite text editor.

In here you're free to customize the way an entire layout is displayed or re-displayed. Often designers may create a mini-page with a flavor of it's own and may have a custom header, sidebar, footer and so forth. Let's start off by calling our first custom field... In this example I'm using a text field I created called subtitle.

<.php if (isset($node->field_shoe_types_subtitle['und'][0]['value']) && count($node->field_shoe_types_subtitle['und'][0]['value']) > 0){ $output_field = $node->field_shoe_types_subtitle['und'][0]['value']; print $output_field; } .>

As you can see "field_shoe_types_subtitle" is the "Machine Name" of the field I created in the back end of Drupal when creating the field itself in the content type. Replace this with the name of your custom text field's Machine Name and your php snippet above should output the correct function.

That about wraps it up for this tidbit, from here you should be able to setup your own content type through the tpl.php format and enable your first custom field. Remember that some fields require different snippets of PHP, it is recommended that you take the time to learn what these functions do when calling them to better achieve the format of other fields. I hope you found this insightful and please don't forget to check us out more often for helpful tips and tutorials like these.

Written by
Anthony Marrero
Front-End Expert

A Florida native, Anthony is always living in the bleeding edge of modern design patterns and practices. He has built countless responsive and mobile websites using the latest technologies, and is our go to person for ensuring the finished product is pixel perfect on all devices. Outside of code he is an accomplished lyricist and musician.