I have installed page_title-4.7.x-1.x-dev and flatforum-4.7.x-1.x-dev. The modules were tested and work flawlessly; however both require additions to templates.php.

The problem is that the two functions work when used separately but when both are added to templates.php I get a blank page when viewing the site. Please have a look at the code below; any help will be greatly appreciated.

<?php

/*
  page_title.module addition
*/
function _phptemplate_variables($hook, $vars) {
  $vars = array();
  if ($hook == 'page') {
  	if (module_exist('page_title')) {
      $vars['head_title'] = page_title_page_get_title();
  	}
  }
  return $vars;
}



/*
  flatforum.module addition
*/
// $Id: template.php,v 1.7 2006/05/11 18:17:29 ayman Exp $
function _phptemplate_variables($hook, $vars) {
  static $is_forum;
  $variables = array();
  if (!isset($is_forum)) {
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
      $nid = arg(1);
    }
    if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
      $nid = arg(2);
    }
    if ($nid) {
      $node = node_load(array('nid' => $nid));
    }
    $is_forum = ($node && $node->type == 'forum');
    _is_forum($is_forum);
  }
  if ($is_forum) {
    switch ($hook) {
      case 'comment' :
        $variables['template_file'] = 'node-forum';
        $variables['row_class'] = _row_class();
        $variables['name'] = $vars['author'];
        $variables['userid'] = $vars['comment']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['comment']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['comment']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['submitted'] = format_date($vars['comment']->timestamp);
        $subject = $vars['comment']->subject;
        $variables['title'] = empty($subject) ? '&nbsp' : $subject;
        $variables['content'] = $vars['comment']->comment;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
      case 'node' :
        $variables['row_class'] = _row_class();
        $variables['userid']=$vars['node']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['node']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['node']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['title'] = empty($vars['title']) ? '&nbsp' : $vars['title'];
        $variables['content'] = $vars['node']->body;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
    }
  }
  return $variables;
}
function _row_class() {
  static $forum_row = TRUE;
  $forum_row = !$forum_row;
  return $forum_row ? 'odd' : 'even';
}
function _is_forum($arg = NULL) {
  static $is_forum = FALSE;
  if ($arg) {
    $is_forum = $arg;
  }
  return $is_forum;
}

I have tried to research this but I'm new to drupal and know little more than basic php. Also I'm not sure if the code should resemble the following or wether or not the ending ?> tag should be included.

<?php 
//EXISTING CODE 
?>
<?php 
//NEW CODE 
?>

or

<?php
//code from #1
//code from #2
?>

Thanks guys,
ncweb

Comments

rootwork’s picture

In template.php you should have <?php at the very top and ?> at the very bottom. You can paste everything else in between, and remove any opening or closing PHP tags this or any other module (such as views) ever gives you.

rootwork’s picture

You might want to read this post on the dreaded blank page of death as it may help you track down other problems.

kevinwalsh’s picture

clicking on the above link currently gives ... the dreaded blank page of death. At first i thought this was a joke, but it seems like the same is true for all of http://devbee.com .
-kev

styro’s picture

is no doubt telling you that you can't redefine an existing function.

You'll need to combine both functions into one. Shouldn't be too hard - they both operate on different sets of hooks (ie page vs node and comment) so they won't conflict, but they are both written a little differently.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

linweb’s picture

You'll need to combine both functions into one. Shouldn't be too hard

I think that this is the problem as there is clearly a repeat of

function _phptemplate_variables($hook, $vars) {

I have tried by trial and error everything I know to do with no luck. Could anyone please point me in the right direction where to learn how to combine the funtions. I'm sure its a simple fix, but I'm really stuck here. I found only a few relevant topics and they seem to explain merging when both contain

<?php
function _phptemplate_variables($hook, $vars = array()) {

// code here

}
      break;
  }
  return $vars;
}
?>

Reference: http://drupal.org/node/54437

After several attempts to combine the two I got the page to display but without the function working.

Thanks again,
ncweb

styro’s picture

I can't test it though as I don't use either module.

I slotted the page hook from page_title into the hook checking section of flat_forum (which didn't have a page hook) and changed $vars to $variables to suit the naming flat_forum used.

<?php

function _phptemplate_variables($hook, $vars) {
  static $is_forum;
  $variables = array();
  if (!isset($is_forum)) {
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
      $nid = arg(1);
    }
    if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
      $nid = arg(2);
    }
    if ($nid) {
      $node = node_load(array('nid' => $nid));
    }
    $is_forum = ($node && $node->type == 'forum');
    _is_forum($is_forum);
  }
  if ($is_forum) {
    switch ($hook) {
      case 'page':
        // this is the page hook from the page_title module
        if (module_exist('page_title')) {
          $variables['head_title'] = page_title_page_get_title();
        }
        break;
      case 'comment' :
        $variables['template_file'] = 'node-forum';
        $variables['row_class'] = _row_class();
        $variables['name'] = $vars['author'];
        $variables['userid'] = $vars['comment']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['comment']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['comment']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['submitted'] = format_date($vars['comment']->timestamp);
        $subject = $vars['comment']->subject;
        $variables['title'] = empty($subject) ? '&nbsp' : $subject;
        $variables['content'] = $vars['comment']->comment;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
      case 'node' :
        $variables['row_class'] = _row_class();
        $variables['userid']=$vars['node']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['node']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['node']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['title'] = empty($vars['title']) ? '&nbsp' : $vars['title'];
        $variables['content'] = $vars['node']->body;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
    }
  }
  return $variables;
}
?>

I thought it odd that both modules emptied the $vars (or $variables) array before putting their own stuff in it.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

linweb’s picture

The code in your post resulted in another 'blank page' scenario.

I think maybe I will open a minor support issue at the one of the project pages. I am new to drupal and havent had much experience with adding PHPTemplate theme snippets to templates.php.

Your comment left me wondering:

I thought it odd that both modules emptied the $vars (or $variables) array before putting their own stuff in it.

So I headed over to the handbook at http://drupal.org/node/45471 and found it quite easy to combine several of the snippets provided there. I'm now looking for a few modules to test and see if I can find others that include

function _phptemplate_variables($hook, $vars) { and successfully combine the two.

I appreciate the replies guys, thanks.

Regards,
ncweb

styro’s picture

The code in your post resulted in another 'blank page' scenario.

You really need to see what the error is to diagnose and fix it. If PHP isn't writing its errors to the screen, you need to look in the web servers error log.

One guess - did the function I posted above replace BOTH previous functions? You can't have two function definitions for the same function name anywhere in a PHP application.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

linweb’s picture

I get the following error

Call to undefined function _is_forum() in /home/site/themes/web/template.php on line 17

After some searching I finnaly realized what was missing. I added the code below to the end one you provided...

function _row_class() {
  static $forum_row = TRUE;
  $forum_row = !$forum_row;
  return $forum_row ? 'odd' : 'even';
}
function _is_forum($arg = NULL) {
  static $is_forum = FALSE;
  if ($arg) {
    $is_forum = $arg;
  }
  return $is_forum;
}

Now they play nice together only the page_title.module doesnt function except on forum nodes. I get "topic | Site.com" on all pages and in forum I get "Site.com | topic tittle" as needed.

Any Ideas on what could be wrong?

Thanks so much for all your help.

Regards,
ncweb

styro’s picture

To move the page stuff outside $is_forum checks

<?php

function _phptemplate_variables($hook, $vars) {
  static $is_forum;
  $variables = array();
  if (!isset($is_forum)) {
    if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
      $nid = arg(1);
    }
    if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
      $nid = arg(2);
    }
    if ($nid) {
      $node = node_load(array('nid' => $nid));
    }
    $is_forum = ($node && $node->type == 'forum');
    _is_forum($is_forum);
  }
  if ($hook == 'page') {
    // this is the page hook from the page_title module
    if (module_exist('page_title')) {
      $variables['head_title'] = page_title_page_get_title();
    }
  }
  if ($is_forum) {
    switch ($hook) {
      case 'comment' :
        $variables['template_file'] = 'node-forum';
        $variables['row_class'] = _row_class();
        $variables['name'] = $vars['author'];
        $variables['userid'] = $vars['comment']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['comment']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['comment']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['submitted'] = format_date($vars['comment']->timestamp);
        $subject = $vars['comment']->subject;
        $variables['title'] = empty($subject) ? '&nbsp' : $subject;
        $variables['content'] = $vars['comment']->comment;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
      case 'node' :
        $variables['row_class'] = _row_class();
        $variables['userid']=$vars['node']->uid;
        $joined = module_invoke('flatforum', 'get_created', $vars['node']->uid);
        $variables['joined'] = $joined ? format_date($joined, 'custom', 'Y-m-d') : '';
        $posts = module_invoke('flatforum', 'get', $vars['node']->uid);
        $variables['posts'] = $posts ? $posts : 0;
        $variables['title'] = empty($vars['title']) ? '&nbsp' : $vars['title'];
        $variables['content'] = $vars['node']->body;
        $variables['links'] = empty($vars['links']) ? '&nbsp' : $vars['links'];
        break;
    }
  }
  return $variables;
}
?>

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

linweb’s picture

That did the trick, thank you. I noticed how you moved the section of code from page_title and will have to study the differences between the codes to better understand.

I read from a comment at http://drupal.org/node/42035

I do remember reading something about resetting a variable or something at the start of each php code snippit when you have more than one. I think it was posted by Dublin Drupaler. Maybe someone else can comment on this.

Is this possible in such a way that the snippets do not have to be combined? In other words, to close out the fist instance of a function so it can be redeclared later on in the script.

On a side note; as you probably gathered, I don't know much about php and have little knowledge of how things work in drupal. Coming from cms like postnuke and mambo I'm not used to having to dive into editing the php side of things. I have much to learn if I am going to succeed with drupal. A challenge yes but I hope not impossible!

I do appreciate all your time spent helping me with this topic.

Thanks,
ncweb

styro’s picture

Basically that function is part of the theme engine and allows the theme designer to override the variables that get passed to the theme templates. Just as an example: the page.tpl.php theme template has $footer variable that contains the footer content entered in the site settings page. If you wanted to override that value occasionally, you would use that function and check for when $hook equaled 'page' then change $vars['footer'] to whatever you wanted. That saves you having to put all that logic in the page.tpl.php template itself. If you leave that logic out of the templates it makes it easier for designers to edit them without getting confused by or screwing up the PHP.

Is this possible in such a way that the snippets do not have to be combined? In other words, to close out the fist instance of a function so it can be redeclared later on in the script.

No not really. Not at least without the phptemplate engine getting redesigned a bit. The engine never really intended for different modules to mess with it like that.

Generally the only people that would implement that function would be theme coders that wanted to alter the variables available to the templates.

On a side note; as you probably gathered, I don't know much about php and have little knowledge of how things work in drupal. Coming from cms like postnuke and mambo I'm not used to having to dive into editing the php side of things. I have much to learn if I am going to succeed with drupal. A challenge yes but I hope not impossible!

I just think you've been unlucky here. I've never used one (let alone two) contrib module before that required me to mess with that stuff. They probably needed to do it, but ideally modules and themes should be independent of each other.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

linweb’s picture

I may be going out on a limb here but to satisfy my curiosity I have to ask in reference to comment

I just think you've been unlucky here. I've never used one (let alone two) contrib module before that required me to mess with that stuff. They probably needed to do it, but ideally modules and themes should be independent of each other.

I'm not sure I follow, though both modules for 4.7.x did need additions to template.php. Could I be missing something; or from your comments above, couldn't it be safe to assume that they should work independently from one another and not require any duplicate of function _phptemplate_variables($hook, $vars) {.. ?

--
ncweb

styro’s picture

Strictly speaking modules and themes should be independent of each other. Modules should just involve adding or changing site functionality and themes are just for changing the way the site looks. In well designed systems these two aspects are separate things that don't mix.

If a module wants to alter your theme it is either poorly designed or it involves some rare corner case that it can't solve any other way. I've played with and looked at a huge number of Drupal modules over the years and never had to change the _phptemplate_variables() function for any of them. I've never used flat forum or page title obviously.

What I meant by bad luck was that you not only found one module that needed to do that which is bad enough but not too big a problem, but two modules that both wanted to do that and required some PHP gymnastics by both wanting to stomp over the same internal theme function. ie you had two modules that both wanted to patch your theme in different ways - I don't think there are many modules at all that require that so the bad luck was just the very rare coincidence of using two of them.

While the module subsystem of Drupal is well designed for multiple modules to coexist in the same page request, it is not the same story with themes - each page request is only intended to have one theme applied to it and the template variables are your themes sole responsibility. So there is no design consideration for multiple sets of template variables to be merged.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ