wieni / wmcontroller
Adds support for bundle-specific controllers for Drupal 9 entities.
Installs: 17 960
Dependents: 3
Suggesters: 1
Security: 0
Stars: 3
Watchers: 9
Forks: 4
Open Issues: 4
Type:drupal-module
Requires
- php: ^8.0
- drupal/core: ^9.1 || ^10 || ^11
- wieni/wmtwig: ^1.0
Requires (Dev)
- composer-runtime-api: ^2.0
- ergebnis/composer-normalize: ^2.0
- wieni/wmcodestyle: ^1.7
- dev-main
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-alpha3
- 2.0.0-alpha2
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.0
- 0.8.13
- 0.8.12
- 0.8.11
- 0.8.10
- 0.8.9
- 0.8.8
- 0.8.7
- 0.8.6
- 0.8.5
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-release/v1
- dev-feature/drupal-10
- dev-feature/drupal-10-no-split
- dev-feature/split
- dev-feature/split-pre
- dev-release/v0.6
This package is auto-updated.
Last update: 2024-12-12 20:31:29 UTC
README
Adds support for bundle-specific controllers for Drupal 8 entities.
Why?
- Improve the developer experience of the Entity API by providing the ability to render entities of different bundles in different ways.
- A new way of building layouts: gather your data in the controller and use it to render a Twig template. Inspired by Laravel and other MVC frameworks. Completely optional.
Installation
This package requires PHP 7.1 and Drupal 8 or higher. It can be installed using Composer:
composer require wieni/wmcontroller
You should also include the patch from #2638686 if you're getting early rendering errors in your controllers.
Configuration
Before you get started, make sure the theme or module that will hold your templates is configured to do so. Check the wmtwig documentation for more info.
Configuration is stored as service parameters. You can override these in a service YAML file defined in
$settings['container_yamls']
or in the services.yml
file of a (custom) module.
parameters: wmcontroller.settings: # The controller responsible for forwarding to bundle-specific controllers. # Only override this if you know what you're doing. frontcontroller: 'Drupal\wmcontroller\Controller\FrontController' # Throw a 404 NotFoundHttpException when an entity is not translated # in the current language. ( /en/node/123 gives 404 if node/123 has no # en translation ) 404_when_not_translated: true # Routes to never reroute through the front controller ignore_routes: []
How does it work?
Creating controllers
-
Create bundle-specific controllers by creating new classes with the following naming convention:
src\Controller\<entityType>\<bundle>Controller
(
<entityType>
and<bundle>
are singular and camelCased)For example:
src\Controller\TaxonomyTerm\CategoryController
will be matched against ataxonomy_term
with bundlecategory
. -
This module will always call the
show
method on the controller class. -
A
ControllerBase
class includingViewBuilderTrait
,MainEntityTrait
andRedirectBuilderTrait
is provided, but extending this class is not required.
Example
// src/Controller/Node/ArticleController.php <?php namespace Drupal\mymodule\Controller\Node; use Drupal\Core\Controller\ControllerBase; use Drupal\node\NodeInterface; class ArticleController extends ControllerBase { public function show(NodeInterface $node) { return [ '#theme' => 'article_node', '#node' => $node, ]; } }
Rendering Twig templates
Using the ViewBuilder
class, you can easily render Twig
templates without having to mess with render arrays.
This module automatically resolves view builders to render arrays, so it's safe to return instances of this class in controllers.
The easiest way of building views is using the view
method included in ControllerBase and ViewBuilderTrait. Just pass
the template name, any parameters and you're good to go.
The template name is the path to the template file, but with dots as path separators and without the file extension. Note that you can only use templates in the configured theme and path.
Example
// src/Controller/Node/ArticleController.php <?php namespace Drupal\mymodule\Controller\Node; use Drupal\Core\Controller\ControllerBase; use Drupal\mymodule\Entity\Node\Article; # See wieni/wmmodel class ArticleController extends ControllerBase { public function show(Article $article) { // Loads mytheme/templates/article/detail.html.twig return $this->view( 'article.detail', [ 'article' => $article, ] ); } }
Accessing the main entity
It's often useful to access the main entity of the current request, e.g. on canonical or edit routes.
It has always been possible to access this entity by extracting it from the route parameters of the current route match,
but the MainEntity
service makes that easier.
Apart from having easier access to the entity, it's also possible to manually set the main entity of custom routes
using the MainEntityTrait or the wmcontroller.main_entity
service directly.
If the wmpage_cache
module is installed, this main entity is also used to
determine cachability metadata of the current request.
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.