Skip to content

Instantly share code, notes, and snippets.

@EclipseGc
Last active January 3, 2016 00: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/8385561 to your computer and use it in GitHub Desktop.
Save EclipseGc/8385561 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\node\NodeEntityControllers
*/
namespace Drupal\node;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Entity\Event\EntityControllerEvent;
use Drupal\Core\Entity\FieldableDatabaseStorageController;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
class NodeEntityControllers implements EventSubscriberInterface {
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The node grant storage.
*
* @var \Drupal\node\NodeGrantStorageControllerInterface
*/
protected $grantStorage;
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* The date service.
*
* @var \Drupal\Core\Datetime\Date
*/
protected $dateService;
/**
* @param ContainerInterface $container
* @param ModuleHandlerInterface $module_handler
* @param TranslationInterface $translation_manager
*/
function __construct(Connection $database, FieldInfo $field_info, ModuleHandlerInterface $module_handler, TranslationInterface $translation_manager, EntityManagerInterface $entity_manager, NodeGrantDatabaseStorageInterface $grant_storage, UrlGeneratorInterface $url_generator, Date $date_service) {
$this->database = $database;
$this->fieldInfo = $field_info;
$this->moduleHandler = $module_handler;
$this->translationManager = $translation_manager;
$this->entityManager = $entity_manager;
$this->grantStorage = $grant_storage;
$this->urlGenerator = $url_generator;
$this->dateService = $date_service;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$controllers = array(
'storage' => 'getStorageController',
'view_builder' => 'getViewBuilderController',
'access' => 'getAccessController',
'form' => 'getFormController',
'list' => 'getListController',
'translation' => 'getTranslationController',
);
$events = array();
foreach ($controllers as $controller => $method) {
$events["node--$controller"][] = array($method);
}
return $events;
}
/**
* @param EntityControllerEvent $event
*/
public function getStorageController(EntityControllerEvent $event) {
$controller = new FieldableDatabaseStorageController($event->getEntityType(), $this->database, $this->fieldInfo);
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getViewBuilderController(EntityControllerEvent $event) {
$controller = new NodeViewBuilder($event->getEntityType(), $this->entityManager);
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getAccessController(EntityControllerEvent $event) {
$controller = new NodeAccessController($event->getEntityType(), $this->grantStorage);
$controller->setModuleHandler($this->moduleHandler);
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getFormController(EntityControllerEvent $event) {
$operation = $event->getControllerOperation();
switch ($operation) {
case 'default':
case 'edit':
$controller = new NodeFormController($this->entityManager);
break;
case 'delete':
$controller = new NodeDeleteForm($this->entityManager, $this->urlGenerator);
break;
}
if ($controller) {
$controller->setTranslationManager($this->translationManager)
->setModuleHandler($this->moduleHandler)
->setOperation($operation);
$event->setController($controller);
}
}
/**
* @param EntityControllerEvent $event
*/
public function getListController(EntityControllerEvent $event) {
$controller = new NodeListController($event->getEntityType(), $this->entityManager->getStorageController($event->getEntityType()->id()), $this->moduleHandler, $this->dateService);
$event->setController($controller);
}
/**
* @param EntityControllerEvent $event
*/
public function getTranslationController(EntityControllerEvent $event) {
$controller = new NodeTranslationController($event->getEntityType());
$event->setController($controller);
}
}
<?php
protected function getController($entity_type, $controller_type) {
if (!isset($this->controllers[$controller_type][$entity_type])) {
$event = new EntityControllerEvent();
$event->setEntityType($this->getDefinition($entity_type));
$event->setControllerType($controller_type);
$this->dispatcher->dispatch("$entity_type--$controller_type", $event);
$this->controllers[$controller_type][$entity_type] = $event->getController();
}
return $this->controllers[$controller_type][$entity_type];
}
<?php
/**
* @file
* Contains EntityControllerEvent.php.
*/
namespace Drupal\Core\Entity\Event;
use Symfony\Component\EventDispatcher\Event;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityControllerInterface;
class EntityControllerEvent extends Event {
/**
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entity_type;
/**
* @var string
*/
protected $controller_type;
/**
* @var object
*/
protected $controller;
/**
* @var string
*/
protected $operation;
/**
* @param EntityTypeInterface $entity_type
*/
public function setEntityType(EntityTypeInterface $entity_type) {
$this->entity_type = $entity_type;
}
/**
* @return \Drupal\Core\Entity\EntityTypeInterface
*/
public function getEntityType() {
return $this->entity_type;
}
/**
* @param string $controller_type
*/
public function setControllerType($controller_type) {
$this->controller_type = $controller_type;
}
public function getControllerType() {
return $this->controller_type;
}
/**
* @param string $operation
*/
public function setControllerOperation($operation) {
$this->operation = $operation;
}
/**
* @return string
*/
public function getControllerOperation() {
return $this->operation;
}
/**
* @param object $controller
*/
public function setController($controller) {
$this->controller = $controller;
}
/**
* @return \Drupal\Core\Entity\EntityControllerInterface
*/
public function getController() {
return $this->controller;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment