1

I have a global module that has several .tpl files.

I would like to alter these files, but only have the alterations appear on one particular page (or Node Template).

I know I can duplicate the existing .tpl files and place them in my theme directory to overwrite the global .tpl files, but how would I go about creating .tpl files that only get picked up when a particular page is viewed?

Eg, something like:

/sites/mysite.com/themes/custom/mytheme/templates/gallery.tpl

and then, for the 'About us' page:

/sites/mysite.com/themes/custom/mytheme/templates/about-us-page-gallery.tpl

4

With theme hook suggestions:

function MYMODULE_preprocess_page(&$vars) {
  if (some_contextual_condition()) {
    $vars['theme_hook_suggestions'][] = 'page__about_us_gallery';
  }
}

The above will correspond to a template file in your theme named page--about-us-gallery.tpl.php

| improve this answer | |
  • Thanks! I assume this goes in the theme's template.php file? – MeltingDog Mar 13 '17 at 0:28
  • Yes either there or in the .module file if putting in a module – Clive Mar 13 '17 at 1:56
1

but only have the alterations appear on one particular page (or Node Template).

...

how would I go about creating .tpl files that only get picked up when a particular page is viewed?

 

Based on the above, I'm assuming these are all nodes. If so,

node--85.tpl.php

Where 85 is the nid.

 

This is built in. Just copy node.tpl.php. Inside your theme, paste it and rename each copy with the correct nid. Make sure that there are 2 dashes -- between node and nid

 


This seems to be a similar question, Template page--node--<id>.tpl.php not working and there's some debugging things in the comments. Just in case.

| improve this answer | |
0

If you don't want to get your hands dirty and want to achieve this functionality using a module, context module is your best bet.

Context UI provides an administrative interface for managing and editing Contexts. It is not necessary for the proper functioning of contexts once they are built and can be turned off on most production sites.
Context layouts provides a formalized way for themes to declare and switch between page templates using Context. It is a continuation of an old Drupal themer's trick to switch to something besides the standard page.tpl.php file for a variety of special case pages like the site frontpage, login page, admin section, etc.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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