I created a diagram outlining the issues here: http://www.erality.com/assets/link/phptemplatevars.gif

Ultimate goal:
Specify different node templates for pages (i.e. node/5) and blocks (i.e. nodes being retrieved for blocks [recent posts, etc]).

Summary
For the site I am working on, I will need to create a number of custom blocks. These blocks will be using node content in output. For example, a "recent posts" block. However, I am unable to specify which node template to use when a block retrieves nodes.

Attempt: I tried using phptemplate_variables(), however, was unsuccessful at determining whether the requested node was for a block or page (see code below) . The $hook variable is set to 'node' when a block retrieves a node.

Possible solution: Is a way to know if a node is being retrieved for a block or a page in the phptemplate_variables() function? Is this the wrong approach?

Any help would be greatly appreciated.

function _phptemplate_variables($hook, $vars) {
   switch($hook) {
      case "node": 
            $suggestions[] = 'nodePage';
            $vars['template_files'] = $suggestions; 
            break;
   }   
   return $vars; 
}

Comments

nevets’s picture

How do you populate the block with the nodes?

theabacus’s picture

In this case I am using views, however, some of the blocks will be created outside of views.

nevets’s picture

You can theme the view (see: http://drupal.org/node/42597). Here is a general solution assume you have a full node or teaser node view.

function phptemplate_views_view_nodes($view, $nodes, $type, $teasers = false, $links = true) {
  foreach ($nodes as $n) {
    $node = node_load($n->nid);
    $node->view_name = $view->name;
    $node->view_build_type = $view->build_type;
    $output .= node_view($node, $teasers, false, $links);
  }
  return $output;
}

When theming the node (in node.tpl.php for example) you could them check to see if $node->view_build_type iis equal to 'block'.

If you only want this information for one view you can change phptemplate_views_view_nodes() to phptemplate_views_view_nodes_VIEWNAME() replacing VIEWNAME with the name of your view.

theabacus’s picture

Sweet. That looks like it solves the problem if I am using views. However, some of the blocks I will be creating will be made outside of views. How do I solve the problem then?

nevets’s picture

In case where you have something like

    $node = node_load($nid);
    $output .= node_view($node, $teasers, false, $links);

You add add fields to $node like in the above example.

theabacus’s picture

Ah, pass a custom value. Righto, thanks for the help.