0

There's a place in the search_krumo module where we want to supply a different JavaScript file path basing on the version of the Devel module being used.

Is there a Drupal function that will check the version for us, or do we need to get the module version with system_get_info(), and then do something with $module['version'] (which is a string like 7.x-1.5) ourselves?

If the latter, how should we test if version is greater than 1.6? Should we convert the string to an integer, strip out the x and stuff, and compare integers?

0

Supposing that the second version string is also in the 7.x-y.z format, where y and z are numbers, you could use code similar to the following one.

function compare_module_version($module, $version) {
  $info = system_get_info('module', $module);

  if (empty($info) || empty($info['version'])) {
    // The module doesn't exist, it's not enabled,
    // or there isn't any information about the module version.
    return FALSE;
  }

  return version_compare($info['version'], $version);
}

If $version uses the y.z format (where y and z are numbers), you can use the following code.

function compare_module_version($module, $version) {
  $info = system_get_info('module', $module);

  if (empty($info) || empty($info['version'])) {
    // The module doesn't exist, it's not enabled,
    // or there isn't any information about the module version.
    return FALSE;
  }

  return version_compare(substr($info['version'], 4), $version);
}

If then both the version strings are in the 7.x-y.z format, and you want to compare the version string without considering the Drupal compatibility part of the version, you could use the following code.

function compare_module_version($module, $version) {
  $info = system_get_info('module', $module);

  if (empty($info) || empty($info['version'])) {
    // The module doesn't exist, it's not enabled,
    // or there isn't any information about the module version.
    return FALSE;
  }

  return version_compare(substr($info['version'], 4), substr($version, 4));
}

version_compare() is thought to compare version number strings, and it is able to correctly handle a version like '7.x-1.0-alpha1', which is considered to be lower than '7.x-1.0-beta1'.

compare_module_version() returns FALSE if the module is not enabled (which also includes the case the module is not even installed), or when the information about the module version has not been found. Otherwise, it returns -1 if the module version is lower than the second function argument, 0 if they are equal, and 1 if the second function argument is lower.

I would not use drupal_parse_info_file() since that function re-parses the module .info file when the information contained in that file (or any .info file for enabled modules) has been already gathered from Drupal.

0
  1. You can use system_get_info('module', 'devel') function and split version to get number after 7.x-

  2. Or goes with this aproach parsing info file here is a solution from stackoverflow get a module version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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