You are here

public function PopupMessageSubscriber::showPopupMessage in Popup message 8

Init PopupMessage.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event: PopupMessage event.

File

src/EventSubscriber/PopupMessageSubscriber.php, line 86

Class

PopupMessageSubscriber
Class PopupMessageSubscriber.

Namespace

Drupal\popup_message\EventSubscriber

Code

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);
    }
  }
}