https://github.com/wieni/wmpresenter
Adds support for creating & injecting view presenters on top of entity classes
https://github.com/wieni/wmpresenter
drupal-8 drupal-9 drupal-entity drupal-module presenter
Last synced: 8 months ago
JSON representation
Adds support for creating & injecting view presenters on top of entity classes
- Host: GitHub
- URL: https://github.com/wieni/wmpresenter
- Owner: wieni
- License: mit
- Created: 2020-11-10T15:39:10.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-01-07T10:30:36.000Z (over 2 years ago)
- Last Synced: 2024-12-27T14:25:39.155Z (over 1 year ago)
- Topics: drupal-8, drupal-9, drupal-entity, drupal-module, presenter
- Language: PHP
- Homepage:
- Size: 238 KB
- Stars: 0
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Entity Presenter
======================
[](https://packagist.org/packages/wieni/wmpresenter)
[](https://packagist.org/packages/wieni/wmpresenter)
[](https://packagist.org/packages/wieni/wmpresenter)
> Adds support for creating & injecting view presenters on top of entity classes
## 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
This package requires PHP 7.1 and Drupal 8 or higher. It can be
installed using Composer:
```bash
composer require wieni/wmpresenter
```
## How does it work?
### 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.
```php
_ 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`:
```php
$settings['twig_sandbox_whitelisted_classes'] = [
\Drupal\wmpresenter\Entity\PresenterInterface::class,
];
```
## Changelog
All notable changes to this project will be documented in the
[CHANGELOG](CHANGELOG.md) file.
## Security
If you discover any security-related issues, please email
[security@wieni.be](mailto:security@wieni.be) instead of using the issue
tracker.
## License
Distributed under the MIT License. See the [LICENSE](LICENSE) file
for more information.