How to Define a Minimum Drupal Core Version
3rd April 2015
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
This week, my first code patch was committed to Drupal core. The patch adds the
user_has_role() function to the user module, to simplify the way to check whether a user in Drupal has been assigned a specific role. This is something that I normally write a custom function for each project, but it's now available in Drupal core as of 7.36.
But what if someone is using a core version less than 7.36 and tries using the function? The site would return an error because that function wouldn't exist.
If you're building a new Drupal site, then I'd assume that you're using a latest version of core, or you have the opportunity to update it when needed. But what if you're writing a contrib module? How can you be sure that the correct minimum version of core?
What I'm going to be doing for my contrib projects is defining a minimum version of Drupal core that the module is compatible with. If this dependency isn't met, the module won't be able to be enabled. This is done within your module's .info file.
Adding a Simple Dependency
You can define a simple dependency for your module by adding a line this this to your project's .info file:
dependencies = views
This would make your module dependant on having the Views module present and enabled, which you'd need if you were including views as part of your module, for example.
Adding a Complex Dependency
In the previous example, our module would enable if any version of Views was enabled, but we need to specify a specific version. We can do this by including version numbers within the dependencies field in the following format:
dependencies = modulename (major.minor)
This can be a for a specific module release or a branch name:
dependencies = modulename (1.0) dependencies = modulename (1.x)
We can also use the following as part of the field for extra granularity:
- = or == equals (this is the default)
- > greater than
- < lesser than
- >= greater than or equal to
- <= lesser than or equal to
- != not equal to
In the original scenario, we want to specify that the module can only be enabled on Drupal core 7.36 or later. To do this, we can use the "greater than or equal to" option.
dependencies = system (>=7.36)
Because we need to check for Drupal's core version, we're using the system module as the dependency and specifying that it needs to be either equal to or greater than 7.36. If this dependency is not met, e.g. Drupal 7.35 is being used, then the module cannot be enabled rather than showing a function not found error for
user_has_role() when it is called.
Questions? Comments? I’m @opdavies on Twitter.
About the Author
Oliver Davies is a full-stack Web Developer and System Administrator based in the UK. He is a Senior Developer at Microserve and a part-time freelancer specialising in Drupal, Symfony and Laravel development and Linux systems administration.