PHPackages                             wieni/wmpresenter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. wieni/wmpresenter

ActiveDrupal-module[Templating &amp; Views](/categories/templating)

wieni/wmpresenter
=================

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

1.0.1(2y ago)02.5k↓80%2[1 PRs](https://github.com/wieni/wmpresenter/pulls)MITPHPPHP ^8.0

Since Jun 28Pushed 2y ago2 watchersCompare

[ Source](https://github.com/wieni/wmpresenter)[ Packagist](https://packagist.org/packages/wieni/wmpresenter)[ RSS](/packages/wieni-wmpresenter/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (5)Versions (6)Used By (0)

Entity Presenter
================

[](#entity-presenter)

[![Latest Stable Version](https://camo.githubusercontent.com/ebb295369cde06210a6be1ad90af84467ce6def5b6ac1f2d30b095a3be0a9273/68747470733a2f2f706f7365722e707567782e6f72672f7769656e692f776d70726573656e7465722f762f737461626c65)](https://packagist.org/packages/wieni/wmpresenter)[![Total Downloads](https://camo.githubusercontent.com/50f8bee5a34851d10adff715245b471e03c653efda3f0dd4e9b6403f58d21fa9/68747470733a2f2f706f7365722e707567782e6f72672f7769656e692f776d70726573656e7465722f646f776e6c6f616473)](https://packagist.org/packages/wieni/wmpresenter)[![License](https://camo.githubusercontent.com/10c0c233b81afbd90182788a259900b595e9e8c0a7156bd3f98e70d1ee78dc5d/68747470733a2f2f706f7365722e707567782e6f72672f7769656e692f776d70726573656e7465722f6c6963656e7365)](https://packagist.org/packages/wieni/wmpresenter)

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

Why?
----

[](#why)

Presenters are a principle taken from [Model-view-presenter](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter), a software design pattern similar to [Model-view-controller](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). 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
------------

[](#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?
-----------------

[](#how-does-it-work)

### Creating presenters

[](#creating-presenters)

Presenter classes should implement [`PresenterInterface`](src/Entity/PresenterInterface.php).

[`AbstractPresenter`](src/Entity/AbstractPresenter.php) 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.

```
