You are here

public function ContentTranslationOverviewAccess::access in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_translation/src/Access/ContentTranslationOverviewAccess.php \Drupal\content_translation\Access\ContentTranslationOverviewAccess::access()

Checks access to the translation overview for the entity and bundle.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The currently logged in account.

string $entity_type_id: The entity type ID.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

core/modules/content_translation/src/Access/ContentTranslationOverviewAccess.php, line 46

Class

ContentTranslationOverviewAccess
Access check for entity translation overview.

Namespace

Drupal\content_translation\Access

Code

public function access(RouteMatchInterface $route_match, AccountInterface $account, $entity_type_id) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $route_match
    ->getParameter($entity_type_id);
  if ($entity && $entity
    ->isTranslatable()) {

    // Get entity base info.
    $bundle = $entity
      ->bundle();

    // Get entity access callback.
    $definition = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    $translation = $definition
      ->get('translation');
    $access_callback = $translation['content_translation']['access_callback'];
    $access = call_user_func($access_callback, $entity);
    if ($access
      ->isAllowed()) {
      return $access;
    }

    // Check "translate any entity" permission.
    if ($account
      ->hasPermission('translate any entity')) {
      return AccessResult::allowed()
        ->cachePerPermissions()
        ->inheritCacheability($access);
    }

    // Check per entity permission.
    $permission = "translate {$entity_type_id}";
    if ($definition
      ->getPermissionGranularity() == 'bundle') {
      $permission = "translate {$bundle} {$entity_type_id}";
    }
    return AccessResult::allowedIfHasPermission($account, $permission)
      ->inheritCacheability($access);
  }

  // No opinion.
  return AccessResult::neutral();
}