|
| 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