Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / content_translation / src / Access / ContentTranslationOverviewAccess.php
1 <?php
2
3 namespace Drupal\content_translation\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
11 /**
12  * Access check for entity translation overview.
13  */
14 class ContentTranslationOverviewAccess implements AccessInterface {
15
16   /**
17    * The entity type manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityManager;
22
23   /**
24    * Constructs a ContentTranslationOverviewAccess object.
25    *
26    * @param \Drupal\Core\Entity\EntityManagerInterface $manager
27    *   The entity type manager.
28    */
29   public function __construct(EntityManagerInterface $manager) {
30     $this->entityManager = $manager;
31   }
32
33   /**
34    * Checks access to the translation overview for the entity and bundle.
35    *
36    * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
37    *   The parametrized route.
38    * @param \Drupal\Core\Session\AccountInterface $account
39    *   The currently logged in account.
40    * @param string $entity_type_id
41    *   The entity type ID.
42    *
43    * @return \Drupal\Core\Access\AccessResultInterface
44    *   The access result.
45    */
46   public function access(RouteMatchInterface $route_match, AccountInterface $account, $entity_type_id) {
47     /* @var \Drupal\Core\Entity\ContentEntityInterface $entity */
48     $entity = $route_match->getParameter($entity_type_id);
49     if ($entity && $entity->isTranslatable()) {
50       // Get entity base info.
51       $bundle = $entity->bundle();
52
53       // Get entity access callback.
54       $definition = $this->entityManager->getDefinition($entity_type_id);
55       $translation = $definition->get('translation');
56       $access_callback = $translation['content_translation']['access_callback'];
57       $access = call_user_func($access_callback, $entity);
58       if ($access->isAllowed()) {
59         return $access;
60       }
61
62       // Check "translate any entity" permission.
63       if ($account->hasPermission('translate any entity')) {
64         return AccessResult::allowed()->cachePerPermissions()->inheritCacheability($access);
65       }
66
67       // Check per entity permission.
68       $permission = "translate {$entity_type_id}";
69       if ($definition->getPermissionGranularity() == 'bundle') {
70         $permission = "translate {$bundle} {$entity_type_id}";
71       }
72       return AccessResult::allowedIfHasPermission($account, $permission)->inheritCacheability($access);
73     }
74
75     // No opinion.
76     return AccessResult::neutral();
77   }
78
79 }