Skip to content

Commit fc5990f

Browse files
committed
Implemented TreeBehavior::findTreeList
1 parent 122c754 commit fc5990f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Model/Behavior/TreeBehavior.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TreeBehavior extends Behavior {
3939
'implementedFinders' => [
4040
'path' => 'findPath',
4141
'children' => 'findChildren',
42+
'treeList' => 'findTreeList'
4243
],
4344
'implementedMethods' => [
4445
'childCount' => 'childCount',
@@ -327,6 +328,21 @@ public function findChildren($query, $options) {
327328
]);
328329
}
329330

331+
public function findTreeList($query, $options) {
332+
return $this->_scope($query)
333+
->find('threaded', ['parentField' => $this->config()['parent']])
334+
->formatResults(function($results) use ($options) {
335+
$options += [
336+
'keyPath' => $this->_table->primaryKey(),
337+
'valuePath' => $this->_table->displayField(),
338+
'spacer' => '_'
339+
];
340+
return $results
341+
->listNested()
342+
->printer($options['valuePath'], $options['keyPath'], $options['spacer']);
343+
});
344+
}
345+
330346
/**
331347
* Removes the current node from the tree, by positioning it as a new root
332348
* and reparents all children up one level.

tests/TestCase/Model/Behavior/TreeBehaviorTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,52 @@ public function testFindChildrenException() {
158158
$query = $table->find('children', ['for' => 500]);
159159
}
160160

161+
/**
162+
* Tests the find('treeList') method
163+
*
164+
* @return void
165+
*/
166+
public function testFindTreeList() {
167+
$table = TableRegistry::get('MenuLinkTrees');
168+
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
169+
$result = $table->find('treeList')->toArray();
170+
$expected = [
171+
1 => 'Link 1',
172+
2 => '_Link 2',
173+
3 => '_Link 3',
174+
4 => '__Link 4',
175+
5 => '___Link 5',
176+
6 => 'Link 6',
177+
7 => '_Link 7',
178+
8 => 'Link 8'
179+
];
180+
$this->assertEquals($expected, $result);
181+
}
182+
183+
/**
184+
* Tests the find('treeList') method with custom options
185+
*
186+
* @return void
187+
*/
188+
public function testFindTreeListCustom() {
189+
$table = TableRegistry::get('MenuLinkTrees');
190+
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
191+
$result = $table
192+
->find('treeList', ['keyPath' => 'url', 'valuePath' => 'id', 'spacer' => ' '])
193+
->toArray();
194+
$expected = [
195+
'/link1.html' => '1',
196+
'http://example.com' => ' 2',
197+
'/what/even-more-links.html' => ' 3',
198+
'/lorem/ipsum.html' => ' 4',
199+
'/what/the.html' => ' 5',
200+
'/yeah/another-link.html' => '6',
201+
'http://cakephp.org' => ' 7',
202+
'/page/who-we-are.html' => '8'
203+
];
204+
$this->assertEquals($expected, $result);
205+
}
206+
161207
/**
162208
* Tests the moveUp() method
163209
*

0 commit comments

Comments
 (0)