icomoon.module in Icomoon 7
icomoon.module Integrates the Icomoon service as an icon provider for Icon API.
File
icomoon.moduleView source
<?php
/**
* @file
* icomoon.module
* Integrates the Icomoon service as an icon provider for Icon API.
*/
/**
* Provide default bundle settings.
*/
function icomoon_default_settings() {
return array(
// @todo set this back to 1 once core can handle JS browser conditions in
// drupal_add_js().
// @see https://drupal.org/node/865536
'ie7' => 0,
'tag' => 'span',
);
}
/**
* Implements hook_icon_providers().
*/
function icomoon_icon_providers() {
$providers['icomoon'] = array(
'title' => t('Icomoon'),
'url' => 'http://icomoon.io/app',
'default bundle' => array(
'render' => 'sprite',
'settings' => icomoon_default_settings(),
),
);
return $providers;
}
/**
* Retrieve JSON data about a bundle.
*
* @todo Move this into Icon API as a generic helper function for retrieving
* and parsing a bundle's "JSON configuration file".
*
* @param string $path
* The bundle path.
*
* @return array|FALSE
* The JSON array or FALSE if not found.
*/
function icomoon_get_json($path) {
if (file_exists($path . '/selection.json')) {
$json = drupal_json_decode(file_get_contents($path . '/selection.json'));
if (!empty($json)) {
return $json;
}
}
return FALSE;
}
/**
* Implements hook_icon_PROVIDER_import_validate().
*/
function icomoon_icon_icomoon_import_validate(&$bundle) {
if ($json = icomoon_get_json($bundle['path'])) {
return TRUE;
}
return t('The uploaded archive file does not contain the %json file. Ensure that this is a proper archive file generated by <a href="!url">@provider</a>.', array(
'%json' => 'selection.json',
'@provider' => t('IcoMoon'),
'!url' => url('http://icomoon.io'),
));
}
/**
* Implements hook_icon_PROVIDER_import_process().
*/
function icomoon_icon_icomoon_import_process(&$bundle) {
if ($json = icomoon_get_json($bundle['path'])) {
if (!empty($json['preferences']['fontPref']['ie7'])) {
drupal_set_message(t('Notice: IE7 support was detected in your bundle and has been temporarily disabled. Please read the description below for further instructions.'), 'warning', FALSE);
}
// @todo remove this override once core can handle browser conditions in
// drupal_add_js().
// @see https://drupal.org/node/865536
$json['preferences']['fontPref']['ie7'] = FALSE;
// Merge in settings from archive.
$bundle['settings'] = icon_array_merge_recursive($bundle['settings'], $json['preferences']['fontPref']);
if (!empty($json['icons'])) {
foreach ($json['icons'] as $icon) {
$bundle['icons'][$icon['properties']['name']] = $icon['properties']['name'];
}
// Add processing callback.
$bundle['#attached']['icomoon_process_attached'] = array(
array(
$bundle['name'],
),
);
}
}
}
/**
* Implements hook_preprocess_icon().
*/
function icomoon_preprocess_icon(&$variables) {
if (!empty($variables['bundle']['provider']) && $variables['bundle']['provider'] === 'icomoon') {
$icon =& $variables['icon'];
$classes =& $variables['attributes']['class'];
$settings = $variables['bundle']['settings'];
// IcoMoon defaults to a 'icon-' prefix if not overridden.
if (empty($settings['prefix']) && empty($settings['useClassSelector']) && $settings['classSelector'] === '.icon') {
$settings['prefix'] = 'icon-';
}
// Alter the class according to any prefix and postfix.
if (!empty($settings['prefix'])) {
$icon = $settings['prefix'] . $icon;
}
if (!empty($settings['postfix'])) {
$icon .= $settings['postfix'];
}
// Replace the default "icon" class if a different one is provided.
if (!empty($settings['useClassSelector']) && !empty($settings['classSelector']) && $settings['classSelector'] !== '.icon') {
$key = array_search('icon', $classes);
if ($key !== FALSE) {
$classes[$key] = str_replace('.', '', $settings['classSelector']);
}
}
// Add an identifier class for this bundle.
$classes[] = 'icomoon';
}
}
/**
* Attach callback for an IcoMoon bundle.
*/
function icomoon_process_attached($bundle_name) {
$bundle = icon_bundle_load($bundle_name);
if (!$bundle) {
return;
}
$css = array();
$js = array();
// Apply default settings, in case bundle lives in code.
$bundle['settings'] += icomoon_default_settings();
$css[$bundle['path'] . '/style.css'] = array(
'basename' => 'icomoon.' . $bundle_name . '.style.css',
);
// IE7 support.
if (!empty($bundle['settings']['ie7'])) {
$css[$bundle['path'] . '/ie7/ie7.css'] = array(
'basename' => 'icomoon.' . $bundle_name . '.ie7.css',
'browsers' => array(
'IE' => 'lt IE 8',
'!IE' => FALSE,
),
);
$js[$bundle['path'] . '/ie7/ie7.js'] = array(
'scope' => 'footer',
'browsers' => array(
'IE' => 'lt IE 8',
'!IE' => FALSE,
),
);
}
foreach ($css as $data => $options) {
drupal_add_css($data, $options);
}
foreach ($js as $data => $options) {
drupal_add_js($data, $options);
}
}
/**
* Implements hook_icon_bundle_configure().
*/
function icomoon_icon_bundle_configure(&$settings, &$form_state, &$complete_form) {
$bundle = $form_state['bundle'];
if ($bundle['provider'] === 'icomoon') {
// @todo remove the description once core can handle browser conditions in
// drupal_add_js().
// @see https://drupal.org/node/865536
$settings['ie7'] = array(
'#type' => 'checkbox',
'#title' => t('Enable IE7 Support'),
'#description' => t('<strong>WARNING:</strong> Drupal core does not detect browser conditional comments in drupal_add_js(). Enabling this setting might result in duplicated icons.<br />It is strongly recommended that this setting remain disabled until the latest patch in <a href="!link">!link</a> is committed to Drupal.<br />If you decide to apply the patch manually and enable this setting, do so at your own risk.', array(
'!link' => url('https://drupal.org/node/865536'),
)),
'#default_value' => $bundle['settings']['ie7'],
);
$settings['tag'] = array(
'#type' => 'select',
'#title' => t('HTML Markup'),
'#description' => t('Choose the HTML markup tag that icons should be created with. Typically, this is an %tag tag, however it can be changed to suite the theme requirements.', array(
'%tag' => '<span/>',
)),
'#options' => drupal_map_assoc(array(
'span',
'div',
'i',
)),
'#default_value' => $bundle['settings']['tag'],
);
}
}
/**
* Callback for downloading bundle's JSON configuration file.
*
* @todo Move this into Icon API as a generic callback for a bundle's "JSON
* configuration file".
*
* @param string $bundle
* An associative array containing the bundle settings.
*/
function icomoon_download_config_callback($bundle) {
if ($bundle) {
global $user;
$file = new stdClass();
$file->fid = NULL;
$file->uri = $bundle['path'] . '/selection.json';
$file->filename = drupal_basename($bundle['path']) . '-' . drupal_basename($file->uri);
$file->filemime = file_get_mimetype($file->uri);
$file->filesize = filesize($file->uri);
$file->uid = $user->uid;
$file->status = FILE_STATUS_PERMANENT;
file_transfer($file->uri, file_get_content_headers($file));
}
drupal_not_found();
}
Functions
Name![]() |
Description |
---|---|
icomoon_default_settings | Provide default bundle settings. |
icomoon_download_config_callback | Callback for downloading bundle's JSON configuration file. |
icomoon_get_json | Retrieve JSON data about a bundle. |
icomoon_icon_bundle_configure | Implements hook_icon_bundle_configure(). |
icomoon_icon_icomoon_import_process | Implements hook_icon_PROVIDER_import_process(). |
icomoon_icon_icomoon_import_validate | Implements hook_icon_PROVIDER_import_validate(). |
icomoon_icon_providers | Implements hook_icon_providers(). |
icomoon_preprocess_icon | Implements hook_preprocess_icon(). |
icomoon_process_attached | Attach callback for an IcoMoon bundle. |