Allow decorating route controllers

Created on 18 August 2021, over 3 years ago
Updated 24 February 2023, almost 2 years ago

Problem/Motivation

There are a number of issues where we want one module to be able to add to the output of another:

- 3rd-party should be able to alter the EntityListBuilder rows Needs work
- #2612222: Provide a better way to alter entity revision overview
- Content Translation Overview Controller - make overview table easier to extend Closed: duplicate

Various solutions are being suggested on those issues:

- alter hooks for the render array, invoked in the controller
- an event that is fired by the controller

Both of these solutions have a problem that they do not scale to all the other places where alteration of a controller's output is needed. We'd need to add more and more alter hooks or BuildEvents. Or add a Single All-Encompassing one of those, which would be a new version of D7's hook_page_alter() (urgh), or an event version of the same.

Steps to reproduce

Proposed resolution

What if we added a way for router controllers to be decorated?

We'd have something like this in routing.yml:

original.route.decorator:
  decorates: original.route
  defaults:
    _decorator: '\Drupal\mymodule\Controller\MyRouteDecorator'

MyRouteDecorator then becomes the class called for the route's output. It receives the render array from the decorated route's controller.

If multiple decorators are declared, they can use a weight property to set their order.

This would be an extension to Symfony's routing system, but I don't see this being adopted upstream, because Symfony doesn't have module-based architecture where we need one part of the system to be able to alter things made by another part.

This has been implemented as a proof of concept contrib module: https://www.drupal.org/project/route_decorator

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Feature request
Status

Closed: duplicate

Version

10.1

Component
Routing 

Last updated 3 days ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024