Page Template path dependant
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:
http://drupal7.babygeorges.com/full_page_image_gallery
http://drupal7.babygeorges.com/fresh_sliding_thumbnails_gallery
http://drupal7.babygeorges.com/sliding_panel_photo_wall_gallery
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
$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
// 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.
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,
$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/includes--theme.inc/function/_theme_process_registry/7" under "api.drupal.org/api/drupal"/includes--theme.inc/function/_theme_process_registry/7
"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:
http://drupal7.babygeorges.com/threed_wall_gallery
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).