Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/custom_forms / src / EventSubscriber / PageLoadSubscriber.php
Size: Mime:
<?php

namespace Drupal\custom_forms\EventSubscriber;

use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class PageLoadSubscriber
 *
 * The pageload subscriber so that we can display a clear warning if the
 * encryption key is missing.
 *
 * @package Drupal\custom_forms\EventSubscriber
 */
class PageLoadSubscriber implements EventSubscriberInterface {

  use StringTranslationTrait;

  /**
   * @var \Drupal\Core\Messenger\Messenger $messenger
   */
  protected $messenger;

  /**
   * PageLoadSubscriber constructor.
   *
   * @param \Drupal\Core\Messenger\Messenger $messenger
   */
  public function __construct(Messenger $messenger) {
    $this->messenger = $messenger;
  }

  /**
   * Returns an array of event names this subscriber wants to listen to.
   *
   * The array keys are event names and the value can be:
   *
   *  * The method name to call (priority defaults to 0)
   *  * An array composed of the method name to call and the priority
   *  * An array of arrays composed of the method names to call and respective
   *    priorities, or 0 if unset
   *
   * For instance:
   *
   *  * ['eventName' => 'methodName']
   *  * ['eventName' => ['methodName', $priority]]
   *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
   *
   * @return array The event names to listen to
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('onPageLoad', 20);
    return $events;
  }

  public function onPageLoad(GetResponseEvent $event) {
    if (!Settings::get('custom_forms.aes_key', FALSE)) {
      $this->messenger->addError($this->t('<b>Custom Forms Security:</b> AES key is missing from site settings! <br>Please add it to the site\'s settings.php as <code>$settings[\'custom_forms_security.aes_key\']</code>'));
    }
  }
}