$categories = $this->categoryRepository->findAll()->toArray(); $categoriesTree = $this->buildTree($categories); $this->view->assign('categories', $categoriesTree);
public function buildTree($elements, $parentId = null) { $tree = array(); foreach ($elements as $element) { if ($element->getParent() == $parentId) { $children = $this->categoryRepository->findByParent($element->getUid()); if ($children) { $this->buildTree($elements, $element->getUid()); $element->setChildren($children); } $tree[] = $element; } } return $tree; }
<f:render partial="Field/CategoriesTree" arguments="{categories:categories}"/>
Field/CategoriesTree.html <ul> <f:for as="category" each="{categories}"> <li> {category.title} <f:if condition="{category.children}"> <f:render partial="Field/CategoriesTree" arguments="{categories:category.children}"/> </f:if> </li> </f:for> </ul>