Find a parent of a certain type

Gabriel Radic - August 13, 2009 - 19:00
Project:Node Hierarchy
Version:6.x-1.2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Hello,

I am using Node Hierarchy extensively on this one web site, to create sections and for the main navigation. The structure looks something like this:

Product A           <<< product logo attached here     
  - Product Page A.1      <<< needs to appear here 
  - Product Page A.2
    + Some Page A.2.a     <<< and here  
    + Some Page A.2.b
  - Product Page A.3
Product B
...

The product page has a couple of images (as cck fields) that need to be be visible on the child pages as well. Basically, I need to find the closest parent of type `product` and use its `nid` in a view.

I was thinking there may be a way to do this with Views and arguments, but I don't have the skills to make that work. I also noticed this #256696: Top-most parent as argument in view, but the instructions are enough to put me on the right track. I even polished the code a bit to make it check for the `product` node type, but I don't know how to use this with the Views.

<?php
function phptemplate_get_oldest_ancestor($nid) {
 $n = node_load($nid);
    if (
$n->type == 'product') = return $n;
 $node_parent = $n->parent;
 if($n && $n->parent) {
    $n = theme('get_oldest_ancestor', $n->parent);
 }
}
?>

If anyone could help, I would really appreciate it. Thanks.

#1

crooke - August 14, 2009 - 16:13

So just for future reference, the way we solved this problem:

1) Modified the code a bit and instead of using a theming function, we put that in a module like that:

<?php

function custom_get_oldest_ancestor($nid) {

$n = node_load($nid);
if (
$n->type == 'product') {
    return
$n;
}

elseif(
$n && $n->parent) {
   
$n = custom_get_oldest_ancestor($n->parent);
}

return
$n;
}
?>

2) Built a views block to display the fields we need (2 image fields and a title) and added an argument Node: ID

3) Created a PHP block where we call our function and embed the view with the argument like that:

<?php
$arg
= custom_get_oldest_ancestor(arg(1));
print
views_embed_view('product_headers', 'block_2', $arg->nid);
?>

product_headers is the name of the view, block_2 is the ID of the views block within the view. See http://drupalcontrib.org/api/function/views_embed_view for more information on views_embed_view.

Rob

#2

crooke - August 14, 2009 - 16:11
Status:active» closed
 
 

Drupal is a registered trademark of Dries Buytaert.