You are here

function modernizr_generate_url in Modernizr 7.3

Same name and namespace in other branches
  1. 8 modernizr.admin.inc \modernizr_generate_url()

Generates new Modernizr URL for admin interface Callback for 'admin/config/development/modernizr'.

1 string reference to 'modernizr_generate_url'
modernizr_menu in ./modernizr.module
Implements hook_menu().

File

./modernizr.admin.inc, line 95
Admin include for Modernizr module.

Code

function modernizr_generate_url() {

  // Reset our lists of needed Modernizr tests in drupal_static.
  drupal_static_reset('modernizr_api_list');

  // Get latest tests from modules and themes
  $tests = modernizr_api_list();

  // Begin assembling link to re-download Modernizr
  $link = 'https://modernizr.com/download/?';

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

    // We can't find a custom build of modernizr.
    // Decide how severe this warning is based on admin settings.
    $modernizr_severity = variable_get('modernizr_quiet', MODERNIZR_QUIET_DEFAULT) ? 'warning' : 'error';

    // Set error/warning message.
    drupal_set_message(t("You don't seem to have a custom build of Modernizr installed yet, or we are detecting the wrong version. Remove any existing modernizr-latest.js or modernizr custom builds from !path and replace it with a new copy. This page will help generate one for you.", array(
      '!path' => module_exists('libraries') ? libraries_get_path('modernizr') : 'sites/all/libraries/modernizr',
    )), $modernizr_severity);
  }
  else {

    // Generate the list of tests that are being requested by the Test API, but
    // are NOT in the Modernizr JS file.
    $missing_tests = array_diff_key($tests, array_flip($current_tests));
    foreach ($current_tests as $current_test) {

      // Adds all current tests to the array of tests.
      if (!isset($tests[$current_test])) {
        $tests[$current_test] = array(
          'name' => $current_test,
          'source' => array(
            modernizr_get_filename(),
          ),
          'desc' => _modernizr_get_desc($current_test),
          'docs' => '',
          'caniuse' => '',
        );
      }
    }
  }

  // Create indexes for first download link/desc to place them
  // above everything. They will be populated later.
  $elements['download_modernizr1']['link'] = array();
  $elements['download_modernizr1']['description'] = array();

  // Give a hint about which file is currently in use and in which directories
  // we have searched for the file.
  $file_path = modernizr_get_path();
  $file_markup = $file_path ? l($file_path, $file_path) : t('No file found');
  $elements['modernizr_file']['title'] = array(
    '#type' => 'html_tag',
    '#value' => t('Currently loaded file'),
    '#tag' => 'h2',
  );
  $elements['modernizr_file']['file'] = array(
    '#type' => 'markup',
    '#markup' => $file_markup,
  );
  if (!$file_path) {
    $elements['modernizr_file']['paths'] = array(
      '#theme' => 'item_list',
      '#title' => t('Scanned directories'),
      '#items' => _modernizr_get_paths(),
    );
  }

  // Create the tests heading render element.
  $elements['tests']['heading-tests'] = array(
    '#theme' => 'html_tag',
    '#value' => t('Current Modernizr Tests'),
    '#tag' => 'h2',
  );

  // Create the tests description render element.
  $elements['tests']['description'] = array(
    '#theme' => 'html_tag',
    '#value' => t('Currently enabled Drupal modules and themes have requested the following Modernizr tests:'),
    '#tag' => 'p',
  );

  // Create a container to indent everything
  $elements['tests']['list'] = array(
    '#prefix' => '<div class="modernizr-tests">',
    '#suffix' => '</div>',
  );

  // Check to see if there are any registered tests.
  if (!empty($tests)) {

    // Loop through each registered test.
    foreach ($tests as $key => $test) {

      // API was changed between 7.x-3.0 and 7.x-3.1. We originally specified
      // `module` but since themes can also specify tests the attribute was
      // changed to `source`.
      $source = isset($test['source']) ? $test['source'] : $test['module'];

      // Check to see if this test has been set. If not, add it.
      if (!isset($elements['tests']['list'][$key])) {

        // Check if this test is missing, and apply a special class only when it
        // is missing from the current build of Modernizr JS file.
        $not_included = isset($missing_tests[$key]) ? 'not-included' : '';

        // Build the element for this Modernizr test.
        $elements['tests']['list'][$key]['name'] = array(
          '#theme' => 'html_tag',
          '#value' => $key,
          '#tag' => 'h3',
          '#attributes' => array(
            'class' => $not_included,
          ),
        );
      }

      // Create the description for this test.
      $elements['tests']['list'][$key]['tests'][] = array(
        'description' => array(
          '#markup' => _modernizr_get_desc($key),
          '#prefix' => '<p class="desc">',
          '#suffix' => '</p>',
        ),
      );

      // @TODO: Check to see if this test has already been added by another module.
      $link .= $key . '-';
    }
  }
  else {
    $elements['tests']['list']['#markup'] = '<p>There are no Modernizr tests registered</p>';
  }

  // Create the download link render element.
  $download_link = array(
    '#prefix' => '<div class="download-modernizr">',
    '#theme' => 'link',
    '#text' => t('Download your Modernizr production build'),
    '#path' => substr($link, 0, -1),
    // Truncate the last hyphen.
    '#options' => array(
      'attributes' => array(
        'class' => array(
          'button',
        ),
        'target' => '_blank',
      ),
      'html' => FALSE,
    ),
    '#suffix' => '</div>',
  );

  // Create the download description render element.
  $download_desc = array(
    '#theme' => 'html_tag',
    '#value' => t('The button links to a custom Modernizr build based on the tests listed above. <br/> Once you download the script, place it inside <b>!path</b> and !cc.', array(
      '!path' => module_exists('libraries') ? libraries_get_path('modernizr') : 'sites/all/libraries/modernizr',
      '!cc' => l(t('clear your cache'), 'admin/config/development/performance'),
    )),
    '#tag' => 'p',
  );

  // Print the Download link above and below the tests.
  $elements['download_modernizr1']['link'] = $download_link;
  $elements['download_modernizr2']['link'] = $download_link;

  // Print the Download description above and below the tests.
  $elements['download_modernizr1']['description'] = $download_desc;
  $elements['download_modernizr2']['description'] = $download_desc;

  // Load admin CSS
  drupal_add_css(drupal_get_path('module', 'modernizr') . '/css/modernizr.admin.css');
  return $elements;
}