umanit / easyadmin-tree-bundle
Plugin to add category tree features for EasyAdmin
Installs: 4 765
Dependents: 0
Suggesters: 0
Security: 0
Stars: 20
Watchers: 7
Forks: 7
Open Issues: 4
Type:symfony-bundle
Requires
- php: ^8.0
- doctrine/dbal: ^3.4
- doctrine/doctrine-bundle: ^2.7
- doctrine/orm: ^2.13
- easycorp/easyadmin-bundle: ^4.4
- gedmo/doctrine-extensions: ^3.9
- stof/doctrine-extensions-bundle: ^1.7
- symfony/config: ^5.4|^6.0
- symfony/dependency-injection: ^5.4|^6.0
- symfony/doctrine-bridge: ^5.4|^6.0
- symfony/form: ^5.4|^6.0
- symfony/options-resolver: ^5.4|^6.0
- symfony/translation: ^5.4|^6.0
README
Plugin to add category tree features for EasyAdmin
Features
- Display of content's entries as a tree structure on index page
- Filter content's entries through a tree sidebar on index page
- Provide a TreeField to manage tree structures on forms
Installation
$ composer require umanit/easyadmin-tree-bundle
Configuration
The templates paths needs to be declared in your Twig configuration :
twig: paths: '%kernel.project_dir%/vendor/umanit/easyadmin-tree-bundle/Resources/views': UmanitEasyAdminTreeBundle
This bundle relies on StofDoctrineExtensionsBundle, so make sure it is configured properly : documentation. You need to :
- add tree extension to your mapping :
doctrine: orm: entity_managers: default: mappings: gedmo_tree: type: annotation prefix: Gedmo\Tree\Entity dir: "%kernel.project_dir%/vendor/gedmo/doctrine-extensions/src/Tree/Entity" alias: GedmoTree # (optional) it will default to the name set for the mapping is_bundle: false
- activate the tree extension :
stof_doctrine_extensions: default_locale: en_US orm: default: tree: true
Usage
Administer a category tree
Create you category entity by extending AbstractTreeItem :
<?php // src/Entity/Category.php namespace App\Entity; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Umanit\EasyAdminTreeBundle\Entity\AbstractTreeItem; #[ORM\Entity] #[ORM\Table(name: 'app_category')] class Category extends AbstractTreeItem { ... }
Create a CRUD controller for your categories by extending the TreeCrudController
<?php // src/Controller/Admin/MyTreeCrudController.php namespace App\Controller\Admin; use App\Entity\Category; use EasyCorp\Bundle\EasyAdminBundle\Config\Action; use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; use EasyCorp\Bundle\EasyAdminBundle\Field\IdField; use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; use Umanit\EasyAdminTreeBundle\Controller\TreeCrudController; use Umanit\EasyAdminTreeBundle\Field\TreeField; class MyTreeCrudController extends TreeCrudController { public static function getEntityFqcn(): string { // return your Category entity FQCN return Category::class; } protected function getEntityLabelProperty(): string { // return the property of your category to use as a label in tree display return 'name'; } }
Enable the Crud in your EasyAdmin Dashboard :
public function configureMenuItems(): iterable { return [ ... MenuItem::linkToCrud('Categories', 'fas fa-list', Category::class), ... ]; }
Add a tree sidebar
To enable the tree sidebar to an existing CRUDController, you have to make it extend AbstractCategorizedCrudController :
<?php // src/Controller/Admin/MediaCrudController.php namespace App\Controller\Admin; use App\Entity\Category; use App\Entity\Media; use Umanit\EasyAdminTreeBundle\Controller\AbstractCategorizedCrudController; class MediaCrudController extends AbstractCategorizedCrudController { public static function getEntityFqcn(): string { return Media::class; } public function configureCrud(Crud $crud): Crud { $crud = parent::configureCrud($crud); // ... } public static function getCategoryFqcn(): string { // The FQCN of the entity to use as a category (should extend AbstractTreeItem) return Category::class; } protected static function getCategoryPropertyName(): string { // the name of the category property for the entity managed by this CRUDController return 'category'; } protected function getDefaultCategoryId(): int { // The id of the category that will be used as default filter for the index page of your CRUD return $this->getCategoryRepository()->findOneBy(['parent' => null])->getId(); } }
Use TreeField in your forms
You just need to add the field in the EasyAdmin configureFields function :
<?php // src/Controller/Admin/MediaCrudController.php namespace App\Controller\Admin; use Umanit\EasyAdminTreeBundle\Field\TreeField; class MediaCrudController extends AbstractCategorizedCrudController { public function configureFields(string $pageName): iterable { return [ // ... TreeField::new('category', 'My category'), // ... ]; }
Screenshots
TODO
List of needed improvements :
- fold/unfold categories
- reorder category items with drag'n drop
- prefill category in TreeField with current category in the sidebar
- improve front (design, css)