You are here

function fontawesome_preprocess_html in Font Awesome Icons 7.3

Same name and namespace in other branches
  1. 7.2 fontawesome.module \fontawesome_preprocess_html()

Implements hook_preprocess_html().

Purposefully only load on page requests and not hook_init(). This is required so it does not increase the bootstrap time of Drupal when it isn't necessary.

File

./fontawesome.module, line 304
Drupal integration with Font Awesome 5.

Code

function fontawesome_preprocess_html() {

  // Get module settings.
  $method = variable_get('fontawesome_method', 'svg');
  $external_settings = variable_get('fontawesome_external', array(
    'use_cdn' => TRUE,
    'external_location' => '',
  ));
  $shim_settings = variable_get('fontawesome_shim', array(
    'use_shim' => FALSE,
    'external_location' => '',
  ));
  $partial_files_settings = variable_get('fontawesome_partial', array(
    'use_solid_file' => TRUE,
    'use_regular_file' => TRUE,
    'use_light_file' => TRUE,
    'use_brands_file' => TRUE,
  ));

  // Check if we are using the CDN.
  if ($external_settings['use_cdn']) {

    // First check if we're using everything.
    if ($partial_files_settings['use_solid_file'] && $partial_files_settings['use_regular_file'] && $partial_files_settings['use_light_file'] && $partial_files_settings['use_brands_file']) {

      // Webfonts.
      if ($method == 'webfonts') {

        // Load the file.
        drupal_add_css($external_settings['external_location'], array(
          'type' => 'external',
          'group' => CSS_THEME,
          'every_page' => TRUE,
        ));
      }
      elseif ($method == 'svg') {

        // Load the file.
        drupal_add_js($external_settings['external_location'], array(
          'type' => 'external',
          'scope' => 'footer',
          'group' => JS_THEME,
          'every_page' => TRUE,
          'weight' => 10,
        ));
      }
    }
    else {

      // Determine the base for the CDN.
      $cdn_components = _fontawesome_get_base_path($external_settings['external_location']);

      // Webfonts.
      if ($method == 'webfonts') {

        // Load the base file.
        drupal_add_css($cdn_components . 'fontawesome.css', array(
          'type' => 'external',
          'group' => CSS_THEME,
          'every_page' => TRUE,
        ));
        if ($partial_files_settings['use_solid_file']) {

          // Load the solid file.
          drupal_add_css($cdn_components . 'solid.css', array(
            'type' => 'external',
            'group' => CSS_THEME,
            'every_page' => TRUE,
          ));
        }
        if ($partial_files_settings['use_regular_file']) {

          // Load the regular file.
          drupal_add_css($cdn_components . 'regular.css', array(
            'type' => 'external',
            'group' => CSS_THEME,
            'every_page' => TRUE,
          ));
        }
        if ($partial_files_settings['use_light_file']) {

          // Load the light file.
          drupal_add_css($cdn_components . 'light.css', array(
            'type' => 'external',
            'group' => CSS_THEME,
            'every_page' => TRUE,
          ));
        }
        if ($partial_files_settings['use_brands_file']) {

          // Load the brands file.
          drupal_add_css($cdn_components . 'brands.css', array(
            'type' => 'external',
            'group' => CSS_THEME,
            'every_page' => TRUE,
          ));
        }
      }
      elseif ($method == 'svg') {

        // Load the base file.
        drupal_add_js($cdn_components . 'fontawesome.js', array(
          'type' => 'external',
          'scope' => 'footer',
          'group' => JS_THEME,
          'every_page' => TRUE,
          'weight' => 10,
        ));
        if ($partial_files_settings['use_solid_file']) {

          // Load the solid file.
          drupal_add_js($cdn_components . 'solid.js', array(
            'type' => 'external',
            'scope' => 'footer',
            'group' => JS_THEME,
            'every_page' => TRUE,
            'weight' => 10,
          ));
        }
        if ($partial_files_settings['use_regular_file']) {

          // Load the regular file.
          drupal_add_js($cdn_components . 'regular.js', array(
            'type' => 'external',
            'scope' => 'footer',
            'group' => JS_THEME,
            'every_page' => TRUE,
            'weight' => 10,
          ));
        }
        if ($partial_files_settings['use_light_file']) {

          // Load the light file.
          drupal_add_js($cdn_components . 'light.js', array(
            'type' => 'external',
            'scope' => 'footer',
            'group' => JS_THEME,
            'every_page' => TRUE,
            'weight' => 10,
          ));
        }
        if ($partial_files_settings['use_brands_file']) {

          // Load the brands file.
          drupal_add_js($cdn_components . 'brands.js', array(
            'type' => 'external',
            'scope' => 'footer',
            'group' => JS_THEME,
            'every_page' => TRUE,
            'weight' => 10,
          ));
        }
      }
    }

    // Handle backwards compatibility shim file.
    if ($method == 'svg' && $shim_settings['use_shim']) {
      drupal_add_js($shim_settings['external_location'], array(
        'type' => 'external',
        'scope' => 'footer',
        'group' => JS_THEME,
        'every_page' => TRUE,
        'weight' => 11,
      ));
    }
  }
  else {

    // First check if we're using everything.
    if ($partial_files_settings['use_solid_file'] && $partial_files_settings['use_regular_file'] && $partial_files_settings['use_light_file'] && $partial_files_settings['use_brands_file']) {

      // Load the main library.
      $library = libraries_load(FONTAWESOME_LIBRARY . '_' . $method);
      if (!$library['loaded']) {
        drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
      }
    }
    else {
      if ($partial_files_settings['use_solid_file']) {
        $library = libraries_load(FONTAWESOME_LIBRARY . '_' . $method . '_solid');
        if (!$library['loaded']) {
          drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
        }
      }
      if ($partial_files_settings['use_regular_file']) {
        $library = libraries_load(FONTAWESOME_LIBRARY . '_' . $method . '_regular');
        if (!$library['loaded']) {
          drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
        }
      }
      if ($partial_files_settings['use_light_file']) {
        $library = libraries_load(FONTAWESOME_LIBRARY . '_' . $method . '_light');
        if (!$library['loaded']) {
          drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
        }
      }
      if ($partial_files_settings['use_brands_file']) {
        $library = libraries_load(FONTAWESOME_LIBRARY . '_' . $method . '_brands');
        if (!$library['loaded']) {
          drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
        }
      }
    }

    // Handle backwards compatibility shim file.
    if ($method == 'svg' && $shim_settings['use_shim']) {
      $library = libraries_load(FONTAWESOME_LIBRARY . '_shim');
      if (!$library['loaded']) {
        drupal_set_message($library['error message'] . ' ' . t('Please make sure that Font Awesome was downloaded and extracted in the sites/all/libraries/fontawesome directory. Please check README.txt for more details.'), 'warning');
      }
    }
  }
}