4

For some extensions, we need to have a different behaviour for different civicrm versions.

I can either manage two branches, but I'll have to backport some fixes and that seems a big overhead. For most of our extensions, I'm tempted for a lighter approach with a single code base

3 Answers 3

1

We have used the domain in some extensions, like so:

$civiVersion = CRM_Core_BAO_Domain::version();

if (version_compare($civiVersion, '4.5', '<')) {
  //in CiviCRM 4.4 the invoke function has 5 arguments maximum
  return $hook->invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $fnSuffix);
} else {
  //in CiviCRM 4.5 and later the invoke function has 6 arguments
  return $hook->invoke($numParams, $arg1, $arg2, $arg3, $arg4, $arg5, CRM_Utils_Hook::$_nullObject, $fnSuffix);
}
0

So what about keeps the same code but adds a few extra tests to handle the differences more easily:

  • if it's a new feature we need, test if it exists (eg if function_exists) and include a local copy or workaround of that function if it doesn't (for older versions)
  • call the new api and check the is_error return, and add a new api/v3 folder in the include path that contains a copy of the missing function and call the api again

  • if it's a missing library (eg. angular or underscore), it's a bit trickier as when you realise that the library is missing, it might be too late as the page is already built and sent to the browser.

For that, I'd like to test what civicrm version we have, is

civicrm_api3("domain",get",array("sequential"=>1))[values][0][version] the best way to do that?

Can I use the helper functions to compare versions safely with that?

1
  • Not sure it's best, but in provision_civicrm, CRM_Utils_System::version() is used to get the version
    – samuelsov
    Commented Jan 14, 2016 at 15:52
0

at least from 4.4 you can use

$result = civicrm_api3('System', 'get', array(
  'sequential' => 1,
));

which contains the CiviCRM version in the result.

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.