You are here

function modernizr_requirements in Modernizr 7.3

Same name and namespace in other branches
  1. 8 modernizr.install \modernizr_requirements()
  2. 6.2 modernizr.install \modernizr_requirements()
  3. 6 modernizr.install \modernizr_requirements()
  4. 7 modernizr.install \modernizr_requirements()
  5. 7.2 modernizr.install \modernizr_requirements()

Implements hook_requirements().

Changes its status based on ability to locate JS library. Changes its instructions based on Libraries API being enabled.

File

./modernizr.install, line 14
Install file for Modernizr module.

Code

function modernizr_requirements($phase) {
  $requirements = array();
  switch ($phase) {
    case 'runtime':

      /*
       * Test for conditions
       */

      // Fetch the version and force it to skip cache.
      $version = modernizr_get_version(TRUE);

      // Fetch the path to the JS lib.
      $path = modernizr_get_path();

      // Test if Libraries module is being used by comparing output of path functions
      if (module_exists('libraries')) {

        // If this is truthy, the Modernizr is using Libraries API as best we can tell.
        $using_libraries = strpos($path, libraries_get_path('modernizr')) !== FALSE;
      }
      else {
        $using_libraries = FALSE;
      }

      /*
       * Generate status message and severity
       */

      // Modernizr / Libraries API installed and working correctly.
      // Do the Drupal happy dance!
      if ($path && $using_libraries) {
        $description = FALSE;
        $severity = REQUIREMENT_OK;
      }
      elseif ($path && !$using_libraries) {
        $description = t('Modernizr JS library is installed but you aren\'t using !libraries-api. You should use it.', array(
          '!libraries-api' => l(t('Libraries API'), 'http://drupal.org/project/libraries'),
        ));
        $severity = REQUIREMENT_WARNING;
      }
      elseif (!$path && module_exists('libraries')) {
        $description = t('Modernizr JS library cannot be found. Download it from !modernizr-site, copy it into !path and rename it to modernizr.min.js.', array(
          '!modernizr-site' => l(t('modernizr.com'), 'http://modernizr.com/download/'),
          // !path has a hardcoded default because the libraries_get_path() function might not return
          // the correct path when conditions lead to this block of code being executed
          '!path' => libraries_get_path('modernizr') ? libraries_get_path('modernizr') : 'sites/all/libraries/modernizr',
        ));
        $severity = REQUIREMENT_ERROR;
      }
      else {
        $description = t('Modernizr and Libraries API cannot be found. Download Modernizr from !modernizr-site, copy it into !path and rename it to modernizr.min.js. You should also use the !libraries-api by installing from drupal.org.', array(
          '!modernizr-site' => l(t('modernizr.com'), 'http://modernizr.com/download/'),
          '!path' => 'sites/all/libraries/modernizr',
          '!libraries-api' => l(t('Libraries API'), 'http://drupal.org/project/libraries'),
        ));
        $severity = REQUIREMENT_ERROR;
      }

      /**
       * We need a secondary set of requirements in case all modernizr tests
       * requested are not added to the current modernizr build. This will only
       * run if modernizr is available.
       */
      if ($path) {
        $missing_tests = _modernizr_info_missing_tests();
        if (empty($missing_tests)) {

          // There are no missing tests! We are awesome!
          $tests_value = t('All required tests are present in current Modernizr build.');
          $tests_description = FALSE;
          $tests_severity = REQUIREMENT_OK;
        }
        else {

          // Pull tests that are currently set.
          $current_tests = _modernizr_current_build();

          // If the custom build hasn't been created yet, we should report that
          // instead of saying that they're missing altogether. The development
          // copy has all the tests, so none are missing. However, dev does NOT
          // have Modernizr.load(), so it still registers as a full-blown error
          // by default.
          if (is_null($current_tests)) {
            $tests_value = t('You haven\'t created a custom build yet.');
            $tests_description = t('Modernizr only works with a custom build. Visit the !modernizr-settings to create one.', array(
              '!modernizr-settings' => l(t('Modernizr settings page'), 'admin/config/development/modernizr'),
            ));
            $tests_severity = variable_get('modernizr_quiet', MODERNIZR_QUIET_DEFAULT) ? REQUIREMENT_WARNING : REQUIREMENT_ERROR;
          }
          else {

            // Custom build exists, and tests are missing, we need to fix that.
            $tests_value = t('Tests are missing in current Modernizr build.');
            $tests_description = t('Certain tests requested by currently enabled modules and themes are not within the current Modernizr build. Go to the !link to download a new version of Modernizr. The tests that are missing are: ', array(
              '!link' => l(t('Modernizr settings page'), 'admin/config/development/modernizr'),
            )) . '<code>' . implode('</code>, <code>', array_keys($missing_tests)) . '</code>';
            $tests_severity = variable_get('modernizr_quiet', MODERNIZR_QUIET_DEFAULT) ? REQUIREMENT_WARNING : REQUIREMENT_ERROR;
          }
        }

        /**
         * Declare requirement to Drupal
         */
        $requirements[] = array(
          'title' => t('Modernizr Tests'),
          'value' => $tests_value,
          'description' => $tests_description,
          'severity' => $tests_severity,
        );
      }

      /*
       * Declare requirement to Drupal
       */
      $requirements[] = array(
        'title' => t('Modernizr'),
        'value' => $version ? $version : t('Not installed'),
        'description' => $description,
        'severity' => $severity,
      );
      break;
  }
  return $requirements;
}