2

Is there a good way of checking for if jQuery UI is being included on the current page request?

Found drupal_get_library(), and drupal_add_library(), but neither of them allows me to check if it is being included.

I am trying to do this from the theme, and then load a jquery.ui.touch-punch library to enable iPad/iPhone support.

1 Answer 1

2

There's no built in way but as drupal_add_library() uses Drupal's static pattern you can find out by doing this:

function ui_is_included() {
  // Get the list of libraries included in this page load
  $libs = drupal_static('drupal_add_library');

  // The system library will pretty much always exist as it includes jQuery,
  // but it's just good sense to check just in case.
  if (isset($libs['system'])) {
    // If the system library array includes UI then make sure it's enbabled
    return isset($libs['system']['ui']) && $libs['system']['ui'] === TRUE;
  }

  // UI is not loaded if we've got to this point
  return FALSE;
}

You can even compress that down a bit:

function ui_is_included() {
  $libs = drupal_static('drupal_add_library');
  return isset($libs['system']['ui']) && $libs['system']['ui'];
}

I'd advise calling this function as late on in the page build process as possible, to ensure that all modules have had a chance to request that UI is added. hook_preprocess_html() in your theme's template.php file might be a good place to put it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.