Is there a "best practice" for overriding a contributed module's controller in Drupal 8? 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:

&lt;?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 &amp;$entity_types) {
  /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['webform_submission']-&gt;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.
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-&gt;getRequest();
    $route = $request-&gt;attributes-&gt;get('_route');

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

        $event-&gt;setControllerResult($build);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    // priority &gt; 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 }