nivas/easyadmin-tree-list-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nivas/easyadmin-tree-list-bundle

Composer 安装命令:

composer require nivas/easyadmin-tree-list-bundle

包简介

Plugin for EasyAdmin to show nested tree in list view

README 文档

README

EasyAdmin 3.x / Symfony 5.x compatible bundle which overrides default EasyAdmin index (list) templates and adds nested tree view on list for entities that use Gedmo Tree extension.

EasyAdmin 3 tree list bundle demo

In order to achieve nested tree view, your entity must implement nested Gedmo tree on entity and following properties:

  • root_id property
  • parent_id property
  • lft (left) property
  • rgt (right) property
  • lvl (level) property

Example of such entity: https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/tree.md

Notable mentions and some history

On many Symfony 4 projects, we used 2lenet/EasyAdminPlusBundle's tree view feature. As we migrated projects to Symfony v5 / EasyAdmin v3 and Symfony v6 / EasyAdmin v4 - we lacked this simple tree view feature and decided to make tree view work again.

Hopefully bundle will work on PHP 8, Symfony 6 & EasyAdmin v4 as well (not yet tested).

History:

To achieve tree functionality in templates, really old jQuery treetable Plugin 3.2.0 was used from Ludo van den Boom, just like in 2lenet/EasyAdminPlusBundle bundle. Will check out how to replace it with something recent.

Installation

composer require nivas/easyadmin-tree-list-bundle

after installation your bundles.php will contain new bundle:

<?php

return [
...
    Nivas\Bundle\EasyAdminTreeListBundle\EasyAdminTreeListBundle::class => ['all' => true],
];

Configuration

As mentioned previously, your entity must implement Gedmo Tree.

Your EasyAdmin CRUD controller should override:

  • configureResponseParameters - used to enable tree config for twig
  • createIndexQueryBuilder - used to change sorting order needed to make a tree list
  • configureActions - used to remove batch actions in front of tree navigation arrow

Example of src/Controller/Admin/TermCrudController.php

<?php

namespace App\Controller\Admin;

use App\Entity\Term;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;

// za custom order query
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Orm\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
use EasyCorp\Bundle\EasyAdminBundle\Collection\FilterCollection;
use Doctrine\ORM\QueryBuilder;

// za ugasit akcije
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;

class TermCrudController extends AbstractCrudController
{
    public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
    {
        $responseParameters->set('tree', true);
        return $responseParameters;
    }

    // not needed, it looks better this way
    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->disable(Action::BATCH_DELETE);
    }    

    public function createIndexQueryBuilder(SearchDto $searchDto, EntityDto $entityDto, FieldCollection $fields, FilterCollection $filters): QueryBuilder
    {
        $qb = $this->get(EntityRepository::class)->createQueryBuilder($searchDto, $entityDto, $fields, $filters);
        $qb->resetDQLPart('orderBy');
        $qb->addOrderBy($qb->getRootAlias().'.root', 'ASC');
        $qb->addOrderBy($qb->getRootAlias().'.lft', 'ASC');
        return $qb;
    }

  ...

}

Easyadmin v2 to v3 migration

In EasyAdmin v2 you could access value of entity property by typing eg. entity.id. Not anymore. this was replaced by EasyAdmin's EntityDTO.

eg:

entity.fields.getByProperty('id').value

or eg: entity.root.id becomes: entity.fields.getByProperty('root').value.id

Also, befure it was easy to access custom property defined in yaml (eg. tree: true in example below):

easy_admin:
  entities:
    Organization:
      class: App\Entity\Organization
      label: 'Organization'
      tree: true
      form:
        fields:
          - name
  ...

In templates you would just use entity.tree to access it.

Now this is done by overriding configureResponseParameters of your admin crud class which extends AbstractCrudController:

use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;

class OrganizationCrudController extends AbstractCrudController
{
    public function configureResponseParameters(KeyValueStore $responseParameters): KeyValueStore
    {
        $responseParameters->set('tree', true);
        // $responseParameters->setIfNotSet('bar.foo', '...');

        return $responseParameters;
    }

And usage in Twig template:

{% if tree is defined and tree %}
tree je true - {{ tree }}
{% endif %}

nivas/easyadmin-tree-list-bundle 适用场景与选型建议

nivas/easyadmin-tree-list-bundle 是一款 基于 Twig 开发的 Composer 扩展包,目前已累计 552 次下载、GitHub Stars 达 8, 最近一次更新时间为 2023 年 03 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「tree」 「list」 「easyadmin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nivas/easyadmin-tree-list-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nivas/easyadmin-tree-list-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 nivas/easyadmin-tree-list-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 552
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 4
  • Forks: 2
  • 开发语言: Twig

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-27