I have a page with url ?q=admin/settings/history
. Is there any way by which I can change the template
for this page alone. I tried using hook_preprocess_page
but I was unable to change the layout of page. Could someone please lead me in the right direction?
-
I posted a generic, module answer. If you want the code answer, remember that questions that are substantially about code, where the code is not shown, are possibly closed.– MołotCommented Dec 16, 2013 at 9:54
5 Answers
Yet another solution, in a couple of lines of code, is:
Return the machine-readable name of the theme to use for the current page. This hook can be used to dynamically set the theme for the current page request. It should be used by modules which need to override the theme based on dynamic conditions
e.g
/**
* Implements hook_custom_theme
*/
function MYMODULE_custom_theme() {
// Set path specific theme.
if (current_path() == 'admin/settings/history') {
return variable_get('custom_theme', 'theme_name');
}
}
Core example: system_custom_theme
Update: To use a specific template you can override page.tpl.php
- copy
page.tpl.php
topage--admin--settings--history.tpl.php
, clear caches (theme registry) and use that.
More info: https://drupal.org/node/1089656
-
tanx for the response.Should i be create a new custom theme or is there a possiblility to link my page to a custom.tpl.php page..?? Commented Dec 16, 2013 at 15:28
-
Hey, to use a custom
page.tpl.php
you can just copypage.tpl.php
topage--admin--settings--history.tpl.php
, clear caches and use that. More info: drupal.org/node/1089656 Commented Dec 17, 2013 at 10:27
It looks like you might be looking for a ThemeKey module:
ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties. It can also be easily extended to support additional properties exposed by other modules
Emphasis mine.
You can either use it directly, or use it's source code as an example - it's GPL, so you have the right to do it.
-
@Mołot tanx for the response sir,but is there any posssibility by which i could link this to a tpl.php page.?? Commented Dec 16, 2013 at 15:41
-
@Outlooker - yes, by creating a child theme with only that one tpl changed, and then setting this new theme for your path.– MołotCommented Dec 16, 2013 at 15:43
-
In D6 i used
$variables['template_file'] = 'testing';
where i could use a tpl page named testing.tpl.php.In D7 I shud be creating a sub theme..??Sorry @Mołot,am not really familiar with creating a subtheme. Commented Dec 16, 2013 at 15:49
In a simple way you can change the theme key by checking the condition,
/**
* Implements hook_init().
*/
function <module_name>_theme_init() {
global $theme_key;
if ($_GET['q'] == "admin/settings/history") {
$theme_key = '<Your new Theme key>';
}
}
-
3For Hook_init it's worth to remember: "Perform setup tasks for non-cached page requests." So on cached pages results may get funny. Also, there is no hook_init in Drupal 8, so using it now is deliberately asking for trouble with future migration / upgrade. Last but not least, I strongly suggest using
arg()
instead of$_GET
.– MołotCommented Dec 16, 2013 at 10:00
You can use Page Theme module
The Page Theme module is a simple and easy module to use which allows to use different themes than the site default on specific pages.
-
You cannot override the admin section using page theme module Commented Dec 16, 2013 at 9:54
I would suggest to implement a custom module that changes the theme being used for specific pages. It is enough to implement hook_custom_theme() as follows:
function mymodule_custom_theme() {
if (arg(0) == '' && is_numeric(arg()) && arg(3)== history
) {
return 'go theme page what you want yo use';
}
}