Walk up nav tree to find value of input field

I have an input field on my navon and navtree. I need to figure out how to walk up the tree to find the value of this field.

The field is holding a collection name. There are going to be sections of the site where this collection name will change and other section that need the value from the navtree.

Example:
Root Folder (navTree contains default value)

  • Subfolder 1 (navon contains a different value)
    • page 1 will use value from navon
      -SubFolder 1.1 (navon has no value)
      -page 1.1 will use value from Subfolder One
  • Subfolder 2 (navon has no value)
    • Page 2 will use value from NavTree

Not sure if this makes sense, let me know if this isn’t clear and I"ll try to figure out a better example. But otherwise if you have some thoughts, please let me know.

Thanks everyone,

Shane

Do you have the PSO Toolkit installed? If so, you may find the following method to be helpful: $user.psoNavTools.getAncestors(selfNode)

Using that you could then do something like

  • for each item in ancestors
    – check break boolean if true, do nothing if false continue to next step
    – check for value in item, if found set break boolean to true, output value

Hm, I’ve tried replying 3 times now and its failing, so I’ll try again.

Thanks for the reply Jitendra, what is a break boolean?

oh…just a variable to check to see if you need to keep trying to find the value…I believe you might be able to actually use #break (but it really depends on how you feel about using that to break out of the loop)

Thanks for the help, here’s what I have but its not quite working.

#set($ancestorsNodes=$user.psoNavTools.getAncestors($nav.self))
#if($nav.self!=$nav.root)
  #foreach( $i in $ancestorsNodes )
    #if ($i.hasProperty("searchCollection"))
	#set($searchcollection = $i.getProperty("searchCollection").String)
	${searchcollection}
	#break
    #end
  #end
#else
  #set($searchcollection = $nav.root.getProperty("rx:searchCollection").String)
  2 ${searchcollection}
#end

But it looks like its coming down the tree as opposed to up the tree. When it gets to the IF statement it seems to be reading the root, so it passes as true and displays that value and then breaks. What am I missing?

SHane

OH, I think I might have solved it just as I was posting my last reply. Here’s the code:

#set($ancestorsNodes=$user.psoNavTools.getAncestors($nav.self))
	  #if($nav.self!=$nav.root)
	    #foreach( $i in $ancestorsNodes )
	       #set($node=$ancestorsNodes.get($i.Depth))
	       #if($i.Depth>0)##NavTree does not have the property
		  #if ($node.hasProperty("searchCollection"))
		     #set($searchcollection = $node.getProperty("searchCollection").String)
		     ${searchcollection}
		     #break
		  #end
		#end	   
             #end
	#else
		#set($searchcollection = $nav.root.getProperty("rx:searchCollection").String)
		2 ${searchcollection}
	#end

Does anyone see any flaws in this method that I might be missing?

Shane

There is a $user.psoListTools.reverse(list) that will reverse the list for you. Also if you need to check your content type, I would do it via getNode().getPrimaryNodeType().getType() as opposed to relying on the assumption (which may or may not be true) that the root is going to be a navtree item.

Oh, fyi, note that in the generated code, you’ll have a large amount of whitespace (when the ${searchcollection} is displayed). I realize that this is development code and thus should look pretty, but just letting you know…

Thanks for the help Jitendra. I’ve got it working for now but I will look at the other functions you mentioned to see if I can do this a little differently or more efficiently when I get some time.

In case anyone would like it, here’s the completed code.

#set($ancestorsNodes=$user.psoNavTools.getAncestors($nav.self))
	#if($nav.self!=$nav.root)
		#foreach( $i in $ancestorsNodes )
			#set($node=$ancestorsNodes.get($i.Depth))
			#if($i.Depth>0)##NavTree does not have the property
				#if ($node.hasProperty("searchCollection"))
					#set($searchcollection = $node.getProperty("searchCollection").String)
					##${searchcollection} ##used for testing only
					#break
				#else
					#set($searchcollection = $nav.root.getProperty("rx:searchCollection").String)
					##2 ${searchcollection} ##used for testing only
##					#break
				#end
			#end	   
		#end
	#else
		#set($searchcollection = $nav.root.getProperty("rx:searchCollection").String)
		##2 ${searchcollection} ##used for testing only
	#end