Custom page template, template.php, and $classes

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
esod's picture

I'm creating a sub-theme based on Zen 6.x-2.1 in Drupal 6.20. I've created a content type called recipe. I add the following code to template.php:

function mytheme_preprocess_page(&$vars) {
  if (isset($vars['node'])) {
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type); 
  }
...

After clearing the cache, page-recipe.tpl.php is recognized by the theme. I remove <?php print $sidebar_first; ?> and <?php print $sidebar_second; ?> from page-recipe.tpl.php. However, <body> still contains one-sidebar sidebar-first classes (i.e. <body class="page not-front logged-in node-type-recipe page-node-157 section-node one-sidebar sidebar-first admin-menu>). I add the following to mytheme_preprocess_page(&vars):

...
  if ($vars['node']->type == 'recipe') {
    $vars['classes'] = str_replace('one-sidebar sidebar-first', 'no-sidebars', $vars['classes']);
  }
}

Only now <body> renders as <body class="admin-menu">. My goal is for <body> to be <body class="page not-front logged-in node-type-recipe page-node-157 section-node no-sidebars admin-menu>. Will someone tell me what's wrong?

thanks!

Comments

Perhaps if I elaborate

esod's picture

Perhaps if I elaborate, someone will offer some assistance.

Basically, I'm trying to remove the sidebars from the recipe content type.

Once page-recipe.tpl.php overrides page.tpl.php for recipe type nodes, and the sidebar variables are removed from page-recipe.tpl.php, all should be well. However, <body> still contains sidebar-first, for which the CSS directive:


.sidebar-first #content {
  width: 760px;
  margin-left: 200px; /* LTR / / The width of .region-sidebar-first. /
  margin-right: -960px; /
LTR / / Negative value of #content's width + left margin. */
}

causes the content on the recipe node to indent 200px on the left margin. :-| If I can replace sidebar-first with no-sidebars, this CSS directive:

#content,
.no-sidebars #content {
  float: left; /* LTR /
  width: 760px;
  margin-left: 0; /
LTR /
  margin-right: -760px; /
LTR / / Negative value of #content's width + left margin. /
  padding: 0; /
DO NOT CHANGE. Add padding or margin to #content .section. */
}

which has no left margin will be used instead, and all will indeed be well. Unfortunately, my preprocess function is removing all the classes from <body> except admin-menu, which is most likely not relevant to this problem.

thanks!

Try modifying

Garrett Albright's picture

Try modifying $vars['classes_array'] as well. It will be an array, so you'll have to go about it a bit differently.

Still can't modify $vars['classes_array']

esod's picture

I've found a way forward, which is good, but I still am not modifying $vars['classes_array'], which is not good enough. My theme_preprocess_page() function so far:

<?php
function theme_preprocess_page(&$vars, $hook) {
 
// custom content type page template
  // Renders a new page template to the list of templates used if it exists
 
if (isset($vars['node'])) {
   
// This code looks for any page-custom_content_type.tpl.php page
   
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type); 
  }

 
$recipe = $vars['node']->type == 'recipe';
  if (
$recipe) {
   
$vars['sidebar_first'] = '';
    if (
in_array('one-sidebar', $classes_array)) { unset($classes_array['one-sidebar']); }
  
//$vars['classes_array'][] = 'no-sidebars';
 
}
}
?>

I guess $vars['sidebar_first'] = ''; removes sidebar-first from <body class=" ... "> which allows no-sidebars in. This is better than before although I'd still like to know how to modify $vars['classes_array'].

in_array() produces this non-fatal error:


Warning: in_array() expects parameter 2 to be array, null given in theme_preprocess_page() (line 197 of .../template.php).

Thanks

This is better than before

Garrett Albright's picture

This is better than before although I'd still like to know how to modify $vars['classes_array'].

The same way you modify any array. Maybe I don't understand quite what the problem is?

Modifying $vars['classes_array'] -- Solved!

esod's picture

along with the entire question. Here's the answer:

The following version of theme_preprocess_page() permits custom page templates and modifications to $vars['classes_array'] in Drupal 6.20, Zen 6.x-2.1 (not enabled), and My theme, a sub-theme (enabled).

<?php
function mytheme_preprocess_page(&$vars, $hook) {
 
// page
  // Renders a new page template to the list of templates used if it exists
 
if ($vars['node']->type != "") {
   
$vars['template_files'][] = "page-node-" . $vars['node']->type;
  }
  if (
$vars['node']->type == 'recipe') {
   
$vars['sidebar_second'] = '';
   
$vars['sidebar_first'] = '';
    if ((
$pos = array_search('one-sidebar', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
    if ((
$pos = array_search('two-sidebar', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
    if ((
$pos = array_search('sidebar-second', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
    if ((
$pos = array_search('sidebar-first', $vars['classes_array'])) !== false) { unset($vars['classes_array'][$pos]); }
   
$vars['classes_array'][] = 'no-sidebars';
   
$vars['classes_array'][] = 'and-another';
  }
}
?>

Per http://drupal.org/node/249726, I altered the page template suggestion instruction and changed the name of page-recipe.tpl.php to page-node-recipe.tpl.php. It wasn't necessary to remove <?php print $sidebar_first; ?> or <?php print $sidebar_second; ?> from page-node-recipe.tpl.php since template.php overrides template files.

When I moved the function to the web site's template.php, the null array error appeared again. A quick search through some custom module code revealed a .js file that was trying to create a variable from #sidebar-left and #sidebar-right. Once that was corrected, the null array error was gone, and I am now modifying $vars['classes_array'] at will and adding custom page templates to the theme as required.

Thanks :)

How can I do this in D7

erok415's picture

I would like achieve the same as you and have tried your code in my D7 install. Unfortunately, it's not working. It seems that this code may be for D6.

/*This function allows other page templates to work.
*Example: If I want to create a new template page called page--3col.tpl.php. I just need to create the template and it will work.
*/
function framework_process_page(&$variables) {
    // page
    // Renders a new page template to the list of templates used if it exists
    if (isset($variables['node'])) {
        if ($variables['node']->type != "") {
            $variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
        }
        if ($variables['node']->type == 'page1column') {
            $variables['sidebar_second'] = '';
            $variables['sidebar_first'] = '';
            if (($pos = array_search('one-sidebar', $variables['classes_array'])) !== false) { unset($variables['classes_array'][$pos]); }
            if (($pos = array_search('two-sidebar', $variables['classes_array'])) !== false) { unset($variables['classes_array'][$pos]); }
            if (($pos = array_search('sidebar-second', $variables['classes_array'])) !== false) { unset($variables['classes_array'][$pos]); }
            if (($pos = array_search('sidebar-first', $variables['classes_array'])) !== false) { unset($variables['classes_array'][$pos]); }
            $variables['classes_array'][] = 'no-sidebars';
            $variables['classes_array'][] = 'and-another';
        }
    }
}

Thanks,
E.

Director of Technology and Trainer at Molly Duggan Associates
Design, Build and Train for small business to enterprise/global brands.