You are here

function simple_popup_blocks_page_attachments in Simple Popup Blocks 8

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

Implements hook_page_attachments().

File

./simple_popup_blocks.module, line 36
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);
  $database = \Drupal::getContainer()
    ->get('database');
  if (!$is_admin) {
    $popup_settings = [];
    foreach (SimplePopupBlocksStorage::loadAll($database) as $entry) {
      if ($entry->status == 1) {
        if ($entry->type == 0) {
          $identifier = preg_replace('/[_]+/', '-', $entry->identifier);
          $entry->identifier = 'block-' . $identifier;
        }
        $visit_counts = unserialize($entry->visit_counts);
        $entry->visit_counts = implode(",", $visit_counts);

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