You are here

class AllowedLanguagesManager in Allowed Languages 2.x

The allowed language manager controls access to content by language.

@package Drupal\allowed_languages

Hierarchy

Expanded class hierarchy of AllowedLanguagesManager

1 string reference to 'AllowedLanguagesManager'
allowed_languages.services.yml in ./allowed_languages.services.yml
allowed_languages.services.yml
1 service uses AllowedLanguagesManager
allowed_languages.allowed_languages_manager in ./allowed_languages.services.yml
Drupal\allowed_languages\AllowedLanguagesManager

File

src/AllowedLanguagesManager.php, line 17

Namespace

Drupal\allowed_languages
View source
class AllowedLanguagesManager implements AllowedLanguagesManagerInterface {

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * AllowedLanguagesManager constructor.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function accountFromProxy(AccountInterface $account = NULL) {
    if ($account === NULL) {
      $account = $this->currentUser;
    }
    if ($account instanceof AccountProxyInterface) {
      $account = $this->entityTypeManager
        ->getStorage('user')
        ->load($account
        ->id());
    }
    return $account;
  }

  /**
   * {@inheritdoc}
   */
  public function assignedLanguages(AccountInterface $account = NULL) {
    $account = $this
      ->accountFromProxy($this->currentUser);
    $language_values = [];

    // Make sure the field exists before attempting to get languages.
    if (!$account
      ->hasField('allowed_languages')) {
      return $language_values;
    }

    // Get the id of each referenced language.
    foreach ($account
      ->get('allowed_languages')
      ->getValue() as $item) {
      $language_values[] = $item['target_id'];
    }
    return $language_values;
  }

  /**
   * {@inheritdoc}
   */
  public function hasPermissionForLanguage(LanguageInterface $language, AccountInterface $account = NULL) {
    $account = $this
      ->accountFromProxy($this->currentUser);

    // Bypass the check if the user has permission to translate all languages.
    if ($account
      ->hasPermission('translate all languages')) {
      return TRUE;
    }
    $allowed_languages = $this
      ->assignedLanguages($account);
    return in_array($language
      ->getId(), $allowed_languages);
  }

  /**
   * {@inheritdoc}
   */
  public function isEntityLanguageControlled(EntityInterface $entity) {
    if ($entity instanceof ContentEntityInterface && $entity
      ->isTranslatable()) {
      return TRUE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AllowedLanguagesManager::$currentUser protected property The current user service.
AllowedLanguagesManager::$entityTypeManager protected property The entity type manager service.
AllowedLanguagesManager::accountFromProxy public function Get the actual account entity behind the proxy. Overrides AllowedLanguagesManagerInterface::accountFromProxy
AllowedLanguagesManager::assignedLanguages public function Get the allowed languages for the specified user. Overrides AllowedLanguagesManagerInterface::assignedLanguages
AllowedLanguagesManager::hasPermissionForLanguage public function Checks if the user is allowed to translate the specified language. Overrides AllowedLanguagesManagerInterface::hasPermissionForLanguage
AllowedLanguagesManager::isEntityLanguageControlled public function Decide whether the entity should be language controlled or not. Overrides AllowedLanguagesManagerInterface::isEntityLanguageControlled
AllowedLanguagesManager::__construct public function AllowedLanguagesManager constructor.