Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The Modernizr module needs a way to afford other modules the chance to load assets if they depend on a Modernizr test via hook_modernizr_info()
. For example, a the Geolocation Field might want to load a JavaScript shim if native support is lacking:
// Imaginary Geolocation example
function geolocation_modernizr_load() {
// For the following key, the first 'geolocation' is the Drupal module
// used as a namespace. The second 'geolocation' is the Modernizr test.
$load['geolocation_geolocation'] = array(
'test' => 'Modernizr.geolocation', // This is the Modernizr test
'yep' => '', // If the test passed, the browser has built-in support for what we need.
'nope' => '/'. drupal_get_path('module', 'geolocation') . '/js/geolocation_shim.js', //Missing Geolocation; load polyfill
'both' => '/'. drupal_get_path('module', 'geolocation') . '/js/geolocation_main.js', // Always load module functionality
'callback' => '', // Execute stuff after each resource is downloaded. Takes an anonymous function.
'complete' => '', // A final callback after all resources are fetched. Takes an anonymous function.
);
return $load;
}
// Another example for a theme
function mytheme_modernizr_load() {
$load['mytheme_borderradius'] = array(
'test' => 'Modernizr.borderradius', // Modernizr test for border-radius
'nope' => '/'. drupal_get_path('theme', 'mytheme') . '/js/css3pie.js', // Missing border-radius support; load polyfill
);
return $load;
}
Which would output something like:
Modernizr.load([{
test: Modernizr.geolocation,
nope: '/sites/all/modules/contrib/geolocation/js/geolocation_shim.js',
both: '/sites/all/modules/contrib/geolocation/js/geolocation_main.js',
},{
test: Modernizr.borderradius,
nope: '/sites/all/themes/mytheme/js/css3pie.js',
}]);
Read yepnope.js documentation to find out more about each parameter. This example is based on yepnope 1.5. All parameters are optional.
Comment | File | Size | Author |
---|---|---|---|
#3 | 1661746-3-modernizr-load.patch | 10.38 KB | rupl |
#1 | 1661746-1-modernizr-load.patch | 8.58 KB | rupl |
Comments
Comment #1
ruplThis patch adds
hook_modernizr_load()
and combines its output with the original.info
file M.load() code that already exists in -dev. For now, I decided to load the results ofhook_modernizr_load()
after the entries in the.info
files since themes are more visual and in most cases should be loaded sooner. Modules can take advantage ofhook_modernizr_load_alter()
if they absolutely must be loaded before the themes.Would love to get some feedback!
Comment #2
tim.plunkettStill don't get it :\
Why not put this in modernizr.api.php?
Comment #2.0
ruplUpdated issue summary. Made code samples more accurate according to patch in #1.
Comment #2.1
ruplUpdated issue summary, this time just for formatting.
Comment #3
ruplMe neither. I took this from some example. Removed.
That's a great idea. I expanded the example a bit too. I also added support for
load
this time, which is just syntactic sugar forboth
. I'd like to match the upstream library's settings as closely as possible.Comment #4
ruplAdded support for multiple actions/callbacks and committed to dev. Wahoo! Follow-up issues welcome.
Comment #5.0
(not verified) CreditAttribution: commentedArgh missed one filename.