agencearcange / symfony-form-tree
create select field with indentation for tree structure
v2.0.1
2021-09-24 21:46 UTC
Requires
- php: >=7.4
- doctrine/common: ~3.1
- doctrine/orm: ~2.9
- gedmo/doctrine-extensions: ~v3.1
- symfony/doctrine-bridge: ~5.3
- symfony/form: ~5.3
- symfony/options-resolver: ~5.3
- symfony/property-access: ~5.3
Requires (Dev)
- ext-pdo_sqlite: *
- antiseptikk/dev-kit: ^2.0
- phpunit/phpunit: ~9.0
README
This is a fork from abandoned repo: https://github.com/Yavin/symfony-form-tree
This extension provide displaying doctrine tree entity types in symfony forms. It add a prefix to option names in select list that indicates tree level.
<select name="..." data-level-prefix="-"> <option value="1">Motors</option> <option value="2">Electronics</option> <option value="3">-Cell phones</option> <option value="4">--Samsung</option> <option value="5">-Computers</option> <option value="6">Fasion</option> </select>
Instalation
-
With composer.json
composer require agencearcange/symfony-form-tree
-
Add type guesser in your services file (optional):
<service class="Yavin\Symfony\Form\Type\TreeTypeGuesser"> <argument type="service" id="doctrine"/> <tag name="form.type_guesser"/> </service>
or if you use
yml
format:services: symfony.form.type_guesser.tree: class: Yavin\Symfony\Form\Type\TreeTypeGuesser arguments: [ "@doctrine" ] tags: - { name: form.type_guesser }
-
Then add field to tree model. In this example
use Yavin\Symfony\Form\Type\TreeType; public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('category'); //extension will guess field type //or this is full example with default options: $builder->add('category', TreeType::class, [ 'class' => Category::class, // tree class 'levelPrefix' => '-', 'orderFields' => ['treeLeft' => 'asc'], 'prefixAttributeName' => 'data-level-prefix', 'treeLevelField' => 'treeLevel', ]); }
This extension assume that in tree model You have
treeLeft
andtreeLevel
fields. It can be changed in field options.Here is example tree entity.