SimpleEntityFormModes.php in Form mode manager 8.2
File
src/SimpleEntityFormModes.php
View source
<?php
namespace Drupal\form_mode_manager;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\RouteMatchInterface;
class SimpleEntityFormModes extends AbstractEntityFormModesFactory {
public function addPage(RouteMatchInterface $route_match) {
}
public function checkAccess(RouteMatchInterface $route_match) {
$entity = $this
->getEntityFromRouteMatch($route_match);
$route = $route_match
->getRouteObject();
$form_mode_id = $route
->getDefault('_entity_form');
$cache_tags = $this->formModeManager
->getListCacheTags();
if (empty($entity)) {
$route_entity_type_info = $this
->getEntityTypeFromRouteMatch($route_match);
$entity_type_id = $route_entity_type_info['entity_type_id'];
}
$entity_type_id = isset($entity_type_id) ? $entity_type_id : $entity
->getEntityTypeId();
$operation = $this
->getFormModeOperationName($this->formModeManager
->getFormModeMachineName($form_mode_id));
return AccessResult::allowedIf($this->formModeManager
->isActive($entity_type_id, $entity_type_id, $operation))
->addCacheTags($cache_tags)
->addCacheableDependency($entity);
}
public function getEntityTypeFromRouteMatch(RouteMatchInterface $route_match) {
$route = $route_match
->getRouteObject();
$entity_type_id = $route
->getOption('_form_mode_manager_entity_type_id');
$form_mode = $this->formModeManager
->getFormModeMachineName($route
->getDefault('_entity_form'));
$form_mode_definition = $this->formModeManager
->getActiveDisplaysByBundle($entity_type_id, $entity_type_id);
return [
'entity_type_id' => $entity_type_id,
'form_mode' => isset($form_mode_definition[$entity_type_id][$form_mode]) ? $form_mode_definition[$entity_type_id][$form_mode] : NULL,
];
}
public function getEntity(RouteMatchInterface $route_match) {
$entity = $this
->getEntityFromRouteMatch($route_match);
if (empty($entity)) {
$route_entity_type_info = $this
->getEntityTypeFromRouteMatch($route_match);
$entity = $this->entityTypeManager
->getStorage($route_entity_type_info['entity_type_id'])
->create();
}
return $entity;
}
public function getEntityFromRouteMatch(RouteMatchInterface $route_match) {
$entity_type_id = $route_match
->getRouteObject()
->getOption('_form_mode_manager_entity_type_id');
$entity = $route_match
->getParameter($entity_type_id);
return $entity;
}
}