You are here

function simple_popup_blocks_page_attachments in Simple Popup Blocks 8.2

Same name and namespace in other branches
  1. 8 simple_popup_blocks.module \simple_popup_blocks_page_attachments()

Implements hook_page_attachments().

File

./simple_popup_blocks.module, line 35
Contains simple_popup_blocks.module.

Code

function simple_popup_blocks_page_attachments(array &$attachments) {

  // Add libraries if it is not admin paths.
  $route = \Drupal::routeMatch()
    ->getRouteObject();
  $is_admin = \Drupal::service('router.admin_context')
    ->isAdminRoute($route);
  if (!$is_admin) {
    $popup_settings = [];
    $cacheTags = [];
    $configFactory = \Drupal::service('config.factory');
    $configs = $configFactory
      ->listAll('simple_popup_blocks.popup_');
    foreach ($configs as $config) {
      $data = $configFactory
        ->get($config)
        ->get();
      $configCacheTags = $configFactory
        ->get($config)
        ->getCacheTags();
      if (is_array($configCacheTags) && count($configCacheTags)) {
        $cacheTags = array_merge($cacheTags, $configCacheTags);
      }
      if ($data['status'] == 1) {
        if ($data['type'] == 0) {
          $identifier = preg_replace('/[_]+/', '-', $data['identifier']);
          $data['identifier'] = 'block-' . $identifier;
        }
        $visit_counts = unserialize($data['visit_counts']);
        $data['visit_counts'] = implode(",", $visit_counts);

        // Sanitize each entry.
        $popup_settings[] = array_map('Drupal\\Component\\Utility\\Html::escape', $data);
      }
    }
    $attachments['#attached']['library'][] = 'simple_popup_blocks/simple_popup_blocks';
    $attachments['#attached']['drupalSettings']['simple_popup_blocks']['settings'] = $popup_settings;
    $attachments['#cache']['tags'] = $cacheTags;
  }
}