You are here

varbase_tour.module in Varbase Core 8.6

File

modules/varbase_tour/varbase_tour.module
View source
<?php

/**
 * @file
 * Contains varbase_tour.module.
 */

/**
 * Implements hook_page_attachments().
 */
function varbase_tour_page_attachments(array &$page) {

  // Given that the current user is a logged in user.
  if (\Drupal::currentUser()
    ->isAuthenticated()) {

    // When the url is NOT an admin route or in the admin theme.
    if (!\Drupal::service('router.admin_context')
      ->isAdminRoute()) {

      // Then attach the seven tour styling library.
      // And attach the varbase tour default theme library.
      $page['#attached']['library'][] = 'seven/tour-styling';
      $page['#attached']['library'][] = 'varbase_tour/default-theme';
    }

    // When the current page is the front page.
    if (\Drupal::service('path.matcher')
      ->isFrontPage()) {
      $query_welcome = \Drupal::request()->query
        ->get('welcome');
      if (isset($query_welcome)) {
        $varbase_tour_config = \Drupal::service('config.factory')
          ->getEditable('varbase_core.general_settings');
        $welcome_status = $varbase_tour_config
          ->get('welcome_status');

        // When we do have "/?tour=1&welcome=done" is in the URL address.
        // for the front page.
        if ($query_welcome == 'done' && isset($welcome_status) && $welcome_status == 1) {

          // Then update the "welcome status" checkbox config to unchecked.
          $varbase_tour_config
            ->set('welcome_status', 0);
          $varbase_tour_config
            ->save();
        }
      }
    }
  }
}

/**
 * Implements hook_theme().
 */
function varbase_tour_theme($existing, $type, $theme, $path) {
  return [
    'welcome_modal' => [
      'variables' => [
        'items' => [],
      ],
    ],
  ];
}

/**
 * Implements hook_page_top().
 */
function varbase_tour_page_top(array &$page_top) {

  // Given that the current user is a logged in user.
  if (\Drupal::currentUser()
    ->isAuthenticated()) {

    // And the current page is the front page.
    if (\Drupal::service('path.matcher')
      ->isFrontPage()) {
      $query_welcome = \Drupal::request()->query
        ->get('welcome');
      if (isset($query_welcome)) {
        $varbase_tour_config = \Drupal::service('config.factory')
          ->getEditable('varbase_core.general_settings');

        // When we do have "/?welcome" is in the URL address for the front page.
        if ($query_welcome != 'done') {

          // And the "welcome status" checkbox config is checked.
          $welcome_status = $varbase_tour_config
            ->get('welcome_status');
          if (isset($welcome_status) && $welcome_status == 1) {
            $page_top['welcome_modal'] = [
              '#type' => 'container',
              '#theme' => 'welcome_modal',
              '#access' => \Drupal::currentUser()
                ->isAuthenticated(),
              '#cache' => [
                'keys' => [
                  'varbase_core',
                ],
                'contexts' => [
                  'user.permissions',
                ],
              ],
            ];
          }
        }
      }
    }
  }
}