Automating Drupal module activation
When upgrading a large Drupal site between major revisions, I find myself doing the update over and over again. Since one site I maintain has 149 modules, and a few more that come packaged with other contrib that are not activated, I decided I don't want to click all those checkboxes every time I do a trial upgrade. While I could have probably come up with some way to just activate every module easily, I really wanted selective activation.
Thanks to Greasemonkey, a Firefox addon, I have automated the activation of all the modules I want.
First, make a list of all the modules you want to activate. You can do this by viewing the HTML source of your modules page and looking for the name of your modules in the form input fields named something like status[coder]
. You'll just want the coder
part. I recommend putting each module on its own line so if you change your mind on one module, you can just comment out that line.
var modules = new Array();<br>modules = Array(<br>// REMOVE DEVEL MODULES BEFORE LIVE LAUNCH<br>// Development<br>'coder',<br>'devel',<br>'reroute_email',<br>'cache_disable',<br>'simpletest',<br>// END DEVELOPMENT MODULES<br><br>// Core - optional<br>'help',<br>'menu',<br>'path',<br>'syslog',<br>'tracker',<br>'update',<br>// Other<br>'advanced_help',<br>'cvs_deploy'<br>);
The working part of the Greasemonkey script is a loop that examines the name of all the checkboxes on the page.
var chkboxes=document.getElementsByTagName('input');<br>for (var i=0; i < chkboxes.length; i++) {<br> if (chkboxes[i].type=="checkbox" && in_modules(chkboxes[i].name, modules)) {<br> chkboxes[i].checked=true;<br> }<br>}
Then the part that finds the name of the module is similar to PHP's core in_array() function, with a change to wrap the status[] input array around the string of the module name you're searching for to activate. Remember, the name of each checkbox is actually something like status[coder]
, not just the module name as we have in the modules array at the start of the file.
function in_modules(needle, haystack) {<br> var found = false, key;<br><br> for (key in haystack) {<br> if ('status[' + haystack[key] + ']' == needle) {<br> found = true;<br> break;<br> }<br> }<br> return found;<br>}
The nice part about how this script works is that you can keep the same list of modules as you add modules back to your contrib modules directory during your upgrade. In the case of 149 modules, I have to activate them in groups when I add them back during the update.php process. First I activate and update core optional modules as a group, then cck, views, and workflow as a group, then two groups of the remaining contrib modules. I'm positive that I experience weirdness when activating and updating all 149 modules at the same time, especially with CCK widgets. Since in this process you're only checking the available checkboxes against a list of valid ones to check, there is no error if you have not yet added all the modules in your Greasemonkey array.
Best of all, you can be sure you're checking the correct checkboxes each time. Just be sure to make the included pages URL in your Greasemonkey configuration appropriate to your environment so you don't accidentally activate your development modules on your live site. It's a good idea to keep those development modules in a separate block for easy disabling when going to production.
The attached script should get you started with a long list of popular modules as an example.
File attachments
AttachmentSize example greasemonkey script for checking Drupal modules2.28 KB
Post categories