Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created December 21, 2011 17:09
Show Gist options
  • Save bangpound/1506800 to your computer and use it in GitHub Desktop.
Save bangpound/1506800 to your computer and use it in GitHub Desktop.
Make Drupal 7 aggregation efficient again and use Google CDN for jQuery and jQuery UI
<?php
/**
* Implements hook_library().
*/
function MYMODULE_library() {
// jQuery UI from Google.
$libraries['ui'] = array(
'title' => 'jQuery UI: All',
'website' => 'http://jqueryui.com',
'version' => '1.8.7',
'js' => array(
'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js' => array(
'type' => 'external',
),
),
);
return $libraries;
}
/**
* Implements hook_library_alter().
*/
function MYMODULE_library_alter(&$libraries, $module) {
if ($module == 'system') {
// Replace jQuery with Google CDN.
$url = 'https://ajax.googleapis.com/ajax/libs/jquery/'. $libraries['jquery']['version'] .'/jquery.min.js';
$libraries['jquery']['js'][$url] = $libraries['jquery']['js']['misc/jquery.js'];
$libraries['jquery']['js'][$url]['type'] = 'external';
unset($libraries['jquery']['js']['misc/jquery.js']);
// Rewrite jQuery UI libraries to depend on Google CDN.
foreach ($libraries as $key => &$value) {
if ((strpos($key, 'ui') === 0) || (strpos($key, 'effects') === 0)) {
$value['js'] = array();
}
}
$libraries['ui']['dependencies'][] = array('MYMODULE', 'ui');
$libraries['effects']['dependencies'][] = array('MYMODULE', 'ui');
}
}
/**
* Implements hook_js_alter().
*
* see http://www.metaltoad.com/blog/drupal-7-taking-control-css-and-js-aggregation
*/
function MYMODULE_js_alter(&$javascript) {
uasort($javascript, 'drupal_sort_css_js');
$i = 0;
foreach ($javascript as $name => $script) {
$javascript[$name]['weight'] = $i++;
$javascript[$name]['group'] = JS_DEFAULT;
$javascript[$name]['every_page'] = FALSE;
}
}
/**
* Implements hook_css_alter().
*
* see http://www.metaltoad.com/blog/drupal-7-taking-control-css-and-js-aggregation
*/
function MYMODULE_css_alter(&$css) {
uasort($css, 'drupal_sort_css_js');
$i = 0;
foreach ($css as $name => $style) {
$css[$name]['weight'] = $i++;
$css[$name]['group'] = CSS_DEFAULT;
$css[$name]['every_page'] = FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment