You are here

page_load_progress.module in Page Load Progress 8

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

Allows to attach page_load_progress assets to all pages.

File

page_load_progress.module
View source
<?php

/**
 * @file
 * Allows to attach page_load_progress assets to all pages.
 */
use Drupal\Core\Config\ImmutableConfig;

/**
 * Checks if the current URL is defined in the visibility conditions.
 *
 * When being used, the visibility conditions allow to check if the current URL
 * is one of the paths defined in the visibility conditions. Return statement
 * is inverted by the is_negated() option.
 *
 * @param \Drupal\Core\Config\ImmutableConfig $config
 *   Configuration of the module.
 *
 * @return bool
 *   TRUE if the condition has been met, FALSE otherwise.
 */
function evaluate_visibility_conditions(ImmutableConfig $config) {

  // Convert path to lowercase. This allows comparison of the same path
  // with different case. Ex: /Page, /page, /PAGE.
  $pages = mb_strtolower($config
    ->get('page_load_progress_request_path'));
  if (!$pages) {
    return TRUE;
  }

  // Compare the lowercase path alias (if any) and internal path.
  $path = \Drupal::service('path.current')
    ->getPath();

  // Do not trim a trailing slash if that is the complete path.
  $path = $path === '/' ? $path : rtrim($path, '/');
  $path_alias = mb_strtolower(\Drupal::service('path.alias_manager')
    ->getAliasByPath($path));
  $result = \Drupal::service('path.matcher')
    ->matchPath($path_alias, $pages) || $path != $path_alias && \Drupal::service('path.matcher')
    ->matchPath($path, $pages);
  return is_negated($config) ? !$result : $result;
}

/**
 * Determines whether condition result will be negated.
 *
 * @param \Drupal\Core\Config\ImmutableConfig $config
 *   Configuration of the module.
 *
 * @return bool
 *   Determines whether page visibility is negated.
 */
function is_negated(ImmutableConfig $config) {
  return !empty($config
    ->get('page_load_progress_request_path_negate_condition'));
}

/**
 * Implements hook_page_attachments().
 */
function page_load_progress_page_attachments(array &$attachments) {

  // Load module configuration.
  $config = \Drupal::config('page_load_progress.settings');
  if (\Drupal::currentUser()
    ->hasPermission('use page load progress') && !strpos(\Drupal::service('path.current')
    ->getPath(), 'admin/structure/views/') && evaluate_visibility_conditions($config)) {

    // Unconditionally attach assets to the page.
    $attachments['#attached']['library'][] = 'page_load_progress/page_load_progress';

    // Attach config settings.
    $attachments['#attached']['drupalSettings']['page_load_progress'] = [
      'delay' => $config
        ->get('page_load_progress_time'),
      'elements' => $config
        ->get('page_load_progress_elements'),
      'internal_links' => $config
        ->get('page_load_progress_internal_links'),
      'esc_key' => $config
        ->get('page_load_progress_esc_key'),
    ];
  }
}

/**
 * Implements template_preprocess_input().
 */
function page_load_progress_preprocess_input(&$variables) {
  $element = $variables['element'];

  // Don't trigger the throbber on Ajax-based forms since this causes various
  // issues. E.g. with modal windows.
  if ($element['#type'] == 'submit' && !isset($element['#ajax'])) {
    $variables['attributes']['class'][] = 'page-load-progress-submit';
  }
}

Functions

Namesort descending Description
evaluate_visibility_conditions Checks if the current URL is defined in the visibility conditions.
is_negated Determines whether condition result will be negated.
page_load_progress_page_attachments Implements hook_page_attachments().
page_load_progress_preprocess_input Implements template_preprocess_input().