You are here

function allowed_languages_entity_access in Allowed Languages 2.x

Implements hook_entity_access().

File

./allowed_languages.module, line 159
Contains allowed_languages.module.

Code

function allowed_languages_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  if (!\Drupal::service('allowed_languages.allowed_languages_manager')
    ->isEntityLanguageControlled($entity)) {
    return AccessResult::neutral();
  }

  // Only perform the access check when performing update/delete operations.
  if (!in_array($operation, [
    'update',
    'delete',
  ])) {
    return AccessResult::neutral();
  }
  $language = $entity
    ->language();
  $user = \Drupal::service('allowed_languages.allowed_languages_manager')
    ->accountFromProxy($account);
  if (\Drupal::service('allowed_languages.allowed_languages_manager')
    ->hasPermissionForLanguage($language, $user)) {
    return AccessResult::allowed()
      ->cachePerUser()
      ->addCacheableDependency($user);
  }

  // Access check failed so do not allow the user to translate the specified
  // language.
  return AccessResult::forbidden()
    ->cachePerUser()
    ->addCacheableDependency($user);
}