annotate core/modules/field_ui/src/Access/ViewModeAccessCheck.php @ 0:c75dbcec494b

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