LanguageCookieConditionMethodIsValid.php in Language Cookie 8
File
src/Plugin/LanguageCookieCondition/LanguageCookieConditionMethodIsValid.php
View source
<?php
namespace Drupal\language_cookie\Plugin\LanguageCookieCondition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\language\LanguageNegotiatorInterface;
use Drupal\language_cookie\LanguageCookieConditionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\language_cookie\Plugin\LanguageNegotiation\LanguageNegotiationCookie;
class LanguageCookieConditionMethodIsValid extends LanguageCookieConditionBase {
protected $languageNegotiator;
protected $currentUser;
public function __construct(LanguageNegotiatorInterface $language_negotiator, AccountInterface $current_user, array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageNegotiator = $language_negotiator;
$this->currentUser = $current_user;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('language_negotiator'), $container
->get('current_user'), $configuration, $plugin_id, $plugin_definition);
}
public function evaluate() {
$user = $this->currentUser;
$this->languageNegotiator
->setCurrentUser($user);
$methods = $this->languageNegotiator
->getNegotiationMethods(LanguageInterface::TYPE_INTERFACE);
if (!isset($methods[LanguageNegotiationCookie::METHOD_ID])) {
return $this
->block();
}
return $this
->pass();
}
}