wieni/wmpresenter 问题修复 & 功能扩展

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

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

wieni/wmpresenter

Composer 安装命令:

composer require wieni/wmpresenter

包简介

Adds support for creating & injecting view presenters on top of entity classes

README 文档

README

Latest Stable Version Total Downloads License

Adds support for creating & injecting view presenters on top of entity classes

Why?

Presenters are a principle taken from Model-view-presenter, a software design pattern similar to Model-view-controller. We use it to transform data before displaying it. Some example use cases:

  • Concatenating names and prefixes/suffixes into a person's full title
  • Displaying a fallback image in case an image field is empty
  • Converting a set of opening hours to a format that's easier to consume in Twig

Installation

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

 composer require wieni/wmpresenter

How does it work?

Creating presenters

Presenter classes should implement PresenterInterface.

AbstractPresenter is the recommended base class, it provides magic methods allowing you to call methods of the entity class directly on the presenter class. The @mixin docblock can be used to tell IDE's about this behaviour. The @property docblock can be used if you don't like magic and prefer to call the entity's methods directly on the entity.

<?php

namespace Drupal\wmcustom\Entity\Presenter\Node;

use Drupal\wmcustom\Entity\Model\Node\Page as PageModel;
use Drupal\wmpresenter\Entity\AbstractPresenter;

/**
 * @mixin PageModel
 * @property PageModel $entity
 */
class Page extends AbstractPresenter
{
}

Presenters should be registered as services. It's important to set shared: false on the presenter service, otherwise all presenters of the same type will work with the same entity.

Presenters can be assigned to entities by making the entity class implement HasPresenterInterface. The getPresenterService method should return the presenter service ID.

Entities having presenters don't have to implement EntityInterface. Any class can be used.

Automatically injecting presenters

Entities are automatically converted to their presenter counterparts when including them in a Twig template. Some examples:

  • The entity is passed as argument to the view method of wmcontroller controllers.
  • The entity is passed to other Twig components using functionalities like include or embed.

Manually loading presenters

In code, presenters can be loaded using PresenterFactoryInterface::getPresenterForEntity.

In Twig, presenters can be loaded by passing the entity through the p or presenter filters. When passing an array of entities, all entities will be converted to their presenter counterparts.

Twig\Sandbox\SecurityError: Calling method on a <presenter> object is not allowed

Twig has a whitelist feature that prevent people from calling methods on unknown classes in Twig templates. In order to allow you to use presenters in Twig templates, you'll have to change the whitelist by adding the following to your settings.php:

$settings['twig_sandbox_whitelisted_classes'] = [
    \Drupal\wmpresenter\Entity\PresenterInterface::class,
];

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/wmpresenter 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-10