Skip to content

Commit ddc9574

Browse files
author
Charlie
committed
add docs for choice_tree
1 parent 2501543 commit ddc9574

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ for use when rendering templates.
6060

6161
For more information see the [Twig Helper][].
6262

63+
Choice Tree
64+
--------------
65+
66+
The choice Tree form Type allows have a tree for choices.
67+
68+
For more information see the [Choice Tree Documentation][].
69+
6370
[PolyCollection Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/polycollection.md
6471
[Collection Helper Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/collection-helper.md
6572
[CheckboxGrid Documentation]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/checkboxgrid.md
6673
[Twig Helper]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/twig-helper.md
74+
[Choice Tree Documentation]: https://github.com/Charlie-Lucas/InfiniteFormBundle/blob/feature/add-choice-tree-form-type/Resources/doc/choice_tree.md
6775
[can be found here]: https://github.com/infinite-networks/InfiniteFormBundle/blob/master/Resources/doc/installation.md

Resources/doc/choice_tree.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
InfiniteFormBundle's Choice Tree Form Type
2+
=============================================
3+
4+
Introduction
5+
------------
6+
7+
The choice tree form type allow you to display a tree of choice, a simple
8+
choice list give you the possibility to generate group but just for one level.
9+
10+
Installation
11+
------------
12+
13+
[Installation of InfiniteFormBundle](installation.md) is covered in a separate
14+
document. The Choice Tree type is automatically enabled when the bundle is
15+
installed.
16+
17+
Object Structure
18+
----------------
19+
20+
The Choice Tree type work as a choice but choices must implement a specific pattern:
21+
```php
22+
$tree = {
23+
0 => [
24+
"value" => 1,
25+
"label" => Object/string,
26+
"choice_list" =>{
27+
0 => [
28+
"value" => 3,
29+
"label" => Object/string,
30+
"choice_list" => []
31+
]
32+
}
33+
],
34+
1 => [
35+
"value" => 2,
36+
"label" => Object/string,
37+
"choice_list" =>{
38+
0 => [
39+
"value" => 1,
40+
"label" => Object/string,
41+
"choice_list" => []
42+
]
43+
}
44+
]
45+
}
46+
$builder
47+
->add(
48+
'category', 'infinite_form_choice_tree', [
49+
'choices' => $tree,
50+
]
51+
);
52+
53+
```
54+
For example when you use the Nested Tree Repository from Gedmo :
55+
56+
```php
57+
class MyFormType extends AbstractType
58+
{
59+
public function buildForm(FormBuilderInterface $builder, array $options)
60+
{
61+
$tree = $myRepo->buildTree($myRepo->getNodesHierarchy());
62+
// then we need recursively parse this tree for the form with a recursive function
63+
$builder
64+
->add(
65+
'category', 'infinite_form_choice_tree', [
66+
'choices' => $this->rebuildTree($tree),
67+
]
68+
);
69+
70+
}
71+
72+
73+
public function rebuildTree($tree)
74+
{
75+
$hierarchy = [];
76+
foreach ($tree as $children) {
77+
$node = [];
78+
$node['label'] = $category['title'];
79+
$node['value'] = $category['id'];
80+
$node['choice_list'] = $this->rebuildTree($children);
81+
$hierarchy[] = $node;
82+
83+
}
84+
return $hierarchy;
85+
}
86+
}
87+
```
88+
89+
#TODO
90+
----------------
91+
92+
L'utilisation de paramètres dans le form type devraient permettre de parser ce qui est passer par "choices",
93+
pour éviter ainsi d'avoir à implémenter une méthode de parsing

0 commit comments

Comments
 (0)