8

I would like to add library to my drupal 7 custom module. Library can be installed via composer, but i can't find a solution for install? How can i install this library? I have installed "composer manager" but i still can't get it running.

  • I would love to know the answer of this question too. – itsdarrylnorris Feb 24 '15 at 16:36
  • 1
    Disagree with the close vote on this. How to use composer with Drupal is definitely on topic. – mpdonadio Feb 26 '15 at 16:24
6

To get started, add a composer.json file to your module. The only thing you need here is the require section that includes your external library.

Composer manager is one way to combine all of your dependencies together, but there are other options to get Drupal to load your autoload.php file. The technique I prefer is to create a project-level composer.json file in your Drupal root, and use the module composer_vendor to include your autoload.php file.

The easiest way to get started is to use the composer_generate Drush extension to build your project-level composer.json file.

$ drush dl composer_generate $ cd /path/to/drupalroot $ drush composer-generate > composer.json $ composer install

Composer-generate will add all of the projects currently used in your Drupal site, plus composer_vendor, so composer install should just work for you. The main magic here is in the custom autoloaders added to the template; take a look at the output to see what was included.

Some caution needs to be used here, of course; the default template will put all of your modules in sites/all/modules/contrib. You can edit the template prior to running composer install if this isn't what you want.

You'll also have to be careful if you have any modified modules; there are a number of ways to deal with any hacked content, but that's a bit beyond the scope of this one question.

Update

If you don't want to run Drush, you can also hand-type your own composer.json file using the project https://github.com/drupal-composer/drupal-project. This is the template that composer-generate is based on.

  • we don't use drush. It is about this lib: github.com/giggsey/libphonenumber-for-php – gregab Feb 26 '15 at 6:33
  • You could install Drush and use it only to download and run the composer_generate tool. This would be faster than hand-typing your own composer.json file. – greg_1_anderson Feb 26 '15 at 21:28
  • @greg_1_anderson adding on to the main question, how can we load vendor autoload.php for 3rd party api? – Sukhjinder Singh Sep 15 '16 at 12:50
  • Drupal 8 will load the autoloader for you, so I presume you are asking about Drupal 7. Either use the composer_vendor module I recommend above, or just add the include to your settings.php file. – greg_1_anderson Sep 15 '16 at 20:28

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.