You are here

function popup_maker_page_top in Popup Maker - All popup types 8

Page top function for the popup maker.

File

./popup_maker.module, line 40
This module holds functions for Popup Maker.

Code

function popup_maker_page_top(array &$page_top) {
  $node = \Drupal::routeMatch()
    ->getParameter('node');
  if (!$node instanceof NodeInterface) {
    return;
  }
  $config = \Drupal::config('popup_maker.settings');
  $popups = $config
    ->get('popups');
  $popupSettings = $config
    ->get('popupSettings');
  $template = '<script>
	window.SGPMPopupLoader=window.SGPMPopupLoader||{ids:[],popups:{},call:function(w,d,s,l,id){
	  w[\'sgp\']=w[\'sgp\']||function(){(w[\'sgp\'].q=w[\'sgp\'].q||[]).push(arguments[0]);};
	  var sg1=d.createElement(s),sg0=d.getElementsByTagName(s)[0];
	  if(SGPMPopupLoader && SGPMPopupLoader.ids && SGPMPopupLoader.ids.length > 0){SGPMPopupLoader.ids.push(id); return;}
      SGPMPopupLoader.ids.push(id);
	  sg1.onload = function(){SGPMPopup.openSGPMPopup();}; sg1.async=true; sg1.src=l;
	  sg0.parentNode.insertBefore(sg1,sg0);
	  return {};
	}};';
  if (empty($popups)) {
    return;
  }
  foreach ($popups as $id => $popup) {
    if (!$popupSettings[$id]['enabled']) {
      continue;
    }
    $allowed = TRUE;
    if (!empty($popupSettings[$id]['displayOptions'])) {
      $allowed = FALSE;
      foreach ($popupSettings[$id]['displayOptions'] as $displayOption) {
        if (strpos($displayOption['param'], '_all')) {
          $endIndex = strpos($displayOption['param'], '_all');
          $contentType = substr($displayOption['param'], 0, $endIndex);
          $currentContentType = $node
            ->getType();
          if ($displayOption['operator'] === '==') {
            if ($currentContentType === $contentType) {
              $allowed = TRUE;
            }
          }
          elseif ($currentContentType !== $contentType) {
            $allowed = TRUE;
          }
        }
        elseif (strpos($displayOption['param'], '_selected')) {
          $values = $displayOption['value'];
          if ($displayOption['operator'] === '==') {
            if (in_array($node
              ->id(), $values)) {
              $allowed = TRUE;
            }
          }
          elseif (!in_array($node
            ->id(), $values)) {
            $allowed = TRUE;
          }
        }
      }
    }
    if ($allowed) {
      $template .= "SGPMPopupLoader.call(window,document,'script','https://popupmaker.com/assets/lib/SGPMPopup.min.js','" . $popup['hashId'] . "');";
    }
  }
  $template .= '</script>';
  $page_top['popup_maker'] = [
    '#type' => 'inline_template',
    '#template' => $template,
  ];
}