You are here

class UrlLanguageService in Allowed Languages 8

Allowed languages url language service.

This service is a very dirty solution to a problem that does not seem to be possible to resolve easily in another way.

The problem that this service solves is to be able to know the language of a URL when checking it's access, but access checks do not know the language a route will be rendered in since this information is not available when doing an access check.

This service bridges this gap for the allowed languages module by allowing a language to be stored before invoking access checks for URL objects, the access checkers can then query the url language service to see the language the URL will be rendered in.

Be sure to re-set the stored language when URL access checks are done.

@todo This service should probably be replaced by a better solution that should preferably be added to core in some way.

Maybe it would be possible to have a URL-object provide its options to the access managers checkNamedRoute callback or something similar.

Hierarchy

Expanded class hierarchy of UrlLanguageService

1 file declares its use of UrlLanguageService
EntityAccessCheck.php in src/Access/EntityAccessCheck.php
1 string reference to 'UrlLanguageService'
allowed_languages.services.yml in ./allowed_languages.services.yml
allowed_languages.services.yml
1 service uses UrlLanguageService
allowed_languages.url_language_service in ./allowed_languages.services.yml
Drupal\allowed_languages\UrlLanguageService

File

src/UrlLanguageService.php, line 31

Namespace

Drupal\allowed_languages
View source
class UrlLanguageService {

  /**
   * The temporarily stored language.
   *
   * @var \Drupal\Core\Language\LanguageInterface|null
   */
  private $storedLanguage = NULL;

  /**
   * Set the stored language.
   *
   * @param \Drupal\Core\Language\LanguageInterface $language
   *   The language to set.
   */
  public function setUrlLanguage(LanguageInterface $language) {
    $this->storedLanguage = $language;
  }

  /**
   * Get the stored language.
   *
   * @return \Drupal\Core\Language\LanguageInterface|null
   *   Returns the set language.
   */
  public function getUrlLanguage() {
    return $this->storedLanguage;
  }

  /**
   * Reset the stored language.
   */
  public function resetUrlLanguage() {
    $this->storedLanguage = NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UrlLanguageService::$storedLanguage private property The temporarily stored language.
UrlLanguageService::getUrlLanguage public function Get the stored language.
UrlLanguageService::resetUrlLanguage public function Reset the stored language.
UrlLanguageService::setUrlLanguage public function Set the stored language.