Help with merging php sniplet in template.php

cpract - August 8, 2007 - 11:24

I am trying to merge three code sniplets in template.php to modify the output of my drupal site. I am trying to get the font size module, flatforum module and service link to work together. I got it working, but there is some issues remaining regarding getting the font size module and the flatforum module to work together. I have narrowed it down to a probable error in merging the code sniplets in template.php.

These are the code sniplets I would like to merge under "function _phptemplate_variables ($hook, $vars = array ())"

For the font size module:

function _phptemplate_variables($hook, $vars) {
switch ($hook) {
case 'page':
      global $user;

if (module_exists('fontsize')) {
        foreach (array('fontsize_init','fontsize_links') as $region) {
          $vars[$region] = drupal_get_content($region);
        }
      }
  // node_style module is installed and active
  if (module_exists('node_style')) {
    return node_style_return_vars($hook, $vars);
  }
 
else {
return $vars;
  }

for the flatforum module:

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;
}

...and for the Service Link module:

function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {

case 'node': 

  if (module_exists('service_links')) {
        $vars['service_links'] = theme('links', service_links_render($vars['node'], TRUE));
      }

I have yet not figured out the exact order these code sniplets should be ordered under function _phptemplate_variables. At one time the font size module is working, but not the flatforum and vice versa. The Service Links module is however always working.

Any suggestions?

-deleted-

cpract - August 8, 2007 - 12:45

-deleted-

Well I figured this out by

cpract - August 8, 2007 - 12:44

Well I figured this out by myself. Maybe of some help to another newbie:

function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {

case 'page':
      global $user;

if (module_exists('fontsize')) {
        foreach (array('fontsize_init','fontsize_links') as $region) {
          $vars[$region] = drupal_get_content($region);
        }
      }
  // node_style module is installed and active
  if (module_exists('node_style')) {
    return node_style_return_vars($hook, $vars);
  }

else {
return $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;


   }

switch ($hook) {

case 'node': 

  if (module_exists('service_links')) {
        $vars['service_links'] = theme('links', service_links_render($vars['node'], TRUE));
      }

}

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;
}

Thank you so much for

miasma - January 23, 2008 - 07:08

Thank you so much for posting that - I was having a hell of a time figuring out the whole merging functions thing and this did the trick!

 
 

Drupal is a registered trademark of Dries Buytaert.