Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity type manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Creates a new ViewModeAccessCheck. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks access to the view mode. Chris@0: * Chris@0: * @param \Symfony\Component\Routing\Route $route Chris@0: * The route to check against. Chris@0: * @param \Drupal\Core\Routing\RouteMatchInterface $route_match Chris@0: * The parametrized route. Chris@0: * @param \Drupal\Core\Session\AccountInterface $account Chris@0: * The currently logged in account. Chris@0: * @param string $view_mode_name Chris@0: * (optional) The view mode. Defaults to 'default'. Chris@0: * @param string $bundle Chris@0: * (optional) The bundle. Different entity types can have different names Chris@0: * for their bundle key, so if not specified on the route via a {bundle} Chris@0: * parameter, the access checker determines the appropriate key name, and Chris@0: * gets the value from the corresponding request attribute. For example, Chris@0: * for nodes, the bundle key is "node_type", so the value would be Chris@0: * available via the {node_type} parameter rather than a {bundle} Chris@0: * parameter. Chris@0: * Chris@0: * @return \Drupal\Core\Access\AccessResultInterface Chris@0: * The access result. Chris@0: */ Chris@0: public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $view_mode_name = 'default', $bundle = NULL) { Chris@0: $access = AccessResult::neutral(); Chris@0: if ($entity_type_id = $route->getDefault('entity_type_id')) { Chris@0: if (empty($bundle)) { Chris@18: $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); Chris@0: $bundle = $route_match->getRawParameter($entity_type->getBundleEntityType()); Chris@0: } Chris@0: Chris@0: $visibility = FALSE; Chris@0: if ($view_mode_name == 'default') { Chris@0: $visibility = TRUE; Chris@0: } Chris@18: elseif ($entity_display = $this->entityTypeManager->getStorage('entity_view_display')->load($entity_type_id . '.' . $bundle . '.' . $view_mode_name)) { Chris@0: $visibility = $entity_display->status(); Chris@0: } Chris@0: Chris@0: if ($view_mode_name != 'default' && $entity_display) { Chris@0: $access->addCacheableDependency($entity_display); Chris@0: } Chris@0: Chris@0: if ($visibility) { Chris@0: $permission = $route->getRequirement('_field_ui_view_mode_access'); Chris@0: $access = $access->orIf(AccessResult::allowedIfHasPermission($account, $permission)); Chris@0: } Chris@0: } Chris@0: return $access; Chris@0: } Chris@0: Chris@0: }