class OperationSubscriber in GraphQL 8.4
Sets the language before/after a GraphQL operation.
Hierarchy
- class \Drupal\graphql\EventSubscriber\OperationSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses CurrentLanguageResetTrait
Expanded class hierarchy of OperationSubscriber
1 string reference to 'OperationSubscriber'
1 service uses OperationSubscriber
File
- src/
EventSubscriber/ OperationSubscriber.php, line 17
Namespace
Drupal\graphql\EventSubscriberView source
class OperationSubscriber implements EventSubscriberInterface {
use CurrentLanguageResetTrait;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a OperationSubscriber object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator
* @param \Drupal\Core\Session\AccountInterface $currentUser
* @param \Drupal\language\LanguageNegotiatorInterface $languageNegotiator
*/
public function __construct(ModuleHandlerInterface $moduleHandler, LanguageManagerInterface $languageManager, TranslatorInterface $translator, AccountInterface $currentUser, LanguageNegotiatorInterface $languageNegotiator = NULL) {
$this->moduleHandler = $moduleHandler;
$this->languageManager = $languageManager;
$this->translator = $translator;
$this->currentUser = $currentUser;
$this->languageNegotiator = $languageNegotiator;
}
/**
* Handle operation start events.
*
* @param \Drupal\graphql\Event\OperationEvent $event
* The kernel event object.
*/
public function onBeforeOperation(OperationEvent $event) : void {
if ($this->moduleHandler
->moduleExists('language') && !empty($this->languageNegotiator)) {
OperationLanguageNegotiation::setContext($event
->getContext());
}
$this
->resetLanguageContext();
}
/**
* Handle operation end events.
*
* @param \Drupal\graphql\Event\OperationEvent $event
* The kernel event object.
*/
public function onAfterOperation(OperationEvent $event) : void {
if ($this->moduleHandler
->moduleExists('language') && !empty($this->languageNegotiator)) {
OperationLanguageNegotiation::setContext($event
->getContext());
}
$this
->resetLanguageContext();
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
OperationEvent::GRAPHQL_OPERATION_BEFORE => 'onBeforeOperation',
OperationEvent::GRAPHQL_OPERATION_AFTER => 'onAfterOperation',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CurrentLanguageResetTrait:: |
protected | property | The current user service. | |
CurrentLanguageResetTrait:: |
protected | property | The language manager. | |
CurrentLanguageResetTrait:: |
protected | property | The language negotiator. | |
CurrentLanguageResetTrait:: |
protected | property | The translator. | |
CurrentLanguageResetTrait:: |
protected | function | Resets the global language context across different services. | |
OperationSubscriber:: |
protected | property | The module handler service. | |
OperationSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
OperationSubscriber:: |
public | function | Handle operation end events. | |
OperationSubscriber:: |
public | function | Handle operation start events. | |
OperationSubscriber:: |
public | function | Constructs a OperationSubscriber object. |