Monthly Archives: December 2010

Drupal: sub themes overriding pages based on alias

If you have a theme (for example the lovely rubik admin theme) and you want to change the look and feel for just a few pages based on path aliases (so in my case I have a admin/content/workflow section which I want to have a specific heavily customised look and feel for), you can simply create a sub theme.

The first thing you need to is create your theme folder, .info file and template.php file. Inside your template.php file add a mythemename_preprocess_page containing the following:

if (module_exists('path')) {
$alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
if ($alias == $_GET['q'] && $alias == 'admin/content/workflow') {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$vars['template_files'][] = $template_filename . '-sanbi';

I would imagine it’s more efficient to customise this function specifically for the few cases you will be using. Any way, once you have a function like this (or similar), as you would imagine, you can create a page-your-customised-page-alias.tpl.php page inside your sub theme folder. However you must also create a page.tpl.php page to go in the same directory. The helpful hefox on #drupal pointed out that this page.tpl.php does not have to be copied from your base theme’s folder, but can actually just contain something like:

< ?php include_once('sites/all/themes/rubik/page.tpl.php'); ?>

Which is nice.

Drupal: changing values on submit for node/page form

It’s a pretty simple thing, but just in case there is anybody out there as dumb as me this might be helpful. It’s confusing too because on everybody is talking about doing it like $form[‘#submit’][] = ‘my_custom_submit_handler’, which just doesn’t work for the node edit form.

If you are trying to add a button to the page edit form and you want it to change some of the values in the form before submitting it properly, you need to do it like this:

* Implementation of hook_form().
function mymodule_form_page_node_form_alter(&$form, &$form_state) {
$form['buttons']['Change a taxonomy value and save'] = Array (
'#type' => 'submit',
'#value' => t('Change a taxonomy value and save'),
'#weight' => 4,
'#submit' => Array('_mymodule_change_page_taxonomy_value_submit', 'node_form_submit')

* Function to perform changes to the workflow status field etc once page is saved
function _mymodule_change_page_taxonomy_value_submit($form, &$form_state) {
// Change the taxonomy value to whatever it is meant to be
$mynewtaxonomyvalue = [taxonomyobjectfromsomewhere];
$form_state['values']['taxonomy'][5] = $mynewtaxonomyvalue->tid;