Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active January 3, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EclipseGc/8445585 to your computer and use it in GitHub Desktop.
Save EclipseGc/8445585 to your computer and use it in GitHub Desktop.
services:
block.controllers:
class: Drupal\block\BlockEntityControllers
arguments: ['@service_container', '@module_handler']
tags:
- { name: event_subscriber }
<?php
/**
* @file
* Contains \Drupal\block\BlockEntityControllers
*/
namespace Drupal\block;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\Event\EntityControllerEvent;
use Drupal\Core\Config\Entity\ConfigStorageController;
class BlockEntityControllers implements EventSubscriberInterface {
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* @param ContainerInterface $container
* @param ModuleHandlerInterface $module_handler
*/
function __construct(ContainerInterface $container, ModuleHandlerInterface $module_handler) {
$this->container = $container;
$this->moduleHandler = $module_handler;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$controllers = array(
'storage' => 'getStorageController',
'access' => 'getAccessController',
'view_builder' => 'getViewBuilderController',
'list' => 'getListController',
'form' => 'getFormController',
);
$events = array();
foreach ($controllers as $controller => $method) {
$events["block--$controller"][] = array($method);
}
return $events;
}
/**
* @param EntityControllerEvent $event
*/
public function getStorageController(EntityControllerEvent $event) {
$controller = ConfigStorageController::createInstance($this->container, $event->getEntityType());
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getAccessController(EntityControllerEvent $event) {
$controller = BlockAccessController::createInstance($this->container, $event->getEntityType());
$controller->setModuleHandler($this->moduleHandler);
$event->setController($controller);
}
public function getViewBuilderController(EntityControllerEvent $event) {
$controller = new BlockViewBuilder();
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getListController(EntityControllerEvent $event) {
$controller = BlockListController::createInstance($this->container, $event->getEntityType());
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getFormController(EntityControllerEvent $event) {
$operation = $event->getControllerOperation();
switch ($operation) {
case 'default':
$controller = \Drupal\block\BlockFormController::create($this->container);
break;
case 'delete':
$controller = \Drupal\block\Form\BlockDeleteForm::create($this->container);
break;
}
if ($controller) {
$controller->setTranslationManager($this->translationManager)
->setModuleHandler($this->moduleHandler)
->setOperation($operation);
$event->setController($controller);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment