View source
<?php
namespace Drupal\form_mode_manager\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\form_mode_manager\ComplexEntityFormModes;
use Drupal\form_mode_manager\EntityFormModeManagerInterface;
use Drupal\form_mode_manager\MediaEntityFormModes;
use Drupal\form_mode_manager\SimpleEntityFormModes;
use Drupal\form_mode_manager\EntityRoutingMapManager;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Drupal\form_mode_manager\TaxonomyEntityFormModes;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormModeManagerEntityController implements EntityFormModeManagerInterface, ContainerInjectionInterface {
protected $dateFormatter;
protected $renderer;
protected $account;
protected $formModeManager;
protected $entityFormBuilder;
protected $entityRoutingMap;
protected $formBuilder;
protected $entityTypeManager;
public function __construct(RendererInterface $renderer, AccountInterface $account, FormModeManagerInterface $form_mode_manager, EntityFormBuilderInterface $entity_form_builder, EntityRoutingMapManager $plugin_routes_manager, FormBuilderInterface $form_builder, EntityTypeManagerInterface $entity_type_manager) {
$this->renderer = $renderer;
$this->account = $account;
$this->formModeManager = $form_mode_manager;
$this->entityFormBuilder = $entity_form_builder;
$this->entityRoutingMap = $plugin_routes_manager;
$this->formBuilder = $form_builder;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('current_user'), $container
->get('form_mode.manager'), $container
->get('entity.form_builder'), $container
->get('plugin.manager.entity_routing_map'), $container
->get('form_builder'), $container
->get('entity_type.manager'));
}
public function addPage(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function addPageTitle(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function checkAccess(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function editPageTitle(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function entityAdd(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function entityEdit(RouteMatchInterface $route_match) {
return $this
->getEntityControllerResponse(__FUNCTION__, $route_match);
}
public function getEntityControllerResponse($method, RouteMatchInterface $route_match) {
$entity_type_id = $route_match
->getRouteObject()
->getOption('_form_mode_manager_entity_type_id');
$entity_storage = $this->entityTypeManager
->getStorage($entity_type_id);
$entity_type_id = $this
->bundledEntity($entity_storage) ? $entity_type_id : 'unbundled_entity';
$controller_object = $this
->getEntityControllerObject($entity_type_id);
if (!method_exists($controller_object, $method)) {
throw new \Exception('Specified ' . $method . ' method not found.');
}
return $controller_object
->{$method}($route_match);
}
public function getEntityControllerObject($entity_type_id) {
switch ($entity_type_id) {
case 'unbundled_entity':
$object = new SimpleEntityFormModes($this->renderer, $this->account, $this->formModeManager, $this->entityFormBuilder, $this->entityRoutingMap, $this->formBuilder, $this->entityTypeManager);
break;
case 'taxonomy_term':
$object = new TaxonomyEntityFormModes($this->renderer, $this->account, $this->formModeManager, $this->entityFormBuilder, $this->entityRoutingMap, $this->formBuilder, $this->entityTypeManager);
break;
case 'media':
$object = new MediaEntityFormModes($this->renderer, $this->account, $this->formModeManager, $this->entityFormBuilder, $this->entityRoutingMap, $this->formBuilder, $this->entityTypeManager);
break;
default:
$object = new ComplexEntityFormModes($this->renderer, $this->account, $this->formModeManager, $this->entityFormBuilder, $this->entityRoutingMap, $this->formBuilder, $this->entityTypeManager);
break;
}
return $object;
}
public function bundledEntity(EntityStorageInterface $entity_storage) {
return $entity_storage
->getEntityType()
->getKey('bundle');
}
}