Page Template path dependant

1 reply [Last post]
Last seen: 8 years 9 weeks ago
Joined: 2011-09-12

In D7 how do you theme in a "menu/path" dependent manner, via module hook menu? i.e.
I want to take to have total control of themeing of page just for 1 "menu path" and I want the themeing to be done by the module generating the path using a page template, this way I don't have to reproduce it from theme to theme.
There is a lesson in the libary that show how the module can override, but the whole site gets overridden. I want just one page dependent on a path to be totally overridden, see [Drupal 7 Core Concepts][Section 9]["how to override template files"].

I am doing these exercises below see:

I used both module code to producte the data, to show and I had to page theme template suggestion in the "Bluemaster" theme directory. There must be a way to suggest all themeing templates are in the module directory just for base on a condition on a page (or family of pages)

The problem is as follows, what happen when I change theme? I have to port my code from the previous theme.


Ok. I basically solved my problem - I found this by googling a bit

1) let the registry know of my template file in my module path
2) give the registry my template suggestion that's residing in my module directory

1) let the registry know of my template file in my module path

function mymodule_theme_registry_alter(&$theme_registry) {
  $mod_path = drupal_get_path('module', 'mymodule');
  $theme_registry_copy = $theme_registry;       // munge on a copy
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'pow', $mod_path);
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  $hooks = array('node');
  foreach ($hooks as $h) {
    _mymodule_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);

function _mymodule_insert_after_first_element(&$a, $element) {
  if(is_array($a)) {
    $first_element = array_shift($a);
    array_unshift($a, $first_element, $element);

2) My module preprocess page function will look something like this

function [mymodulename]_preprocess_page(&$variables) {
  // Step 1:
  if (module_exists('path')) {
  // Step 2:
  $path_alias = drupal_get_path_alias($_GET['q']);
  // Step 3:
  $alias_parts = explode('/', $path_alias);
  // Step 4:
  $last = array_reverse($alias_parts);
  $last_part = $last[0];
  if ($last_part != "edit") {
  // Step 5:
    $templates = array();
    $template_name = "page";
  // Step 6:
    foreach ($alias_parts as $part) {
      $template_name = $template_name . '-' . $part;
  // Step 7:
      $templates[] = $template_name;
  // Step 8:
    $variables['template_files'] = $templates;
  } // End of the edit check
  } // End of the check for the path module
  } // End of the preprocess_page function


I haven't try it yet. I'll let y'all know, once I am done.

Thank you.

Last seen: 8 years 9 weeks ago
Joined: 2011-09-12
"How to override template files and ..." answered my question

I just look at the video, "How to override template files and next steps" for the nth time. It has already solved the question. Instead of overriding the path for "page.tpl.php", I can give the path of my "suggestion template" in my case the template file would be "page--threed_wall_gallery.tpl.php" in my module directory.
The code would look like as follows,

function threed_wall_gallery_theme_registry_alter(&$theme_registry) {
  $mod_path = drupal_get_path('module', 'threed_wall_gallery');
  $theme_registry_copy = $theme_registry;       // munge on a copy

  //I am still learning this function.
  //Maybe this should be the subject of a lesson
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'pow', $mod_path);
  // Suggestion is being done here
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);


See "api/drupal/" under ""/
"threed_wall_gallery" is the name of my module and the menu path.
Note: I don't need the pre process code anymore. I took it out. The above suffices.

This actually works, see url:

So I was able to totally override the theming just for this page on a "menu/path" basis without affecting my other pages.

What is so good about that? When I change change theme, I don't have to add extra code in the newly activated theme (less work).

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:

Contact us

Below are the supported keyboard shortcuts for our video player.

Start / stop video
Go to next video
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
Jump to a percentage (2 is 20%, for example)
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
1 month
6 months
10% off
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.