You are here

function quicklink_preprocess_html in Quicklink 7

Same name and namespace in other branches
  1. 8 quicklink.module \quicklink_preprocess_html()
  2. 2.0.x quicklink.module \quicklink_preprocess_html()

Implements hook_preprocess_html().

File

./quicklink.module, line 233

Code

function quicklink_preprocess_html(&$variables) {
  $settings = array();

  // Load current configuration.
  $selector = variable_get('quicklink_selector', '');

  // Get debug variable.
  $debug = variable_get('quicklink_enable_debug_mode', 0);
  $debug_log = array();

  // Load the library unless we disable later.
  $load_library = TRUE;

  // Always ignore the logout link.
  $url_patterns_to_ignore = array(
    'user/logout',
  );
  $debug_log[] = 'Quicklink will ignore "user/logout" URL pattern.';
  $allowed_domains = array();

  // Populate and remove line returns from URL patterns to ignore.
  foreach (explode(PHP_EOL, variable_get('quicklink_url_patterns_to_ignore', '')) as $value) {
    $pattern = str_replace("\r", '', $value);
    if (!empty($pattern)) {
      $url_patterns_to_ignore[] = $pattern;
    }
  }

  // Populate and remove line returns from allowed domains.
  foreach (explode(PHP_EOL, variable_get('quicklink_allowed_domains', '')) as $value) {
    $domain = str_replace("\r", '', $value);
    if (!empty($domain)) {
      $allowed_domains[] = $domain;
    }
  }

  // Populate and remove line returns from "Prefetch these paths only".
  foreach (explode(PHP_EOL, variable_get('quicklink_prefetch_only_paths', '')) as $value) {
    $path = str_replace("\r", '', $value);
    if (!empty($path)) {
      $prefetch_only_paths[] = $path;
    }
  }

  // Check for "Ignore Hashes" option.
  if (variable_get('quicklink_ignore_hashes', 1) == 1) {
    $url_patterns_to_ignore[] = '#';
    $debug_log[] = 'Quicklink will ignore URLs with hashes(#).';
  }

  // If "Ignore admin paths" is selected, ignore the admin paths.
  if (variable_get('quicklink_ignore_admin_paths', 1) == 1) {
    $url_patterns_to_ignore[] = '/admin';
    $url_patterns_to_ignore[] = '/edit';

    // If elements match these selector pattern, they will not be prefetched.
    $admin_link_container_patterns = array(
      '#toolbar a',
      '#overlay a',
      '#admin-menu a',
      '#tabs a',
    );
    $settings['admin_link_container_patterns'] = $admin_link_container_patterns;
    $debug_log[] = 'Quicklink will ignore admin URL patterns.';
  }

  // Check for "Content Types" option.
  $node = menu_get_object();
  if (isset($node) && !empty($node->type)) {
    $no_load_content_types = variable_get('quicklink_no_load_content_types', array());
    if (in_array($node->type, $no_load_content_types) && $no_load_content_types[$node->type]) {
      $load_library = FALSE;
      $debug_log[] = 'Library not loaded because content type "' . $node->type . '" is specified to not load library.';
    }
  }

  // If user is logged in AND "Prefetch for anonymous users only" is selected,
  // do not load library.
  global $user;
  if (isset($user) && variable_get('quicklink_no_load_when_authenticated', 1) == 1 && !empty($user->uid) && $user->uid >= 1) {
    $load_library = FALSE;
    $debug_log[] = 'Library not loaded because user is authenticated.';
  }

  // Disable the library when a session is started.
  if (variable_get('quicklink_no_load_when_session', 1)) {
    $session = drupal_session_started();
    if (!empty($session)) {
      $load_library = FALSE;
      $debug_log[] = 'Library not loaded because PHP session is started.';
    }
  }

  // Disable the library if page caching is disabled.
  if (variable_get('quicklink_no_load_without_page_cache', 1) && !variable_get('cache', 0)) {
    $load_library = FALSE;
    $debug_log[] = 'Library not loaded because Drupal page caching is disabled.';
  }

  // Disable the library if block caching is disabled.
  if (variable_get('quicklink_no_load_without_block_cache', 1) && !variable_get('block_cache', 0)) {
    $load_library = FALSE;
    $debug_log[] = 'Library not loaded because Drupal block caching is disabled.';
  }
  $settings['ignore_admin_paths'] = variable_get('quicklink_ignore_admin_paths', 1);
  $settings['ignore_ajax_links'] = variable_get('quicklink_ignore_ajax_links', 1);
  $settings['ignore_file_ext'] = variable_get('quicklink_ignore_file_ext', 1);
  $settings['debug'] = $debug;
  if (!empty($url_patterns_to_ignore[0])) {
    $settings['url_patterns_to_ignore'] = $url_patterns_to_ignore;
    $debug_log['url_patterns_to_ignore'][] = $url_patterns_to_ignore;
  }
  if (!empty($selector)) {
    $settings['selector'] = $selector;
    $debug_log[] = 'Selector for Quicklink to parse: ' . $selector;
  }
  if (!empty($allowed_domains[0])) {
    $settings['allowed_domains'] = $allowed_domains;
    $debug_log['allowed_domains'][] = $allowed_domains;
  }
  if (!empty($prefetch_only_paths[0])) {
    $settings['prefetch_only_paths'] = $prefetch_only_paths;
    $debug_log['prefetch_only_paths'][] = $prefetch_only_paths;
  }
  if ($load_library) {
    if (variable_get('quicklink_load_polyfill', 1) == 1) {
      drupal_add_js('https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver%2CIntersectionObserverEntry%2CURL%2CObject.assign%2CArray.from%2CArray.prototype.includes%2CString.prototype.includes%2CElement.prototype.matches%2CPromise%2CPromise.prototype.finally', array(
        'type' => 'external',
        'weight' => -100,
      ));
      $debug_log[] = 'Intersection Observer polyfill library loaded';
    }

    // Load Quicklink library.
    if (file_exists(DRUPAL_ROOT . '/sites/all/libraries/quicklink/quicklink.umd.js')) {
      drupal_add_js('sites/all/libraries/quicklink/quicklink.umd.js', array(
        'weight' => -20,
      ));
    }
    else {
      drupal_add_js('https://unpkg.com/quicklink@1.0.1/dist/quicklink.umd.js', array(
        'type' => 'external',
        'weight' => -20,
      ));
    }
  }
  if ($debug) {
    $settings['debug'] = 1;
    drupal_add_css(drupal_get_path('module', 'quicklink') . '/css/quicklink-debug.css');
    $settings['debug_log'] = $debug_log;
  }
  drupal_add_js(array(
    'quicklink' => $settings,
  ), array(
    'type' => 'setting',
  ));
  drupal_add_js(drupal_get_path('module', 'quicklink') . '/js/quicklink_init.js');
}