You are here

popup_dialog.module in Popup Dialog 8.2

Same filename and directory in other branches
  1. 8 popup_dialog.module

Contains popup_dialog.module..

File

popup_dialog.module
View source
<?php

/**
 * @file
 * Contains popup_dialog.module..
 */
use Drupal\block\BlockViewBuilder;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\block\Entity\Block;

/**
 * Implements hook_help().
 */
function popup_dialog_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the popup_dialog module.
    case 'help.page.popup_dialog':
      $text = file_get_contents(dirname(__FILE__) . "/README.txt");
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . $text . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
      break;
    default:
  }
}

/**
 * Implements hook_page_attachments().
 */
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;
  }
}

Functions