May 21, 2009
Filed Under: Website Design and Development
by Jess Walls
We use Garland as an admin theme because it really does a great job with the many admin forms that Drupal provides. This saves us a lot of work re-skinning the admin for every new site.
Although you can get Node Edit and Node Add skinned in the same way, all other menu paths that start with /node/ will use the front-end theme by default. The reason for this is simple and understandable, but unfortunate; the path /node/id/clone, for example, doesn’t start with /admin and for all Drupal knows it could just be an alternate front-end view for the node. So, it’s not at all safe to just default to using the admin theme for these paths.
One way to avoid the issue is to use a menu path that starts with /admin/ but that causes a number of other issues: you can’t leverage the sub-menu system on the node view and it makes the workflow for using it unpleasant, it doesn’t naturally inherit permissions and other settings, etc. So, that’s not a good plan either.
So, instead we will do exactly what the system module does — use hook_init to change the theme based on the current path for node-based functionality.
/**
* Implementation of hook_init().
*/
function clone_init() {
// Only affect the menu path created by this module, and only if the node admin theme checkbox is set.
if (variable_get('node_admin_theme', '0') && arg(0) == 'node' && arg(2) == 'clone') {
global $custom_theme;
// Use the chosen theme ID, or the front-end theme if one hasn't been selected.
$custom_theme = variable_get('admin_theme', '0');
// From system.module, this css file is required in the admin view.
drupal_add_css(drupal_get_path('module', 'system') .'/admin.css', 'module');
}
}