定制 wieni/wmentity_overview 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

wieni/wmentity_overview

Composer 安装命令:

composer require wieni/wmentity_overview

包简介

Improved EntityListBuilders with support for paging, table sorting, table dragging, filtering, bulk actions, database queries and more.

README 文档

README

Latest Stable Version Total Downloads License

Improved EntityListBuilders with support for paging, table sorting, table dragging, filtering, bulk actions, database queries and more.

Why?

At Wieni, we're not a big fan of the Views module and for a couple of reasons:

  • we're programmers, we don't like to create functionality by clicking through the Drupal interface and especially not the bloated Views interface
  • we prefer to write our own database or entity queries, it gives us more flexibility when filtering and including non-entity field data and it makes it easier to optimize queries

That's why a couple of years ago we decided to disable the module altogether and we ended up with the EntityListBuilder based listings.

Those proved to be lacking in functionality and user friendliness soon enough, which is how we ended up creating this module: the perfect middle ground between Views and EntityListBuilder. It offers all features of the latter, plus the following:

  • Exposed filtering with a custom form
  • Behind-the-scenes filtering using custom database queries
  • Table sorting, configurable on a per-column basis
  • Bulk actions using core Action plugins
  • Override built-in entity listings with a custom one, on an entity type or bundle level
  • Override any route with a custom entity listing

Installation

This package requires PHP 7.1 and Drupal 8 or higher. It can be installed using Composer:

 composer require wieni/wmentity_overview

How does it work?

Create an overview

Overviews are Drupal plugins with the @OverviewBuilder annotation. Every annotation needs at least the entity_type and id parameters to function.

There are three base classes you can choose from:

Example

<?php

namespace Drupal\yourmodule\Plugin\EntityOverview;

/**
 * @OverviewBuilder(
 *     entity_type = "node",
 * )
 */
class NodeOverview extends FilterableOverviewBuilderBase
{
}

Override en existing entity listing

You can override the default entity listing with a custom overview by adding the override parameter to the annotation. In case the entity type is not recognised by this module, you can add the route_name and pass the route name of the entity listing instead.

It is also possible to override the entity listing only when a certain combination of filters is active. This way, you could for example add extra filters or table columns when your overview is filtered by a certain bundle.

Examples

<?php

namespace Drupal\yourmodule\Plugin\EntityOverview;

/**
 * @OverviewBuilder(
 *     id = "node",
 *     entity_type = "node",
 *     override = true,
 * )
 */
class NodeOverview extends FilterableOverviewBuilderBase
{
}
<?php

namespace Drupal\yourmodule\Plugin\EntityOverview;

/**
 * @OverviewBuilder(
 *     id = "redirect",
 *     entity_type = "redirect",
 *     route_name = "redirect.list",
 * )
 */
class RedirectOverview extends FilterableOverviewBuilderBase
{
}
<?php

namespace Drupal\yourmodule\Plugin\EntityOverview;

/**
 * @OverviewBuilder(
 *     id = "node.article",
 *     entity_type = "node",
 *     override = true,
 *     filters = {
 *         "type" = "article",
 *     },
 * )
 */
class ArticleOverview extends NodeOverview
{
}

Render an overview

When you create an overview without overriding an existing route, you will have to render it somewhere manually.

Creating an instance of an entity overview is done the same way as other Drupal plugins, by using the createInstance method of OverviewBuilderManager.

Another option is adding _entity_overview to the defaults section of your route definition, with as value the plugin id.

Example

yourmodule.content_overview.article:
    path: '/admin/content/article'
    defaults:
        _entity_overview: 'node.article'
        _title: 'Articles'
    requirements:
        _permission: 'administer nodes'
    options:
        _admin_route: TRUE

Filter storages

Entity overviews with exposed filter forms need a place to (temporarily) store their filter values. That's where filter storages come to play: an abstraction in the way these values are stored.

By default, two storage methods are included: query, which stores values as query parameters in the URL and session, which stores values in the session storage.

Custom storage methods can be added by creating a Drupal plugin with the @FilterStorage annotation and an id parameter, implementing FilterStorageInterface and optionally extending FilterStorageBase.

The default storage method is query, but this can be changed by adding a filter_storage parameter to @OverviewBuilder annotations.

Add bulk actions

Bulk actions can be added to your overview by implementing BulkActionOverviewBuilderInterface and the getBulkActionPlugins method. This method can return two kinds of arrays:

  • an associative array with the plugin IDs as keys and the labels as values
  • a flat array with plugin ID's

In the last case the default plugin labels will be used.

It is possible to attach a configuration form to your action plugin by implementing PluginFormInterface. In case you need access to the entities in your form validate and/or submit handlers, you can implement ActionPluginFormInterface instead. Note that in contrary to PluginFormInterface this is a custom interface, only supported by this module.

Hooks and events

hook_entity_overview_alter

This hook is only called when using overrides or when using the _entity_overview default in routes. An event equivalent to the hook is also provided: WmEntityOverviewEvents::ENTITY_OVERVIEW_ALTER

Examples
<?php

use Drupal\wmentity_overview\Annotation\OverviewBuilder;

function yourmodule_entity_overview_alter(OverviewBuilder $definition, array &$overview)
{
    if (!empty($overview['form'])) {
        $overview['form']['#attributes']['class'][] = 'custom-entity-overview__form';
    }

    $overview['table']['#attributes']['class'][] = 'custom-entity-overview__table';
}
<?php

namespace Drupal\yourmodule\EventSubscriber;

use Drupal\wmentity_overview\Event\EntityOverviewAlterEvent;
use Drupal\wmentity_overview\WmEntityOverviewEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EntityOverviewSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        $events[WmEntityOverviewEvents::ENTITY_OVERVIEW_ALTER][] = ['onAlter'];

        return $events;
    }

    public function onAlter(EntityOverviewAlterEvent $event): void
    {
        $overview = &$event->getOverview();

        if (!empty($overview['form'])) {
            $overview['form']['#attributes']['class'][] = 'custom-entity-overview__form';
        }
    
        $overview['table']['#attributes']['class'][] = 'custom-entity-overview__table';
    }
}

hook_entity_overview_alternatives_alter

This hook is only called in the OverviewBuilderManager::getAlternatives method. An event equivalent to the hook is also provided: WmEntityOverviewEvents::ENTITY_OVERVIEW_ALTERNATIVES_ALTER

Example

<?php

namespace Drupal\yourmodule\EventSubscriber;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\wmentity_overview\Event\EntityOverviewAlternativesAlterEvent;
use Drupal\wmentity_overview\OverviewBuilder\OverviewBuilderManager;
use Drupal\wmentity_overview\WmEntityOverviewEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EntityOverviewAlternativesSubscriber implements EventSubscriberInterface
{
    /** @var OverviewBuilderManager */
    protected $overviewBuilders;
    /** @var RouteMatchInterface */
    protected $routeMatch;

    public function __construct(
        OverviewBuilderManager $overviewBuilders,
        RouteMatchInterface $routeMatch
    ) {
        $this->overviewBuilders = $overviewBuilders;
        $this->routeMatch = $routeMatch;
    }

    public static function getSubscribedEvents()
    {
        $events[WmEntityOverviewEvents::ENTITY_OVERVIEW_ALTERNATIVES_ALTER][] = ['onTaxonomyAlternativesAlter'];

        return $events;
    }

    /**
     * Since taxonomy has a per-bundle overview, we get the bundle from
     * the route parameters and use it to add more possible alternatives.
     */
    public function onTaxonomyAlternativesAlter(EntityOverviewAlternativesAlterEvent $event): void
    {
        if (!$vocabulary = $this->routeMatch->getParameter('taxonomy_vocabulary')) {
            return;
        }

        if ($event->getDefinition()->getEntityTypeId() !== 'taxonomy_term') {
            return;
        }

        $filters = ['vid' => $vocabulary->id()];
        $alternatives = array_merge(
            $event->getAlternatives(),
            $this->overviewBuilders->getAlternativesByFilters($event->getDefinition(), $filters)
        );

        $event->setAlternatives($alternatives);
    }
}

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

Security

If you discover any security-related issues, please email security@wieni.be instead of using the issue tracker.

License

Distributed under the MIT License. See the LICENSE file for more information.

wieni/wmentity_overview 适用场景与选型建议

wieni/wmentity_overview 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.19k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 wieni/wmentity_overview 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 21.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-30