5

I have a small module on drupal.org. Among a few other things, my module adds support for a third-party PHP library to Drupal. Users are expected to download the module to .../libraries/ and the module provides helper functions, etc.

A few days ago I had a feature request come in. Essentially, a user is asking for a way to automagically download the PHP library to .../libraries/ for use by the module.

Before I stamp this request as denied, I was curious whether there is a decent method to do this, and if there are any existing modules that already to this (so I can look at them for inspiration).

2 Answers 2

2

Views Infinite Scroll provides a Drush command to do it, so I'd start there- it sounds like a pretty simple option!

Instead of downloading jquery.autopager manually, drush could be used to download the plugin in /js directory or sites/all/libraries if libraries module is enabled.

Drush command: drush dl-autopager

The Drush include is here, but this is the important bit:

function views_infinite_scroll_drush_autopager_download() {
     if(module_exists('libraries')) {
       $path = 'sites/all/libraries/autopager';

       // Create the path if it does not exist.
       if (!is_dir($path)) {
         drush_op('mkdir', $path);
         drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
       }
     }
     else {
       $path = drupal_get_path('module', 'views_infinite_scroll') . '/js';
     }
     drush_op('chdir', $path);
     // Download the plugin.
     if (drush_shell_exec('wget http://jquery-autopager.googlecode.com/files/jquery.autopager-1.0.0.js')) {
       drush_log(dt('jquery.autopager-1.0.0.js has been downloaded to @path', array('@path' => $path)), 'success');
     }
     else {
       drush_log(dt('Drush was unable to download jquery.autopager-1.0.0.js to @path', array('@path' => $path)), 'error');
     }
   }
4
  • Oooh. That is nifty. Hadn't seen this before.
    – mpdonadio
    Commented Nov 29, 2012 at 12:07
  • @MPD Something like this might help ;-)
    – Chapabu
    Commented Nov 29, 2012 at 14:42
  • Interesting module choice for an example. I'll check it out.
    – mpdonadio
    Commented Nov 29, 2012 at 15:14
  • @MPD I remember you saying it was yours a fews weeks back - thought I'd check :-p
    – Chapabu
    Commented Nov 29, 2012 at 15:16
7

You can use drush make to do this, but that will only work if the person downloading the module is using drush make.

In your modulename.make file:

libraries[foolib][download][type] = "get"
libraries[foolib][download][url] = "http://path.to/file/or/tarball.gz

This is only advisable if you are grabbing libraries. Adding dependent modules here currently can cause version conflicts (although many modules supply this info via a modulename.make.example file).

2
  • Definitely +1, but wouldn't it be even better to depend on a library wrapper module, implementing hook_library or Libraries equivalent, and put the make file in that module.
    – Letharion
    Commented Nov 29, 2012 at 9:41
  • Thanks. Adding an example makefile to the docs was something I hadn't though of.
    – mpdonadio
    Commented Nov 29, 2012 at 12:05

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.