You are here

public function EntityFormModeBase::checkAccess in Form mode manager 8

Checks access for the Form Mode Manager routes.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Controller/EntityFormModeBase.php, line 392

Class

EntityFormModeBase
Controller for entity form mode support.

Namespace

Drupal\form_mode_manager\Controller

Code

public function checkAccess(RouteMatchInterface $route_match) {

  /* @var \Drupal\Core\Entity\EntityInterface $entity */
  $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'];
    $bundle_id = isset($route_entity_type_info['bundle']) ? $route_entity_type_info['bundle'] : $route
      ->getOption('_form_mode_manager_bundle_entity_type_id');
  }
  else {
    $entity_type_id = $route
      ->getOption('_form_mode_manager_entity_type_id');
    $bundle_id = !empty($route_match
      ->getParameter($entity_type_id)) ? $route_match
      ->getParameter($entity_type_id)
      ->bundle() : 'user';
  }
  $operation = $this
    ->getFormModeOperationName($this->formModeManager
    ->getFormModeMachineName($form_mode_id));
  $result = AccessResult::allowedIf($this->formModeManager
    ->isActive($entity_type_id, $bundle_id, $operation))
    ->addCacheTags($cache_tags);
  if ($entity) {
    $result
      ->addCacheableDependency($entity);
  }
  return $result;
}