Remove title, timestamp and author from related content

j0hn-smith - February 7, 2008 - 05:20
Project:RelatedContent
Version:5.x-1.6
Component:Miscellaneous
Category:support request
Priority:normal
Assigned:TBarregren
Status:closed
Description

relatedContent is excellent, well done.

How can I only display the body of the related node instead of the full node?

In relatedcontent.tpl.php I've tried adding
print_r($content);

after
echo content;

but it gives me the whole node again. I don't know enough php or Drupal to go any further.

Thanks

#1

j0hn-smith - February 7, 2008 - 06:33

I found issue #215123: Title only listing of related nodes in settings where you suggest some sudo code, I'll try that with with $body instead of $title.

#2

TBarregren - February 7, 2008 - 08:43
Assigned to:Anonymous» TBarregren

Yes, the pseudo code in #215123 is a good starting point to accomplish what you want. I included a small but powerful API in RelatedContent just for that purpose. The interesting function is

<?php
  relatedcontent
($node, $output_grouped, $content_function, $content_function_args)
?>

As described in the modules handbook, relatedcontent() returns an array whose keys are the names by which the output should be grouped according to $output_grouped, i.e. names of content types, authors, views or 'all', and whose values are also arrays of the return values of calling $content_function with the node objects with related content of $node as the first argument and $content_function_args as the following arguments.

For an example, calling

<?php
  relatedcontent
($node, 'type', 'my_function', array($arg_1, $arg_2));
?>

returns something similar to this:

<?php
 
array(
   
'story' => array(
      [
0] => my_fynction($nodes[4], $arg_1, $arg2),
      [
1] => my_fynction($nodes[12], $arg_1, $arg2),
      ...
      [
m] => my_fynction($nodes[67], $arg_1, $arg2),
    ),
   
'page' => array(
      [
0] => my_fynction($nodes[1], $arg_1, $arg2),
      [
1] => my_fynction($nodes[45], $arg_1, $arg2),
      ...
      [
n] => my_fynction($nodes[78], $arg_1, $arg2),
    )
?>

where $nodes[#] is the node object next in turn, according to its source view's sort order, whithin its group.

The only argument that is mandatory is $node, which is the node id or node object of the node whose related content is to be returned. The default value of $output_grouped is false, which results in a single group called 'all'. The default value of $content_function is a function that merely returns the node object itself. The default value of $content_function_args is en empty array, i.e. no arguments.

If you are not using the API, but just rely on the default behavior of the module, that corresponds to following call:

<?php
  relatedcontent
($node, $output_grouped, 'node_view', array($output_teasers))
?>

where $output_grouped and $output_teasers is set according to the settings of the content type of the node given by $node. The function node_view() used as the content function, is Drupal's standard function for generating the display of a node as either a full page or a teaser.

The fact that RelatedContent uses node_view() as it content function implies that the styling of the bodies or teasers of the related content themselves must be done through the regular theming of nodes, i.e. by overriding theme_node() or changes in node.tpl.php file.

The documentation of the function in source code documentation of relatedcontent() gives some examples of how it can be used. See also the documentation.

#3

j0hn-smith - February 7, 2008 - 09:34

Wow, thanks for replying so fast.

I know that the API can be used to achieve what I want but even though your code is commented perfectly in the module source code, my programming skills aren't sufficient to understand anything but step by step examples, eventually I get there by trial and error. I must learn about using API's.

For others needing similar functionality here's a copy of my post with working code originally posted here #215123: Title only listing of related nodes in settings (apologies duplicating the issue).

I needed to display only the body so I've developed the first pseudo code example above into working code;

<?php
 
if (arg(0) == 'node' && is_numeric($nid = arg(1))) {
   
$nodes = relatedcontent($nid);

    foreach(
$nodes as $node) {

       
$nodeobject = $node[0];
    echo
$nodeobject->body;

    
/*echo "<pre>";     
     print_r($node);
     echo "</pre>"; */
   
}

  }
?>

For title replace echo $nodeobject->body with echo $nodeobject->title.

You don't need the commented out part (between /* and */), I left it in so you can see all the possible attributes that you can choose to display. Set the nodes per page or block to 1 in the view before uncommenting the commented code or you will get the details of every related node the view returns (you don't have to do this but you'll see why if you don't).

Hope this is useful, I need this feature and it's taken me a while to get this far.

#4

TBarregren - February 7, 2008 - 10:03
Status:active» fixed

I have expanded your example in this comment http://drupal.org/node/215123#comment-720582.

#5

Anonymous (not verified) - February 21, 2008 - 10:12
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.