You are here

function _modernizr_load_generate in Modernizr 8

Same name and namespace in other branches
  1. 7.3 modernizr.module \_modernizr_load_generate()

Helper function to render the Modernizr.load() calls.

1 call to _modernizr_load_generate()
modernizr_page_build in ./modernizr.module
Implements hook_page_build().

File

./modernizr.module, line 317
Main module file for Modernizr

Code

function _modernizr_load_generate() {
  $output = FALSE;

  // Get Modernizr.load() calls from the active theme.
  $theme = modernizr_load_data();

  // Collect data from modules that implement hook_modernizr_load().
  $modules = modernizr_load_list();

  // Combine the data from the .info file and the Drupal modules.
  // Themes go first because they are more visual and in most cases
  // it's probably best to load them first. Modules whose assets
  // truly need to be loaded first have hook_modernizr_load_alter()
  // at their disposal.
  $testObjects = array_merge($theme, $modules);

  // Build the Modernizr.load() commands.
  if (count($testObjects)) {
    $num_tests = 1;
    $output .= 'Modernizr.load([';
    foreach ($testObjects as $load) {
      $output .= $num_tests > 1 ? ',' : '';
      $output .= '{' . "\n";
      $output .= '  test: ' . $load['test'] . ',' . "\n";

      // Print each action and its resources
      $actions = array(
        'yep',
        'nope',
        'both',
        'load',
      );
      foreach ($actions as $action) {
        if (isset($load[$action])) {

          // Begin output for this action
          $output .= '  ' . sprintf('%-4s', $action) . ': ';

          // How many resources for this action?
          if (count($load[$action]) == 1) {

            // Single resource
            $output .= "'" . $load[$action][0] . "',\n";
          }
          else {

            // Multiple resources
            $output .= '[';
            foreach ($load[$action] as $resource) {
              $output .= "'" . $resource . "',";
            }

            // Truncate last comma
            $output = substr($output, 0, -1);
            $output .= "],\n";
          }
        }
      }

      // Output these two properties without quotes around the output
      $callbacks = array(
        'callback',
        'complete',
      );
      foreach ($callbacks as $action) {
        if (isset($load[$action])) {

          // Begin output for this action
          $output .= '  ' . sprintf('%-4s', $action) . ': ';

          // How many callbacks for this action?
          if (count($load[$action]) == 1) {

            // Single resource
            $output .= $load[$action][0] . ",\n";
          }
          else {

            // Multiple resources
            $output .= '[';
            foreach ($load[$action] as $callback) {
              $output .= $callback . ",";
            }

            // Truncate last comma
            $output = substr($output, 0, -1);
            $output .= "],\n";
          }
        }
      }

      // Truncate last comma and newline
      $output = substr($output, 0, -2);
      $output .= "\n}";
      $num_tests++;
    }

    // If more than one test was registered, finish the Array notation.
    // Finally, close the Modernizr.load() function parenthesis.
    $output .= $num_tests > 1 ? ']' : '';
    $output .= ');';
  }
  return $output;
}