Add variable to node before node_view() to allow custom theming

Michelle - February 2, 2008 - 18:57
Project:RelatedContent
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:TBarregren
Status:closed
Description

This is a spin-off of http://drupal.org/user/23570

There is currently no way to use a .tpl file to theme nodes only when they are being shown as related content. To do this, we need to be able to identify from _phptemplate_variables that this node is part of the r.c.

To do this needs a line added to the relatedcontent.module at line 1182:

$n->relatedcontent = $node->nid;

So it looks like:

<?php
     
if ($nid && ($n = node_load($nid)) && node_access ('view', $n)) {
       
$n->relatedcontent = $node->nid;
       
$args[0] = $n;
?>

You use this in the "node" hook of _phptemlate_variables in template.php. Here is an example:

<?php
   
if (is_set($vars['node']->relatedcontent)) {
     
$vars['template_file'] = "newsletter-teaser";
    }
?>

This says to use the template file newsletter-teaser.tpl.php for the node only when the node is being viewed as related content. This allows you to have teasers for the nodes themed differently when shown in other parts of the site. Because you have the NID of the parent node, you can get fancier, but this is a simple example.

To show just the title and the teaser without the tags, author, etc, create a file called newsletter-teaser.tpl.php in your theme directory, and put this in it:

<?php
print l($node->title, "node/$node->nid");
print
$node->teaser;
?>

Michelle

#1

TBarregren - February 2, 2008 - 23:00
Assigned to:Anonymous» TBarregren

I will include this in next version of RelatedContent.

#2

TheoRichel - February 3, 2008 - 16:24

Michelle, I do not understand, my PHPTemplate (Channel9) does not contain a line with _phptemplate_variables.
If I just copy your lines to that file I get errors, I
f I copy this from the Drupal-site:

<?php
function  _phptemplate_variables($hook, $vars) {
   switch(
$hook) {
     case
'comment' :
       
$vars['newvar'] = 'new variable';
       
$vars['title'] = 'new title';
        break;
   }
   return
$vars;
}
?>

Then I am confused where to merge this with your code. Can you please help?

Many thanks,

Theo Richel

#3

Michelle - February 3, 2008 - 16:38

I've got an explanation of that function in my advprofile docs here: http://drupal.org/node/207841

Michelle

#4

TheoRichel - February 3, 2008 - 17:09

Michelle, thank you very much! Your explanation is very clear. However I still get this error message:

Fatal error: Call to undefined function is_set() in /home/www/klimatosoof/sites/default/themes/themagrk/template.php on line 76

This is the code I made, based on your suggestions:

function _phptemplate_variables($hook, $vars) {
if (is_set($vars['node']->relatedcontent)) {
$vars['template_file'] = "newsletter-teaser";
}
return $vars;
}

Many thanks again

#5

Michelle - February 3, 2008 - 18:23

Hmm... Wierd. That's a PHP function. Try just if ($vars['node']->relatedcontent) { . That should just return true if $node->relatedcontent is non 0 but I'm not sure how it will react if the var doesn't exist and I'm not in a position to test right now.

Michelle

#6

TheoRichel - February 3, 2008 - 18:45

Yes!
That did it. Many thanks. I suppose I can take it from here.
BTW, where IS the Couleeregion

Thx

TR

#7

Michelle - February 3, 2008 - 19:51

It's the area around La Crosse, Wisconsin.

Glad you got it working. :)

Michelle

#8

TheoRichel - February 4, 2008 - 17:16

Well almost, allow me one more question. I have been struggling all day with it. Here: http://www.groenerekenkamer.nl/node/164 is my crude version of a newsletter. At a height of about 10 cm you see the word 'story', now how do I get rid of that?

Many thanks,

Theo Richel

#9

TBarregren - February 4, 2008 - 17:22

The heading 'Story' is the title of the output group. You can either remove it from the template, or turn grouping off at admin/content/types/newsletter.

#10

TheoRichel - February 4, 2008 - 19:38

OK, done, thank you.

TR

#11

TBarregren - February 4, 2008 - 19:45
Status:active» fixed

#12

Anonymous (not verified) - February 18, 2008 - 19:51
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#13

TBarregren - March 2, 2008 - 09:53
Status:closed» fixed

I have added Michelle's suggested change to the head of the DRUPAL-5 branch. Thus the upcoming nightly development snapshot, 5.x-1.x-dev, will include it.

(I noticed that I already once have checked this issue fixed. That was obviously a mistake. I am sorry.)

#14

Anonymous (not verified) - March 16, 2008 - 10:02
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#15

TBarregren - September 22, 2008 - 06:35

Version 1.7 of RelatedContent includes this feature request.

 
 

Drupal is a registered trademark of Dries Buytaert.