You are here

fontawesome.module in Font Awesome Icons 7.2

fontawesome.module Drupal integration with Font Awesome, the iconic font for use with Bootstrap.

File

fontawesome.module
View source
<?php

/**
 * @file
 * fontawesome.module
 * Drupal integration with Font Awesome, the iconic font for use with Bootstrap.
 */
define('FONTAWESOME_LIBRARY', 'fontawesome');
define('FONTAWESOME_NAME', 'Font Awesome');
define('FONTAWESOME_URL', 'http://fontawesome.io');
define('FONTAWESOME_CDN_URL', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
define('FONTAWESOME_DOWNLOAD_URL', 'https://github.com/FortAwesome/Font-Awesome/archive/v4.7.0.zip');
define('FONTAWESOME_PREFIX', 'fa');

/**
 * Implements hook_help().
 */
function fontawesome_help($path, $arg) {
  switch ($path) {
    case 'admin/help#fontawesome':
      return '<p><i class="icon-flag icon-large"></i>' . t('<a href="!fontawesome_url">@fontawesome</a> is an iconic font designed for use with <a href="!bootstrap_url">Bootstrap</a>.', array(
        '@fontawesome' => FONTAWESOME_NAME,
        '!fontawesome_url' => FONTAWESOME_URL,
        '!bootstrap_url' => 'http://getbootstrap.com',
      )) . '</p>';
  }
}

/**
 * Implements hook_libraries_info().
 */
function fontawesome_libraries_info() {
  $libraries[FONTAWESOME_LIBRARY] = array(
    'name' => FONTAWESOME_NAME,
    'vendor url' => FONTAWESOME_URL,
    'download url' => FONTAWESOME_DOWNLOAD_URL,
    'download file url' => FONTAWESOME_DOWNLOAD_URL,
    'version arguments' => array(
      'file' => 'css/font-awesome.css',
      'pattern' => '/((?:\\d+\\.?){2,3})/',
      'lines' => 10,
      'cols' => 14,
    ),
    'files' => array(
      'css' => array(
        'css/font-awesome.css',
      ),
    ),
  );
  return $libraries;
}

/**
 * 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.
 */
function fontawesome_preprocess_html() {
  if (variable_get('fontawesome_use_cdn', FALSE)) {
    drupal_add_css(FONTAWESOME_CDN_URL, array(
      'type' => 'external',
    ));
  }
  else {
    $library = libraries_load(FONTAWESOME_LIBRARY);
    if (!$library['loaded']) {
      drupal_set_message($library['error message'] . t('Please make sure that ' . 'fontawesome was download & extracted at sites/all/libraries/fontawesome directory.' . 'Please check README.txt for more details.'), 'warning');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Add a checkbox to Drupal Bandwidth optimization settings in performance page
 * to provide an option to load CDN version of FontAwesome.
 */
function fontawesome_form_system_performance_settings_alter(&$form, &$form_state, $form_id) {
  $form['bandwidth_optimization']['fontawesome_use_cdn'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use CDN version of FontAwesome.'),
    '#default_value' => variable_get('fontawesome_use_cdn', FALSE),
  );
}

/**
 * Implements hook_icon_providers().
 */
function fontawesome_icon_providers() {
  $providers[FONTAWESOME_LIBRARY] = array(
    'title' => FONTAWESOME_NAME,
    'url' => FONTAWESOME_URL,
  );
  return $providers;
}

/**
 * Implements hook_icon_bundle_configure().
 */
function fontawesome_icon_bundle_configure(&$settings, &$form_state, &$complete_form) {
  $bundle = $form_state['bundle'];
  if ($bundle['provider'] === FONTAWESOME_LIBRARY) {
    $settings['tag'] = array(
      '#type' => 'select',
      '#title' => t('HTML Markup'),
      '#description' => t('Choose the HTML markup tag that @fontawesome icons should be created with. Typically, this is a %tag tag, however it can be changed to suite the theme requirements.', array(
        '@fontawesome' => FONTAWESOME_NAME,
        '%tag' => '<' . $bundle['settings']['tag'] . '>',
      )),
      '#options' => drupal_map_assoc(array(
        'i',
        'span',
        'div',
      )),
      '#default_value' => $bundle['settings']['tag'],
    );
  }
}

/**
 * Implements hook_preprocess_icon_RENDER_HOOK().
 */
function fontawesome_preprocess_icon_sprite(&$variables) {
  $bundle =& $variables['bundle'];
  if ($bundle['provider'] === FONTAWESOME_LIBRARY) {

    // Remove the default "icon" class.
    $key = array_search('icon', $variables['attributes']['class']);
    if ($key !== FALSE) {
      unset($variables['attributes']['class'][$key]);
    }

    // Add the necessary FA identifier class.
    $variables['attributes']['class'][] = FONTAWESOME_PREFIX;

    // Prepend the icon with the FA prefix (which will be used as the class).
    $variables['icon'] = FONTAWESOME_PREFIX . '-' . $variables['icon'];
  }
}

/**
 * Implements hook_icon_bundles().
 */
function fontawesome_icon_bundles() {
  $bundles[FONTAWESOME_LIBRARY] = array(
    'title' => FONTAWESOME_NAME,
    'provider' => FONTAWESOME_LIBRARY,
    'render' => 'sprite',
    'settings' => array(
      'tag' => 'i',
    ),
    'icons' => fontawesome_extract_icons(),
  );
  return $bundles;
}

/**
 * Extracts all icons from the CSS file.
 *
 * @return array
 */
function fontawesome_extract_icons() {

  // If CDN is enabled, get CSS content from the CDN URL
  if (variable_get('fontawesome_use_cdn', FALSE)) {
    $url = FONTAWESOME_CDN_URL;

    // The URL needs to have a schema to work with drupal_http_request
    if (strpos($url, '//') === 0) {
      $url = 'http:' . $url;
    }
    $request = drupal_http_request($url);
    $content = !empty($request->data) ? $request->data : '';
  }
  else {
    $library = libraries_load(FONTAWESOME_LIBRARY);
    $filepath = DRUPAL_ROOT . '/' . $library['library path'] . '/css/font-awesome.css';
    $content = file_exists($filepath) ? file_get_contents($filepath) : '';
  }

  // Parse the CSS content
  if (preg_match_all('@\\.fa-([a-zA-Z0-9-]*?):before@m', $content, $matches)) {
    $icons = $matches[1];
    asort($icons);
    return drupal_map_assoc($icons);
  }
  return array();
}

Functions

Namesort descending Description
fontawesome_extract_icons Extracts all icons from the CSS file.
fontawesome_form_system_performance_settings_alter Implements hook_form_FORM_ID_alter().
fontawesome_help Implements hook_help().
fontawesome_icon_bundles Implements hook_icon_bundles().
fontawesome_icon_bundle_configure Implements hook_icon_bundle_configure().
fontawesome_icon_providers Implements hook_icon_providers().
fontawesome_libraries_info Implements hook_libraries_info().
fontawesome_preprocess_html Implements hook_preprocess_html().
fontawesome_preprocess_icon_sprite Implements hook_preprocess_icon_RENDER_HOOK().

Constants

Namesort descending Description
FONTAWESOME_CDN_URL
FONTAWESOME_DOWNLOAD_URL
FONTAWESOME_LIBRARY @file fontawesome.module Drupal integration with Font Awesome, the iconic font for use with Bootstrap.
FONTAWESOME_NAME
FONTAWESOME_PREFIX
FONTAWESOME_URL