1

I am developing a module in Drupal 7.56 that is going to rely on jQuery UI 1.12.1. I want my module to check to see if that version is installed on the site, if it is not I am going to use drupal_add_js() to add the correct version. I don't want to just add the js file if it already exists in the system.

I do not have access to the site the module is going to be installed on. I want this module to be used on many different sites and don't want the module to add the js if it is already there.

What is the best way to do this?

2
  • 1
    drupal_add_js doesn't exist in Drupal 8
    – Kevin
    Commented Feb 13, 2018 at 14:20
  • Ok... the question was originally tagged 8.
    – Kevin
    Commented Feb 14, 2018 at 14:24

1 Answer 1

1

If you call drupal_add_js() without parameters it will return an array of all the JavaScript already installed. You can iterate over the elements to look for the jQuery UI version you need. For example

  $installed_js = drupal_add_js();
  foreach ($installed_js as $file => $details) {
    if (preg_match('/jquery\.ui/', $file)) {
      $version = $details['version'];
      break;
    }
  }
  list($major, $minor, $update) = explode('.', $version);
  if ($major != '1' && $minor != '12' && $update != '1') {
    // Add drupal_add_js call here
  }

You might need a more robust regex to check for a wider variety of possible filenames but the above code should work in principle. I should also point out, it's not totally robust - if for some bizarre reason the file was named blah.js, this approach would not find it, so your module's jQuery code should also check to make sure the right version is loaded.

Your Answer

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

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