You are here

class ContentTranslationAccessCheck in Allowed Languages 2.x

Same name and namespace in other branches
  1. 8 src/Access/ContentTranslationAccessCheck.php \Drupal\allowed_languages\Access\ContentTranslationAccessCheck

Allowed languages content translation access check.

Hierarchy

Expanded class hierarchy of ContentTranslationAccessCheck

1 string reference to 'ContentTranslationAccessCheck'
allowed_languages.services.yml in ./allowed_languages.services.yml
allowed_languages.services.yml
1 service uses ContentTranslationAccessCheck
allowed_languages.content_translation_access_check in ./allowed_languages.services.yml
Drupal\allowed_languages\Access\ContentTranslationAccessCheck

File

src/Access/ContentTranslationAccessCheck.php, line 16

Namespace

Drupal\allowed_languages\Access
View source
class ContentTranslationAccessCheck extends AccessCheckBase {

  /**
   * Drupal language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  private $languageManager;

  /**
   * AccessCheck constructor.
   *
   * @param \Drupal\allowed_languages\AllowedLanguagesManagerInterface $allowed_languages_manager
   *   The allowed language manager service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   Drupal language manager.
   */
  public function __construct(AllowedLanguagesManagerInterface $allowed_languages_manager, LanguageManagerInterface $language_manager) {
    parent::__construct($allowed_languages_manager);
    $this->languageManager = $language_manager;
  }

  /**
   * Check language access when managing content translations.
   *
   * This access check is based on the access check provided by the content
   * translation module and uses the same parameters in the access callback.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The parametrized route.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   * @param string $source
   *   (optional) For a create operation, the language code of the source.
   * @param string $target
   *   (optional) For a create operation, the language code of the translation.
   * @param string $language
   *   (optional) For an update or delete operation, the language code of the
   *   translation being updated or deleted.
   * @param string $entity_type_id
   *   (optional) The entity type ID.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account, $source = NULL, $target = NULL, $language = NULL, $entity_type_id = NULL) {

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

    // If the entity could not be found on the parameters let other modules
    // take care of the access check.
    if (!$entity) {
      return AccessResult::neutral();
    }
    $language = $this
      ->getTargetLanguage($target);
    $user = $this->allowedLanguagesManager
      ->accountFromProxy($account);
    if ($this->allowedLanguagesManager
      ->hasPermissionForLanguage($language, $user)) {
      return AccessResult::allowed()
        ->cachePerUser()
        ->addCacheableDependency($user);
    }
    return AccessResult::forbidden()
      ->cachePerUser()
      ->addCacheableDependency($user);
  }

  /**
   * Get the target language object.
   *
   * @param string $target
   *   The target language id.
   *
   * @return \Drupal\Core\Language\LanguageInterface
   *   The target language object or the current content language.
   */
  private function getTargetLanguage($target) {
    return $this->languageManager
      ->getLanguage($target) ?: $this->languageManager
      ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessCheckBase::$allowedLanguagesManager protected property The allowed language manager service.
ContentTranslationAccessCheck::$languageManager private property Drupal language manager.
ContentTranslationAccessCheck::access public function Check language access when managing content translations.
ContentTranslationAccessCheck::getTargetLanguage private function Get the target language object.
ContentTranslationAccessCheck::__construct public function AccessCheck constructor. Overrides AccessCheckBase::__construct