Hi all! I'm using two features based on a function called _phptemplate_variables.

The first one is for showing different page templates depending on URL aliases:

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
   
      // Add page template suggestions based on the aliased path.
      // For instance, if the current page has an alias of about/history/early,
      // we'll have templates of:
      // page_about_history_early.tpl.php
      // page_about_history.tpl.php
      // page_about.tpl.php
      // Whichever is found first is the one that will be used.
      if (module_exists('path')) {
        $alias = drupal_get_path_alias($_GET['q']);
        if ($alias != $_GET['q']) {
          $suggestions = array();
          $template_filename = 'page';
          foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '_' . $path_part;
            $suggestions[] = $template_filename;
          }
        }
        $vars['template_files'] = $suggestions;
      }
      break;
  }
 
  return $vars;
}
?>

The second one is for showing different node templates based on the UID of the node publisher:

<?php
function _phptemplate_variables($hook, $vars) {
  $variables = array();
  if ($hook == 'node' && isset($vars['node']->uid)) {  // Only do this if a node
    $variables['template_file'] = 'node-user-' . $vars['node']->uid;
  }

  return $variables;
}
?>

The question is: I need these two features working at the same time. How could I merge these codes under the same _phptemplate_variables function?

Thank you!

Comments

pbarnett’s picture

you might like to give it a try...

Since your first case is looking for $hook to be 'page' and the second is looking for it to be 'node', would something like this not work?

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
  
      // Add page template suggestions based on the aliased path.
      // For instance, if the current page has an alias of about/history/early,
      // we'll have templates of:
      // page_about_history_early.tpl.php
      // page_about_history.tpl.php
      // page_about.tpl.php
      // Whichever is found first is the one that will be used.
      if (module_exists('path')) {
        $alias = drupal_get_path_alias($_GET['q']);
        if ($alias != $_GET['q']) {
          $suggestions = array();
          $template_filename = 'page';
          foreach (explode('/', $alias) as $path_part) {
            $template_filename = $template_filename . '_' . $path_part;
            $suggestions[] = $template_filename;
          }
        }
        $vars['template_files'] = $suggestions;
	return $vars;
      }
      break;

      case 'node':
        if (isset($vars['node']->uid)) {  // Only do this if a node
		 $variables = array();
                 $variables['template_file'] = 'node-user-' . $vars['node']->uid;
		 return $variables;
      }
      break;
  }

  return $vars;
}
?>

You may not need the last 'return $vars'.

Pete.

Xabi’s picture

It works! Thank you very much!