[TYPO3-dev] 4.5 pagetree API to get page uids of all subpages of a page

Stefan Galinski sgalinski at df.eu
Mon Mar 28 22:45:24 CEST 2011


Steffen Müller wrote:

> Hi Stefan,
> 
> thanks for your feedback.
> 
> On 28.03.2011 18:50 Stefan Galinski wrote:
>>
>> Your solution looks fine if you just have a single tree level. Otherwise
>> you need to collect the pids recursivly as each node inside the node
>> collection can have again child nodes.
>>
> 
> I though this would have been done automatically be getNodes() since it
> has recursive logic inside.
> I don't have a clue what you mean. How do I achieve recursion?
> 
> 

Hi Steffen,

Basic Structure:
The whole tree code is based on the same structures known from ExtBase. The 
data provider with the getNodes method is the pagetree repository, the node 
collection is just an object storage and the single nodes are the domain 
entities. Maybe this helps to understand the sense of the different classes.

GetNodes:
The getNodes method just maps the database results inside a simple object 
model by what you can access and use the tree structure in a object-oriented 
way. You want to collect the id's in a flat array instead of a tree. 
Unfortunately there isn't a method to transform the tree structure into a 
flat array, so you need to write it yourself.

Code:
You can use the basic structure from the getNodes method to iterate thru 
your nodes. Here is some untested code...

protected function tranformTreeStructureIntoFlatArray($nodeCollection, 
$level = 0) {
  if ($level > 9) {
    return array();
  }

  $pids = array();
  foreach ($nodeCollection as $childNode) {
    $pids[] = $childNode->getId();
    if ($childNode->hasChildNodes()) {
      $pids = array_merge($pids, $this-
>tranformTreeStructureIntoFlatArray($childNode->getChildNodes(), $level + 
1);
    } else {
      $pids[] = $childNode->getId();
    }
  }

  return $pids;
}

[...]
$node = t3lib_div::makeInstance('t3lib_tree_pagetree_Node', (array) 
$nodeData);
$dataProvider = t3lib_div::makeInstance('t3lib_tree_pagetree_DataProvider');
$nodeCollection = $dataProvider->getNodes($node);
$this->tranformTreeStructureIntoFlatArray($nodeCollection);
[...]

-- 
Stefan Galinski
staatl. geprüfter Informatiktechniker




More information about the TYPO3-dev mailing list