You are here

class OperationSubscriber in GraphQL 8.4

Sets the language before/after a GraphQL operation.

Hierarchy

Expanded class hierarchy of OperationSubscriber

1 string reference to 'OperationSubscriber'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses OperationSubscriber
graphql.operation_subscriber in ./graphql.services.yml
Drupal\graphql\EventSubscriber\OperationSubscriber

File

src/EventSubscriber/OperationSubscriber.php, line 17

Namespace

Drupal\graphql\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
CurrentLanguageResetTrait::$currentUser protected property The current user service.
CurrentLanguageResetTrait::$languageManager protected property The language manager.
CurrentLanguageResetTrait::$languageNegotiator protected property The language negotiator.
CurrentLanguageResetTrait::$translator protected property The translator.
CurrentLanguageResetTrait::resetLanguageContext protected function Resets the global language context across different services.
OperationSubscriber::$moduleHandler protected property The module handler service.
OperationSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OperationSubscriber::onAfterOperation public function Handle operation end events.
OperationSubscriber::onBeforeOperation public function Handle operation start events.
OperationSubscriber::__construct public function Constructs a OperationSubscriber object.