2017 © Pedro Peláez
 

drupal-module wmcontroller

Some helpers for building controllers in Drupal 8.

image

wieni/wmcontroller

Some helpers for building controllers in Drupal 8.

  • Wednesday, June 20, 2018
  • by Wieni
  • Repository
  • 8 Watchers
  • 4 Stars
  • 1,206 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 49 Versions
  • 23 % Grown

The README.md

Wieni Controller

Latest Stable Version Total Downloads License, (*1)

Adds support for bundle-specific controllers for Drupal 8 entities., (*2)

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:, (*3)

 composer require wieni/wmcontroller

You should also include the patch from #2638686 if you're getting early rendering errors in your controllers., (*4)

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., (*5)

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., (*6)

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:, (*7)

    src\Controller\<entityType>\<bundle>Controller, (*8)

    For example: src\Controller\TaxonomyTerm\CategoryController will be matched against a taxonomy_term with bundle category., (*9)

  • This module will always call the show method on the controller class., (*10)

  • A ControllerBase class including ViewBuilderTrait, MainEntityTrait and RedirectBuilderTrait is provided, but extending this class is not required., (*11)

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., (*12)

This module automatically resolves view builders to render arrays, so it's safe to return instances of this class in controllers., (*13)

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., (*14)

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., (*15)

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., (*16)

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., (*17)

If the wmpage_cache module is installed, this main entity is also used to determine cachability metadata of the current request., (*18)

Changelog

All notable changes to this project will be documented in the CHANGELOG file., (*19)

Security

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

License

Distributed under the MIT License. See the LICENSE file for more information., (*21)

The Versions

20/06 2018

dev-master

9999999-dev

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

20/06 2018

0.6.3

0.6.3.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

20/06 2018

0.6.2

0.6.2.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

09/05 2018

0.5.7

0.5.7.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

30/04 2018

0.6.1

0.6.1.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

27/04 2018

dev-feature/drush-9

dev-feature/drush-9

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

16/03 2018

0.6

0.6.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

15/03 2018

dev-feature/maxage-service

dev-feature/maxage-service

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

25/01 2018

dev-feature/v1/include-cache-response-policy

dev-feature/v1/include-cache-response-policy

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

11/12 2017

0.5.6

0.5.6.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

22/09 2017

0.5.5

0.5.5.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

22/09 2017

dev-issue/0.5/fix-params-not-being-passed-from-presenter

dev-issue/0.5/fix-params-not-being-passed-from-presenter

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

21/09 2017

0.5.4

0.5.4.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

20/09 2017

dev-hotfix/0.5/purge-by-tag

dev-hotfix/0.5/purge-by-tag

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

20/09 2017

dev-tmp/logging

dev-tmp/logging

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

05/09 2017

0.5.3

0.5.3.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

05/09 2017

dev-hotfix/cache-onterminate-session

dev-hotfix/cache-onterminate-session

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

05/09 2017

0.5.2

0.5.2.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

05/09 2017

dev-feature/cache-manager-utils

dev-feature/cache-manager-utils

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

02/09 2017

0.5.1

0.5.1.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

02/09 2017

dev-feature/cache-role-rules

dev-feature/cache-role-rules

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

01/09 2017

0.5.0

0.5.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

01/09 2017

dev-hotfix/override-cache-control

dev-hotfix/override-cache-control

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

01/09 2017

dev-refactor/0.5/refactor-cache-purging

dev-refactor/0.5/refactor-cache-purging

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

10/07 2017

0.4.1

0.4.1.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

03/07 2017

dev-feature/presenters

dev-feature/presenters

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

09/06 2017

0.4.0

0.4.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

09/06 2017

dev-fix/v1/extra-cache-pickups

dev-fix/v1/extra-cache-pickups

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

14/03 2017

dev-feature/caching

dev-feature/caching

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

14/03 2017

0.3.1

0.3.1.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

14/03 2017

0.3.0

0.3.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

27/02 2017

dev-feature/to-response

dev-feature/to-response

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

23/02 2017

dev-feature/caching-route-rules

dev-feature/caching-route-rules

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

09/02 2017

dev-hotfix/pager

dev-hotfix/pager

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

07/02 2017

dev-hotfix/caching-session-start

dev-hotfix/caching-session-start

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

07/02 2017

dev-feature/refactor-and-caching

dev-feature/refactor-and-caching

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

07/02 2017

dev-feature/check-if-node-or-taxonomy-exist

dev-feature/check-if-node-or-taxonomy-exist

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

02/02 2017

dev-cache

dev-cache

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

02/02 2017

dev-hotfix/cs

dev-hotfix/cs

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

23/01 2017

0.2.6

0.2.6.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

23/01 2017

dev-feature/allow-theme-for-twig

dev-feature/allow-theme-for-twig

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

18/01 2017

0.2.5

0.2.5.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

17/01 2017

0.2.4

0.2.4.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

17/01 2017

0.2.3

0.2.3.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

17/01 2017

0.2.2

0.2.2.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

16/01 2017

dev-feature/header-elements

dev-feature/header-elements

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

12/01 2017

0.2.1

0.2.1.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

09/01 2017

0.2.0

0.2.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni

04/01 2017

0.1.0

0.1.0.0

Some helpers for building controllers in Drupal 8.

  Sources   Download

MIT

by Avatar Wieni