You are here

class PopupMessageSubscriber in Popup message 8

Class PopupMessageSubscriber.

@package Drupal\popup_message\EventSubscriber

Hierarchy

  • class \Drupal\popup_message\EventSubscriber\PopupMessageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PopupMessageSubscriber

1 string reference to 'PopupMessageSubscriber'
popup_message.services.yml in ./popup_message.services.yml
popup_message.services.yml
1 service uses PopupMessageSubscriber
popup_message_event_subscriber in ./popup_message.services.yml
Drupal\popup_message\EventSubscriber\PopupMessageSubscriber

File

src/EventSubscriber/PopupMessageSubscriber.php, line 21

Namespace

Drupal\popup_message\EventSubscriber
View source
class PopupMessageSubscriber implements EventSubscriberInterface {

  /**
   * The PopupMessage config.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Current Request.
   *
   * @var null|\Symfony\Component\HttpFoundation\Request
   */
  protected $requestStack;

  /**
   * Path matcher services.
   *
   * @var \Drupal\Core\Path\PathMatcher
   */
  protected $pathMatcher;

  /**
   * User account service.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * PopupMessageSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   Popup_message config.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   Current Request.
   * @param \Drupal\Core\Path\PathMatcher $pathMatcher
   *   Path matcher services.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   User account service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   Module handler service.
   */
  public function __construct(ConfigFactoryInterface $config, RequestStack $requestStack, PathMatcher $pathMatcher, AccountInterface $account, ModuleHandlerInterface $moduleHandler) {
    $this->config = $config
      ->get('popup_message.settings');
    $this->requestStack = $requestStack
      ->getCurrentRequest();
    $this->pathMatcher = $pathMatcher;
    $this->account = $account;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * Init PopupMessage.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   PopupMessage event.
   */
  public function showPopupMessage(FilterResponseEvent $event) {

    // Check permissions to display message.
    $response = $event
      ->getResponse();
    if (!$response instanceof AttachmentsInterface) {
      return;
    }

    // Check module has enable popup.
    $status = $this->config
      ->get('enable');

    // Omit system path.
    $current_url = $this->requestStack
      ->getRequestUri();
    $decline_system_path = '/editor/*';
    $system_path = $this->pathMatcher
      ->matchPath($current_url, $decline_system_path);

    // Check module has enable popup, permission, exclude denied url.
    // Set session with true or false.
    // If all requirements are ok session PopupMessageStatus is set to true.
    if ($status && !$system_path) {
      $permission = $this->account
        ->hasPermission('display popup message');

      // Get status: enabled/disabled.
      // Allow other modules to modify permissions.
      $this->moduleHandler
        ->alter('popup_message_permission', $permission);
      $message_title = Xss::filter($this->config
        ->get('title'));
      $message_body_variable = $this->config
        ->get('body');
      $message_body = check_markup($message_body_variable['value'], $message_body_variable['format'] ? $message_body_variable['format'] : filter_default_format(), FALSE);
      $popup_message_parameters = [
        'title' => $message_title,
        'body' => $message_body,
        'check_cookie' => $this->config
          ->get('check_cookie') ? $this->config
          ->get('check_cookie') : 0,
        'expire' => $this->config
          ->get('expire') ? $this->config
          ->get('expire') : 0,
        'width' => $this->config
          ->get('width') ? $this->config
          ->get('width') : 300,
        'height' => $this->config
          ->get('height') ? $this->config
          ->get('height') : 300,
        'delay' => $this->config
          ->get('delay') ? $this->config
          ->get('delay') : 0,
        'close_delay' => $this->config
          ->get('close_delay') ? $this->config
          ->get('close_delay') : 0,
        'cover_opacity' => $this->config
          ->get('cover_opacity') ?? 70,
      ];

      /* @deprecated: alter containing typo will be removed in the next release. */
      $this->moduleHandler
        ->alter('popup_message_perameters', $popup_message_parameters);

      // Allow other modules to modify message parameters.
      $this->moduleHandler
        ->alter('popup_message_parameters', $popup_message_parameters);
      if ($popup_message_parameters['title'] && $popup_message_parameters['body']) {
        $attachments = $response
          ->getAttachments();
        $attachments['library'][] = 'popup_message/popup_message_style';
        $attachments['drupalSettings']['popupMessage'] = $popup_message_parameters;
        $response
          ->setAttachments($attachments);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'showPopupMessage',
      20,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PopupMessageSubscriber::$account protected property User account service.
PopupMessageSubscriber::$config protected property The PopupMessage config.
PopupMessageSubscriber::$moduleHandler protected property Module handler service.
PopupMessageSubscriber::$pathMatcher protected property Path matcher services.
PopupMessageSubscriber::$requestStack protected property Current Request.
PopupMessageSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PopupMessageSubscriber::showPopupMessage public function Init PopupMessage.
PopupMessageSubscriber::__construct public function PopupMessageSubscriber constructor.