Tree structures - Tree behavior and helper

Simple Example

Using `find(threaded)` on some demo categories.

echo $this->Tree->generate($tree);
  • Alpha
  • Beta
    • Child of 2nd one
      • Child of child
  • Gamma
  • Delta
    • Child of 4th one

Only generate 2 levels

echo $this->Tree->generate($tree, ['maxDepth' => 1]); // 0 based
  • Alpha
  • Beta
    • Child of 2nd one
  • Gamma
  • Delta
    • Child of 4th one

Using a callback to style

And passing in "authPath" for the helper to know the currently active element and the path to it.

echo $this->Tree->generate($tree, ['callback' => $callback, 'autoPath' => $autoPath]);

(See code for details on the callback function).

  • Alpha
  • Beta
    • Child of 2nd one
      • Child of child
  • Gamma
  • Delta
    • Child of 4th one

Displaying a custom array

Using "children" elements, you can also make trees out of any array.

echo $this->Tree->generate($tree, ['maxDepth' => 1]); // 0 based
  • Foo
    • Bar
    • Baz
      • Baz Child

The data for this is:

$data = [
    [
        'name' => 'Foo',
        'children' => [
            [
                'name' => 'Bar',
                'children' => [],
            ],
            [
                'name' => 'Baz',
                'children' => [
                    [
                        'name' => 'Baz Child',
                        'children' => [],
                    ],
                ],
            ],
        ],
    ],
];

Send your feedback or bugreport!