annotate core/modules/field_ui/src/Access/ViewModeAccessCheck.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field_ui\Access;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@18 6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
Chris@18 7 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 8 use Drupal\Core\Routing\Access\AccessInterface;
Chris@0 9 use Drupal\Core\Routing\RouteMatchInterface;
Chris@0 10 use Drupal\Core\Session\AccountInterface;
Chris@0 11 use Symfony\Component\Routing\Route;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Defines an access check for entity view mode routes.
Chris@0 15 *
Chris@0 16 * @see \Drupal\Core\Entity\Entity\EntityViewMode
Chris@0 17 */
Chris@0 18 class ViewModeAccessCheck implements AccessInterface {
Chris@18 19 use DeprecatedServicePropertyTrait;
Chris@0 20
Chris@0 21 /**
Chris@18 22 * {@inheritdoc}
Chris@18 23 */
Chris@18 24 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
Chris@18 25
Chris@18 26 /**
Chris@18 27 * The entity type manager.
Chris@0 28 *
Chris@18 29 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 30 */
Chris@18 31 protected $entityTypeManager;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Creates a new ViewModeAccessCheck.
Chris@0 35 *
Chris@18 36 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 37 * The entity type manager.
Chris@0 38 */
Chris@18 39 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
Chris@18 40 $this->entityTypeManager = $entity_type_manager;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Checks access to the view mode.
Chris@0 45 *
Chris@0 46 * @param \Symfony\Component\Routing\Route $route
Chris@0 47 * The route to check against.
Chris@0 48 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 49 * The parametrized route.
Chris@0 50 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 51 * The currently logged in account.
Chris@0 52 * @param string $view_mode_name
Chris@0 53 * (optional) The view mode. Defaults to 'default'.
Chris@0 54 * @param string $bundle
Chris@0 55 * (optional) The bundle. Different entity types can have different names
Chris@0 56 * for their bundle key, so if not specified on the route via a {bundle}
Chris@0 57 * parameter, the access checker determines the appropriate key name, and
Chris@0 58 * gets the value from the corresponding request attribute. For example,
Chris@0 59 * for nodes, the bundle key is "node_type", so the value would be
Chris@0 60 * available via the {node_type} parameter rather than a {bundle}
Chris@0 61 * parameter.
Chris@0 62 *
Chris@0 63 * @return \Drupal\Core\Access\AccessResultInterface
Chris@0 64 * The access result.
Chris@0 65 */
Chris@0 66 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $view_mode_name = 'default', $bundle = NULL) {
Chris@0 67 $access = AccessResult::neutral();
Chris@0 68 if ($entity_type_id = $route->getDefault('entity_type_id')) {
Chris@0 69 if (empty($bundle)) {
Chris@18 70 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 71 $bundle = $route_match->getRawParameter($entity_type->getBundleEntityType());
Chris@0 72 }
Chris@0 73
Chris@0 74 $visibility = FALSE;
Chris@0 75 if ($view_mode_name == 'default') {
Chris@0 76 $visibility = TRUE;
Chris@0 77 }
Chris@18 78 elseif ($entity_display = $this->entityTypeManager->getStorage('entity_view_display')->load($entity_type_id . '.' . $bundle . '.' . $view_mode_name)) {
Chris@0 79 $visibility = $entity_display->status();
Chris@0 80 }
Chris@0 81
Chris@0 82 if ($view_mode_name != 'default' && $entity_display) {
Chris@0 83 $access->addCacheableDependency($entity_display);
Chris@0 84 }
Chris@0 85
Chris@0 86 if ($visibility) {
Chris@0 87 $permission = $route->getRequirement('_field_ui_view_mode_access');
Chris@0 88 $access = $access->orIf(AccessResult::allowedIfHasPermission($account, $permission));
Chris@0 89 }
Chris@0 90 }
Chris@0 91 return $access;
Chris@0 92 }
Chris@0 93
Chris@0 94 }