PopupMessageSubscriber.php in Popup message 8
File
src/EventSubscriber/PopupMessageSubscriber.php
View source
<?php
namespace Drupal\popup_message\EventSubscriber;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Path\PathMatcher;
use Drupal\Core\Render\AttachmentsInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PopupMessageSubscriber implements EventSubscriberInterface {
protected $config;
protected $requestStack;
protected $pathMatcher;
protected $account;
protected $moduleHandler;
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;
}
public function showPopupMessage(FilterResponseEvent $event) {
$response = $event
->getResponse();
if (!$response instanceof AttachmentsInterface) {
return;
}
$status = $this->config
->get('enable');
$current_url = $this->requestStack
->getRequestUri();
$decline_system_path = '/editor/*';
$system_path = $this->pathMatcher
->matchPath($current_url, $decline_system_path);
if ($status && !$system_path) {
$permission = $this->account
->hasPermission('display popup message');
$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,
];
$this->moduleHandler
->alter('popup_message_perameters', $popup_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);
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'showPopupMessage',
20,
];
return $events;
}
}