You are here

function allowed_languages_entity_operation_alter in Allowed Languages 8

Implements hook_entity_operation_alter().

File

./allowed_languages.module, line 194
Contains allowed_languages.module.

Code

function allowed_languages_entity_operation_alter(array &$operations, EntityInterface $entity) {
  $account = \Drupal::currentUser();

  // Keep all operations if it is not the instance of ContentEntityInterface,
  // the entity is not translatable or if the user has permission to translate all languages.
  if ($entity instanceof ContentEntityInterface && $entity
    ->isTranslatable() && !$account
    ->hasPermission('translate all languages')) {
    $user = User::load($account
      ->id());
    $lang = $entity
      ->language();
    $allowed_languages = allowed_languages_get_allowed_languages_for_user($user);

    // If the language is not allowed for the current user.
    if (!in_array($lang
      ->getId(), $allowed_languages)) {
      foreach ($operations as $id => $operation) {

        // Unset operations.
        if (isset($operation['url']) && !$operation['url']
          ->access($account)) {
          unset($operations[$id]);
        }
      }
    }
  }
}