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