Skip to content

Instantly share code, notes, and snippets.

@pixelwhip
Last active August 29, 2015 14:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelwhip/dde2d6b675b613530899 to your computer and use it in GitHub Desktop.
Save pixelwhip/dde2d6b675b613530899 to your computer and use it in GitHub Desktop.
Grunticon snippet for a Drupal theme.
<?php
/**
* Implements hook_preprocess_html().
*/
function [theme_name]_preprocess_html(&$vars) {
/**
* Add Grunticon stylesheet loader.
*/
/**
* Array of stylesheets.
* This is the default, if you set custom stylesheet names in
* Gruntfile.js, update these to reflect those changes.
*/
$grunticon_stylesheets = array(
'svg' => 'icons.data.svg.css',
'png' => 'icons.data.png.css',
'fallback' => 'icons.fallback.css',
);
/**
* Add the absolute path to your stylesheets.
* This will depend on your Grunticon task's 'dest' option.
* Update this path accordingly.
*/
array_walk($grunticon_stylesheets, function (&$value, $key) {
$value = '"' . $GLOBALS['base_url'] . '/' . $GLOBALS['theme_path'] . '/img/grunticon/' . $value . '"';
}
);
/* grunticon Stylesheet Loader | https://github.com/filamentgroup/grunticon | (c) 2012 Scott Jehl, Filament Group, Inc. | MIT license. */
$grunticon_snippet = 'window.grunticon=function(e){if(e&&3===e.length){var t=window,n=!(!t.document.createElementNS||!t.document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||window.opera&&-1===navigator.userAgent.indexOf("Chrome")),o=function(o){var r=t.document.createElement("link"),a=t.document.getElementsByTagName("script")[0];r.rel="stylesheet",r.href=e[o&&n?0:o?1:2],a.parentNode.insertBefore(r,a)},r=new t.Image;r.onerror=function(){o(!1)},r.onload=function(){o(1===r.width&&1===r.height)},r.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="}};';
$grunticon_snippet .= 'grunticon([' . implode(', ', $grunticon_stylesheets) . ']);';
/**
* Inject the snippet.
*/
drupal_add_js(
$grunticon_snippet,
array(
'type' => 'inline',
'scope' => 'footer',
)
);
/**
* Inject the noscript fallback snippet.
*/
$grunticon_fallback = array(
'#type' => 'markup',
'#markup' => '<noscript><link href=' . $grunticon_stylesheets["fallback"] . ' rel="stylesheet"></noscript>',
);
drupal_add_html_head($grunticon_fallback, 'grunticon_fallback');
}
@jfitzsimmons2
Copy link

Works like a charm, thank you for sharing!

@rmcveigh
Copy link

This is awesome! Thanks John!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment