You are here

ajax_loader.module in Ajax loader 8

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

File

ajax_loader.module
View source
<?php

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function ajax_loader_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.ajax_loader':
      $output = file_get_contents(drupal_get_path('module', 'ajax_loader') . '/README.txt');
      return '<pre>' . $output . '</pre>';
  }
}

/**
 * Implements hook_library_info_alter().
 */
function ajax_loader_library_info_alter(&$libraries, $module) {
  if ($module == 'ajax_loader') {

    /** @var \Drupal\ajax_loader\ThrobberManagerInterface $throbber_manager */
    $throbber_manager = Drupal::service('ajax_loader.throbber_manager');

    // Add all css for throbber plugins on admin page.
    foreach ($throbber_manager
      ->loadAllThrobberInstances() as $throbber) {

      /** @var \Drupal\ajax_loader\ThrobberPluginInterface $throbber */
      if ($css_file = $throbber
        ->getCssFile()) {
        $libraries['ajax_loader.admin']['css']['theme'][$css_file] = [];
      }
    }
    $settings = \Drupal::config('ajax_loader.settings');
    if ($settings
      ->get('throbber') && isset($libraries['ajax_loader.throbber'])) {

      // Add css for chosen throbber.
      $throbber = $throbber_manager
        ->loadThrobberInstance($settings
        ->get('throbber'));
      if ($css_file = $throbber
        ->getCssFile()) {
        $libraries['ajax_loader.throbber']['css']['theme'][$css_file] = [];
      }
    }
  }
}

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

  /** @var \Drupal\ajax_loader\ThrobberManagerInterface $throbber_manager */
  $settings = \Drupal::config('ajax_loader.settings');
  $throbber_manager = Drupal::service('ajax_loader.throbber_manager');
  $throbber = $settings
    ->get('throbber');
  if ($throbber_manager
    ->getDefinition($throbber, FALSE) && $throbber_manager
    ->RouteIsApplicable()) {

    /** @var \Drupal\ajax_loader\ThrobberPluginInterface $throbber */
    $throbber = $throbber_manager
      ->loadThrobberInstance($settings
      ->get('throbber'));
    $settings = [
      'markup' => $throbber
        ->getMarkup(),
      'hideAjaxMessage' => $settings
        ->get('hide_ajax_message'),
      'alwaysFullscreen' => $settings
        ->get('always_fullscreen'),
      'throbberPosition' => $settings
        ->get('throbber_position'),
    ];
    $page['#attached']['drupalSettings']['ajaxLoader'] = $settings;
    $page['#attached']['library'][] = 'ajax_loader/ajax_loader.throbber';
  }
}