google_webfont_loader_api.module in Webfont Loader 6
Same filename and directory in other branches
Google Webfont Loader API primary file The designer/developer creates a set of packages (will use .fontinfo files created in a similar manner to a module or theme .info file) from which the site admin can then choose for their site. The fonts can then be tied to how the page and various rules on the page get rendered.
File
google_webfont_loader_api.moduleView source
<?php
/**
* @file
* Google Webfont Loader API primary file
* The designer/developer creates a set of packages (will use .fontinfo files
* created in a similar manner to a module or theme .info file) from which the
* site admin can then choose for their site. The fonts can then be tied to how
* the page and various rules on the page get rendered.
*/
/**
* Implements hook_cron().
*/
function google_webfont_loader_api_cron() {
// Regenerate the google webfont loader webfont.js every day.
if (time() - variable_get('google_webfont_loader_api_last_cache', 0) >= 86400) {
// New webfont.js version.
file_delete(file_directory_path() . '/google_webfont_loader_api/webfont.js');
// Clear aggregated JS files.
if (variable_get('preprocess_js', 0)) {
drupal_clear_js_cache();
}
variable_set('google_webfont_loader_api_last_cache', time());
}
}
/**
* Implements hook_init().
*/
function google_webfont_loader_api_init() {
global $base_path;
// First deal with the webfont.js file.
$webfont_url = "http://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js";
if (variable_get('google_webfont_loader_api_cache', 0) && variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC && ($source = _google_webfont_loader_api_cache($webfont_url))) {
drupal_add_js($source, 'module');
// Add a drupal setting.
drupal_add_js(array(
'google_webfont_loader_api_preloaded' => 1,
), 'setting');
}
else {
drupal_add_js(array(
'google_webfont_loader_api_preloaded' => 0,
), 'setting');
}
// Retrieve the font list and the font the user chose.
$font_list = google_webfont_loader_api_get_font_list();
$user_chosen_font = variable_get('google_webfont_loader_api_font', '');
if (array_key_exists($user_chosen_font, $font_list)) {
$font = $font_list[$user_chosen_font];
$filepath = str_replace('//', '/', dirname($font->filename));
$font_info = $font->info;
$loader_js = _google_webfont_loader_api_load_js_from_fontinfo($font_info, $filepath);
// If there are items in the loader_js, create a setting and load the render
// css which is useless unless correct information regarding fonts are
// actually loaded.
if (count($loader_js) > 0) {
$loader_js = array(
'google_webfont_loader_api_setting' => $loader_js,
);
drupal_add_js($loader_js, 'setting');
drupal_add_js(drupal_get_path('module', 'google_webfont_loader_api') . '/google_webfont_loader_api.js');
if (variable_get('google_webfont_loader_api_display_style', '') == 'hidden') {
_google_webfont_loader_api_add_hidden_css();
}
// Start loading the css that will be rendered.
if (isset($font_info['render_css'])) {
if (is_array($font_info['render_css'])) {
foreach ($font_info['render_css'] as $render_css) {
drupal_add_css($filepath . '/' . $render_css);
}
}
else {
drupal_add_css($filepath . '/' . $font_info['render_css']);
}
}
}
}
}
/**
* Implements hook_perm().
*/
function google_webfont_loader_api_perm() {
return array(
'administer google webfont loader api',
);
}
/**
* Implements hook_menu().
*/
function google_webfont_loader_api_menu() {
$items = array();
$items['admin/build/themes/manage/google_webfont_loader_api'] = array(
'title' => 'Google Webfont Loader Settings',
'description' => 'Way to load custom webfonts on your site',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'google_webfont_loader_api_site_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer google webfont loader api',
),
'file' => 'google_webfont_loader_api.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Retrieves the list of available fonts.
*/
function google_webfont_loader_api_get_font_list($reset = FALSE) {
// I am unsure of how expensive a call to drupal_system_listing is. As such,
// I am caching the font information.
$listings = cache_get('google_webfont_loader_api_font_list');
if ($reset || !$listings) {
$listings = drupal_system_listing('\\.fontinfo', '');
$font_list = array();
foreach ($listings as $key => $listing) {
$listing->info = drupal_parse_info_file($listing->filename);
}
cache_set('google_webfont_loader_api_font_list', $listings);
}
else {
$listings = $listings->data;
}
return $listings;
}
/**
* Parses a fontinfo description and create a javascript settings loader
*/
function _google_webfont_loader_api_load_js_from_fontinfo($font_info, $filepath) {
$url = url($filepath, array(
'absolute' => TRUE,
)) . '/';
$loader_js = array();
// Load all the information from the font info file into the loader_js.
// loader_js is a settings format recognized by the webfont loader.
// You can specify your google fonts, typekit id, or custom fonts (with their
// fontface css locations).
foreach ($font_info as $key => $key_info) {
$info = $key_info;
if (!is_array($key_info)) {
$info = array(
$key_info,
);
}
if ($key == 'google_families' && is_array($info)) {
$loader_js['google'] = array(
'families' => $info,
);
}
if ($key == "typekit") {
$loader_js['typekit'] = array(
'id' => $info[0],
);
}
if ($key == 'fontdeck') {
$loader_js['fontdeck'] = array(
'id' => $info[0],
);
}
if ($key == 'monotype') {
$loader_js['monotype'] = array(
'projectId' => $info[0],
);
}
// A custom style css must be specified if using custom families.
if ($key == "custom_families" && is_array($font_info['custom_style_css'])) {
$loader_js['custom'] = array(
'families' => $info,
'urls' => array(),
);
foreach ($font_info['custom_style_css'] as $custom_style) {
if (strpos($custom_style, 'http://') === 0) {
$loader_js['custom']['urls'][] = $custom_style;
}
else {
$loader_js['custom']['urls'][] = $url . $custom_style;
}
}
}
}
return $loader_js;
}
/**
* Downloads and caches the webfont.js file locally.
*
* @param string $location
* The full URL to the external javascript file.
*
* @return mixed
* The path to the local javascript file on success, boolean FALSE on failure.
*/
function _google_webfont_loader_api_cache($location) {
$directory = file_directory_path() . '/google_webfont_loader_api';
$file_destination = $directory . '/' . basename($location);
if (!file_exists($file_destination)) {
$result = drupal_http_request($location);
if ($result->code == 200) {
// Check that the files directory is writable.
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
return file_save_data($result->data, $directory . '/' . basename($location), FILE_EXISTS_REPLACE);
}
}
}
else {
return $file_destination;
}
}
/**
* Adds the hidden css file.
*/
function _google_webfont_loader_api_add_hidden_css() {
drupal_add_css(drupal_get_path('module', 'google_webfont_loader_api') . '/google_webfont_loader_api.hidden.css');
}
Functions
Name | Description |
---|---|
google_webfont_loader_api_cron | Implements hook_cron(). |
google_webfont_loader_api_get_font_list | Retrieves the list of available fonts. |
google_webfont_loader_api_init | Implements hook_init(). |
google_webfont_loader_api_menu | Implements hook_menu(). |
google_webfont_loader_api_perm | Implements hook_perm(). |
_google_webfont_loader_api_add_hidden_css | Adds the hidden css file. |
_google_webfont_loader_api_cache | Downloads and caches the webfont.js file locally. |
_google_webfont_loader_api_load_js_from_fontinfo | Parses a fontinfo description and create a javascript settings loader |