You are here

outdatedbrowser.module in Outdated Browser 8

Same filename and directory in other branches
  1. 7 outdatedbrowser.module

Outdated Browser module file.

File

outdatedbrowser.module
View source
<?php

/**
 * @file
 * Outdated Browser module file.
 */
use Drupal\Core\Installer\InstallerKernel;

/**
 * Implements hook_page_attachments().
 */
function outdatedbrowser_page_attachments(array &$attachments) {
  if (!_outdatedbrowser_is_active()) {
    return;
  }
  $config = \Drupal::config('outdatedbrowser.settings');
  $js_settings = array(
    'bgColor' => $config
      ->get('bgcolor'),
    'color' => $config
      ->get('color'),
    'lowerThan' => $config
      ->get('lowerthan'),
    'languagePath' => file_create_url(outdatedbrowser_get_language_file_path()),
  );
  $attachments['#attached']['drupalSettings']['outdatedbrowser'] = $js_settings;
  $attachments['#attached']['library'][] = 'outdatedbrowser/drupal';
}

/**
 * Implements hook_page_bottom().
 */
function outdatedbrowser_page_bottom(array &$page_bottom) {
  if (!_outdatedbrowser_is_active()) {
    return;
  }
  $page_bottom['outdatedbrowser'] = array(
    '#markup' => '<div id="outdated"></div>',
  );
}

/**
 * Implements hook_library_info_alter().
 */
function outdatedbrowser_library_info_alter(array &$libraries, $module) {

  // Use uncompressed version of Outdated Browser library,
  // if 'source' is selected as compression type.
  if ($module === 'outdatedbrowser' && \Drupal::config('outdatedbrowser.settings')
    ->get('compression_type') == 'source') {
    $libraries['library'] = $libraries['library.uncompressed'];
  }
}

/**
 * Gets the path of the appropriate language file of Outdated Browser plugin.
 */
function outdatedbrowser_get_language_file_path() {
  $result = '';
  $config = \Drupal::config('outdatedbrowser.settings');
  if (!($library_path = $config
    ->get('lang_files_path'))) {
    $library_path = 'libraries/outdated-browser/outdatedbrowser/lang';

    //TODO - get the libraries path with some function
  }
  $langfile_pattern = $library_path . "/%s.html";
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $default_language = \Drupal::languageManager()
    ->getDefaultLanguage();
  if (file_exists(\Drupal::service('file_system')
    ->realpath(sprintf($langfile_pattern, $current_language
    ->getId())))) {

    // A translation file for the active interface language was found.
    $result = sprintf($langfile_pattern, $current_language
      ->getId());
  }
  elseif (file_exists(\Drupal::service('file_system')
    ->realpath(sprintf($langfile_pattern, $default_language
    ->getId())))) {

    // A translation file for the default system language was found (fallback).
    $result = sprintf($langfile_pattern, $default_language
      ->getId());
  }
  elseif (file_exists(\Drupal::service('file_system')
    ->realpath(sprintf($langfile_pattern, 'en')))) {

    // An english translation file was found (second and last fallback method).
    $result = sprintf($langfile_pattern, 'en');
  }
  return $result;
}

/**
 * Checks if library should be included in current page request.
 *
 * @return boolean TRUE, if library should be included in current page request,
 *         FALSE otherwise.
 */
function _outdatedbrowser_is_active() {
  $show = TRUE;
  if (InstallerKernel::installationAttempted()) {
    $show = FALSE;
  }
  elseif (\Drupal::service('router.admin_context')
    ->isAdminRoute(\Drupal::routeMatch()
    ->getRouteObject())) {

    // Hide on admin pages.
    $show = FALSE;
  }
  return $show;
}

Functions

Namesort descending Description
outdatedbrowser_get_language_file_path Gets the path of the appropriate language file of Outdated Browser plugin.
outdatedbrowser_library_info_alter Implements hook_library_info_alter().
outdatedbrowser_page_attachments Implements hook_page_attachments().
outdatedbrowser_page_bottom Implements hook_page_bottom().
_outdatedbrowser_is_active Checks if library should be included in current page request.