You are here

function popup_dialog_page_attachments in Popup Dialog 8.2

Same name and namespace in other branches
  1. 8 popup_dialog.module \popup_dialog_page_attachments()

Implements hook_page_attachments().

File

./popup_dialog.module, line 40
Contains popup_dialog.module..

Code

function popup_dialog_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'popup_dialog/dialog_box';
  $config = \Drupal::config('popup_dialog.settings');
  $enabled = $config
    ->get('popup_enabled');
  $attachments['#attached']['drupalSettings']['enabled'] = $enabled;

  // Get the cookie if available else pass the configs to JS.
  $mycookie = \Drupal::request()->cookies
    ->get('FirstUser');
  if ($mycookie != 1 && $enabled == 1) {
    $category_settings = $config
      ->get('category_settings');
    if ($category_settings == 1) {
      $title = $config
        ->get('popup_box_title');
      $body_raw = $config
        ->get('popup_box_body');
      $body = $body_raw['value'];
      $attachments['#attached']['drupalSettings']['title'] = $title;
      $attachments['#attached']['drupalSettings']['body'] = $body;
    }
    elseif ($category_settings == 2) {
      $get_plugin_id = $config
        ->get('list_of_blocks');
      $split_get_plugin_id = explode("|", $get_plugin_id);
      $plugin_id = $split_get_plugin_id[0];
      $title = strtolower($split_get_plugin_id[1]);
      $entityQuery = \Drupal::entityQuery('block');
      $entityQuery
        ->condition('plugin', $plugin_id);
      $block_id = $entityQuery
        ->execute();
      $theme = \Drupal::service('theme.manager')
        ->getActiveTheme()
        ->getName();
      if (empty($block_id)) {
        $remove_space_title = str_replace(' ', '', $title);
        $title = preg_replace('/[^A-Za-z0-9]/', '', $remove_space_title);
        $block_id = $title . "_" . rand();
        $values = [
          'id' => $block_id,
          'plugin' => $plugin_id,
          'region' => 'content',
          'theme' => $theme,
          'settings' => [
            'label_display' => 0,
          ],
        ];
        $block = Block::create($values);
        $block
          ->save();
      }
      if (is_array($block_id)) {
        $block_id = array_keys($block_id)[0];
      }
      $block_details = BlockViewBuilder::lazyBuilder($block_id, 'full');
      $render = \Drupal::service('renderer')
        ->renderPlain($block_details);
      $attachments['#attached']['drupalSettings']['title'] = $render
        ->__toString();
      $attachments['#attached']['drupalSettings']['body'] = '';
    }
    elseif ($category_settings == 3) {
      $get_view = $config
        ->get('list_of_views');
      $arg = $config
        ->get('arguments');
      $exp_view = explode(":", $get_view);
      $view_name = $exp_view[0];
      $display_id = $exp_view[1];
      $result = views_embed_view($view_name, $display_id, $arg);
      $render = \Drupal::service('renderer')
        ->renderPlain($result);
      $attachments['#attached']['drupalSettings']['title'] = $render
        ->__toString();
      $attachments['#attached']['drupalSettings']['body'] = '';
    }
    $delay = $config
      ->get('delay');
    $top = $config
      ->get('popup_top_position');
    $attachments['#attached']['drupalSettings']['delay'] = $delay;
    $attachments['#attached']['drupalSettings']['top'] = $top;
  }
}