3 namespace Drupal\content_translation\Access;
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;
12 * Access check for entity translation overview.
14 class ContentTranslationOverviewAccess implements AccessInterface {
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityManager;
24 * Constructs a ContentTranslationOverviewAccess object.
26 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
27 * The entity type manager.
29 public function __construct(EntityManagerInterface $manager) {
30 $this->entityManager = $manager;
34 * Checks access to the translation overview for the entity and bundle.
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
43 * @return \Drupal\Core\Access\AccessResultInterface
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();
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()) {
62 // Check "translate any entity" permission.
63 if ($account->hasPermission('translate any entity')) {
64 return AccessResult::allowed()->cachePerPermissions()->inheritCacheability($access);
67 // Check per entity permission.
68 $permission = "translate {$entity_type_id}";
69 if ($definition->getPermissionGranularity() == 'bundle') {
70 $permission = "translate {$bundle} {$entity_type_id}";
72 return AccessResult::allowedIfHasPermission($account, $permission)->inheritCacheability($access);
76 return AccessResult::neutral();