Drupal 7 – Customize region template file for specific content type

We can add preprocess function for regions such that we can add customization before rendering the region template file(.tpl.php).
Drupal 7 – Check if the current loading page is the node view page of a specific content type

Sometimes we may even want edit the template file(.tpl.php) for specific content such as a content type node view. In Omega theme, there are different regions and by default they will use the following template files.

<drupal>/sites/all/themes/omega/omega/templates

  • region--branding.tpl.php
  • region--content.tpl.php
  • region--menu.tpl.php
  • region--sidebar_first.tpl.php
  • region--sidebar_second.tpl.php
  • region.tpl.php


 

Let’s say if you want to have your own region--content.tpl.php for the article content type.

1. Copy the above region--content.tpl.php to <drupal>/sites/all/themes/<subtheme>/templates/.
 

2. Rename it to region--content--article.tpl.php and edit it for your own customization.
 

3. Create the <drupal>/sites/all/themes/<subtheme>/preprocess/preprocess-region.inc

<?php
function <subtheme>_alpha_preprocess_region(&$vars) {
  $menu_object = menu_get_object();
  if (isset($menu_object->type) && $vars['region'] == 'content') {
    if ('article' == $menu_object->type) {
      $vars['theme_hook_suggestions'][] = 'region__content__article';
    }
  }
}

 

4. Clear cache and your new .tpl.php should be applied.

If you want to have customized region--content.tpl.php for each content type. Use the following code in <drupal>/sites/all/themes/<subtheme>/preprocess/preprocess-region.inc.

<?php
function <subtheme>_alpha_preprocess_region(&$vars) {
  $menu_object = menu_get_object();
  if (isset($menu_object->type) && $vars['region'] == 'content') {
    if (isset($menu_object->type)) {
      $vars['theme_hook_suggestions'][] = 'region__content__' . $menu_object->type;
    }
  }
}

 

Then you could have region--content--<content type>.tpl.php for all content type.

Similarly, with the above approach you can create and customize any .tpl.php like html.tpl.php, node.tpl.php and comment.tpl.php… etc.

Done =)

About these ads

10 thoughts on “Drupal 7 – Customize region template file for specific content type

  1. Bob

    That helped a lot. I was able to create a new content type and changes I make there are now appearing in my display. I did have to figure out that the preprocess file has to go into /preprocess and not just under the subtheme.

    I have the devel module setup and I can see that my field variables are named:

    body (Array, 1 element)
    field_screencast_folder (Array, 1 element)
    field_screencast_file (Array, 1 element)
    field_screencast_width (Array, 1 element)
    field_screencast_height (Array, 1 element)

    but I can’t seem to figure out what variable name I need to use in my template file.

    $content does display my content, but I want access at the field level.

    I’ve tried $content['field_screencast_file'], $items['field_screencast_file'], print_r($node) and a bunch of other combinations. I’m afraid I don’t understand the naming convention for the actual variables nor how to find out what those are. Any suggestions would be appreciated.

    Bob

    Reply
    1. ykyuen Post author

      Which level are u working at now? preprocess_node? In that case, you could try to print_r($vars); in the preprocess function and get all the available values. and then you can set the value to the $vars and you can retrieve the value in the .tpl.php.

      For example, in the preprocess function.

      $vars['eureka'] = 'Eureka!';
      

       

      The above value could be retrieved by the $eureka in the .tpl.php.

      <?php print $eureka; ?>
      

       

      In you case, i think you just comment out the $content and print the field values directly in the .tpl.php.

      Alternatively, you can consider using the Display Suite module.

      Another possible solution is using Views and Panels to customized the node template. You could find more info in the following link for this approach.
      Drupal – Customize Node Template with Panel

      Hope this help~ =)

      Reply
      1. Lars J.

        Hi.
        First: thankyou
        Second: This is my preprocess-Function.It seems working.

        if (isset($menu_object->type) && $vars['region'] == 'content') {
            if (isset($menu_object->type)) {
              $vars['theme_hook_suggestions'][] = 'region__content__' . $menu_object->type;
            }
          }
        	//print_r($vars);
        	if (isset($vars['elements']['system_main']) && isset($menu_object->type) ): 
        		if ($menu_object->type == "weininfo"): // check for specific contenttype
        			//krumo($vars['elements']['system_main']['nodes']);
        			$vars['field_titel'] = reset($vars['elements']['system_main']['nodes']); // first element of $vars['elements']['system_main']['nodes'] ([node-id] 1,2 ....)
        			if(isset($vars['field_titel']['field_titel'])):
        				$vars['field_titel'] = $vars['field_titel']['field_titel'];
        				$vars['field_titel'] = render($vars['field_titel']);
        			endif;
        		endif;
        	endif;
        }
        
  2. Pingback: Hiding the node:title field but still having it available (using Omega theme in Drupal 7) | Ptocheia.net

  3. 3s2ng

    Thats a very helpful article.

    I implemented your solution on my taxonomy and it works now.

    Now I got 2 templates

    1. region–branding.tpl.php
    2. views-view–collection-taxonomy-term–page-1.tpl.php

    Now what I want to do is move 1 variable called $collection_image_url to my branding template beside my logo.

    This image URL is the taxonomy term default banner.

    How to access this $collection_image_url from the region–branding.tpl.php?

    Apologize for being a noob.

    Thanks and More Power!

    Reply
    1. ykyuen Post author

      You could define the $collection_image_url in the preprocess-region.inc. For example, if i add the following line in the preprocess-region.inc.

      $vars['eureka'] = 'Eureka!';
      

      You could retrieve the string by $eureka in the .tpl.php.

      Reply
  4. Matthijs Brouwer

    Would this still work for Omega 3.1? Because it copied it, changed the names and looked at it a hundred times but it is not working. Should I report the preprocess-regoin.inc somewhere in a .info file?

    Reply
      1. ykyuen Post author

        This code should work in Omega 3.1.

        I think i made a mistake on the post. you should put the preprocess-region.inc in the <drupal>/sites/all/themes/<subtheme>/preprocess folder. I have just updated the post. Does it solve the problem?

        We don’t need to set the .info for the preprocess functions to work. Just clear the cache and the effect would take place immediately.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s