By user5429087


2019-05-13 22:27:23 8 Comments

I'm still relatively new to Drupal 8 and recently I came across an occasion to override the WebFormSubmissionListBuilder Controller.

I eventually found another project which had exactly the same need and they did it as below:

<?php
/**
 * Implements hook_entity_type_alter().
 *
 * @todo This alters all webform_submission entites but should alter only the ones in configured workflows.
 */
function academic_applications_entity_type_alter(array &$entity_types) {
  /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['webform_submission']->setListBuilderClass('Drupal\academic_applications\WebFormSubmissionListBuilderOverride');
}

While this works I can see it being a real problem if more than one module tried to override another module's controller this way. However, I couldn't find a better way so I ended up using their method for now.

Is there a known or "best practices" for doing this? I don't see any obvious plugin points in the WebFormSubmissionListBuilder controller's code so taking it over seems to be the only way to extend it as far as I can tell.

1 comments

@4k4 2019-05-15 07:33:24

To avoid conflicts with other modules you could alter the controller content in a kernel VIEW event similar to a form alter hook:

src/EventSubscriber/ControllerAlterSubscriber.php:

namespace Drupal\mymodule\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class ControllerAlterSubscriber.
 */
class ControllerAlterSubscriber implements EventSubscriberInterface {

  /**
   * Alters the controller output.
   */
  public function onView(GetResponseForControllerResultEvent $event) {
    $request = $event->getRequest();
    $route = $request->attributes->get('_route');

    if ($route == 'entity.webform_submission.collection') {
      $build = $event->getControllerResult();
      if (is_array($build)) {
        // alter controller build array
        $build['added_content'] = ['#markup' => '<p>Added by ControllerAlterSubscriber.</p>'];

        $event->setControllerResult($build);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    // priority > 0 so that it runs before the controller output
    // is rendered by \Drupal\Core\EventSubscriber\MainContentViewSubscriber
    $events[KernelEvents::VIEW][] = ['onView', 50];
    return $events;
  }

}

mymodule.services.yml:

services:
  mymodule.controller_alter:
    class: Drupal\mymodule\EventSubscriber\ControllerAlterSubscriber
    arguments: []
    tags:
      - { name: event_subscriber }

@user5429087 2019-05-15 18:19:56

This seems like a good workable solution.

Related Questions

Sponsored Content

1 Answered Questions

2 Answered Questions

[SOLVED] Best theming practice for leanest possible code?

  • 2018-07-19 14:26:14
  • boredpanda
  • 64 View
  • -1 Score
  • 2 Answer
  • Tags:   8 theming

2 Answered Questions

[SOLVED] Drupal 8 - Use CKEditor on textarea in custom module's controller response

  • 2018-06-05 19:08:56
  • sceithamer
  • 434 View
  • 0 Score
  • 2 Answer
  • Tags:   8 wysiwyg

3 Answered Questions

Render a template file from module's controller method

  • 2016-08-09 08:07:05
  • jepster
  • 2170 View
  • -1 Score
  • 3 Answer
  • Tags:   theming 8

1 Answered Questions

Best practice for node comparison for more than 2000 nodes

  • 2017-04-25 07:40:10
  • Sugandh Khanna
  • 62 View
  • 0 Score
  • 1 Answer
  • Tags:   8 nodes database

2 Answered Questions

[SOLVED] Best practice: $this->t() versus t()

  • 2017-01-05 13:47:02
  • Wim Mostrey
  • 4106 View
  • 8 Score
  • 2 Answer
  • Tags:   8 i18n-l10n

2 Answered Questions

[SOLVED] Best practice: Twig template for custom module's block

  • 2017-01-05 15:13:44
  • Wim Mostrey
  • 854 View
  • 4 Score
  • 2 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] Best practice for caching CSS libraries?

  • 2016-12-30 00:11:54
  • user11448
  • 109 View
  • 0 Score
  • 2 Answer
  • Tags:   theming 8 caching

1 Answered Questions

[SOLVED] Drupal 8: Best practice to get nid in form_submit

  • 2016-05-26 12:02:54
  • Baud
  • 610 View
  • 0 Score
  • 1 Answer
  • Tags:   8

2 Answered Questions

[SOLVED] Drupal 8 JavaScript and Best Practice

Sponsored Content