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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rupl’s picture

Status: Active » Needs review
FileSize
8.58 KB

This 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 of hook_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 of hook_modernizr_load_alter() if they absolutely must be loaded before the themes.

Would love to get some feedback!

tim.plunkett’s picture

+++ b/modernizr.moduleundefined
@@ -346,3 +384,50 @@ function modernizr_modernizr_info() {
+  // See module_implements() for an explanation of this cast.

Still don't get it :\

+++ b/modernizr.moduleundefined
@@ -346,3 +384,50 @@ function modernizr_modernizr_info() {
+function __modernizr_modernizr_load() {

Why not put this in modernizr.api.php?

rupl’s picture

Issue summary: View changes

Updated issue summary. Made code samples more accurate according to patch in #1.

rupl’s picture

Issue summary: View changes

Updated issue summary, this time just for formatting.

rupl’s picture

Still don't get it :\

Me neither. I took this from some example. Removed.

Why not put this in modernizr.api.php?

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 for both. I'd like to match the upstream library's settings as closely as possible.

rupl’s picture

Component: User interface » Code
Status: Needs review » Fixed

Added support for multiple actions/callbacks and committed to dev. Wahoo! Follow-up issues welcome.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Argh missed one filename.