eZ Community » Forums » General » EZ Publish Content Tree Traversal
expandshrink

EZ Publish Content Tree Traversal

EZ Publish Content Tree Traversal

Thursday 30 August 2012 5:41:25 pm - 3 replies

I need to traverse up the content tree to a parent at an unknown depth of a particular content class and retrieve information from this

So for example:

 

Home(Frontpage)

- About (Custom Class)

         - My InnerPage (Frontpage)

   - Anotherpage (Article)

 

I would like to retrieve information from the About object in the Article object, has anyone got any suggestions of how to do this

Friday 31 August 2012 11:04:56 pm

Hi Jon,

The path_string of your current node contains the NodeIDs of all the parents, so if working in templates you could loop through that, fetching one at a time with a class filter, to retrieve the content.

If you're working in PHP, you could create a custom fetch function, using eZContentObjectTreeNode::fetchNodesByPathString() , which takes the path_string as the first argument, and you could add a class filter to your fetch function.  See kernel/classes/ezcontentobjecttreenode.php for details.

Hope that helps,
Geoff 

Modified on Saturday 01 September 2012 12:54:35 pm by Geoff Bentley

Saturday 01 September 2012 3:38:40 pm

For me, a custom fetch is desired because I hate the overhead of the template language in cases like this.  However, template logic can be used to use a traditional control-break approach can be used:

 

Written from head - may need some tuning, but its the general layout for this approach.

{def  $target_node = false() {* used as the final node we want.. test for not false after we fall out of this code *}
        $test_node = $node.parent {* the first iteration of traversal up the tree *}
}
{while $test_node.node_id|ne(1)} {*break out if we have reached the top of the tree*} 
        {* If we find our target class, then assign it to our target node and break out *}
        {if $test_node.object.class_identifier|eq('YOUR_CLASS_IDENTIFIER_HERE')}
               $target_node = $test_node;
               {break}
        {/if}
        {* still here? Aw snap!  Lets walk up the tree some more. *}
        $test_node = $test_node.parent;
{/while} 
 
 
{* somewhere else in your code: *}
{if $target_node|ne(false())}
        {* So, we have a node somewhere in our parents that matches the test (class_identifier) *}
        {* Now go do something nice with it *}
{/if}
 

Modified on Saturday 01 September 2012 3:40:48 pm by David Ennis

Thursday 20 September 2012 12:00:24 pm

Hi Jon,

Did you find a good solution?  If so, what was it?  If you could post your solution here and mark this issue as resolved by clicking the icon next to the title, that would be useful for other community members.

Cheers,
Geoff 

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from