Downloading and installing libraries for every new site is a PITA.
So I've spent an hour today with trial and error to find out how libraries can be installed with composer.
Results: there are three things to do in composer.json
- define an "installer path" for "drupal-library"
- define the library source in "repositories"
- require the library. it will then be installed using the above settings
If you read my other articles, it will be no surprise to you that this is where Drupal documentation often ends. But I'm here to help !
so,the first part is defining an installer path.
in your composer.json file, you got a section called "extras" and a subsection "installer paths", such as this:
"extra": { "installer-paths": { "web/core": [ "type:drupal-core" ], "web/libraries/{$name}": [ "type:drupal-library", "type:bower-asset", "type:npm-asset" ],
there may be other lines between installer-paths and web/libraries.
here you see the path already defined, make sure you got something looking like the above.
what it does is telling composer, hey if you are fed any library of the type "drupal-library", then install it in the path web/libraries/ under its name.
the second part is about defining the library source.
this part can be a bit tricky because of the lack of easily understandable documentation.
GIT libraries are not all equal, some have numbered versions, some don't, some have several branches, or not. GIT also uses labels.
This library, for example, has a version and a label:
https://github.com/woocommerce/FlexSlider
the GIT link is https://github.com/woocommerce/FlexSlider.git (found if you press the green "code" button)
and version number is also the label, it's 2.7.2 (if labeled, the label is a proper label pictogram at the right side)
so for that library, we would add the following in the composer.json's "repositiories" section:
"repositories": [ { "type": "package", "package": { "name": "woothemes/flexslider", <= the name, the second part is important "version": "2.7.2", <= the version. use the version number if there is one, if not, just write "1" "type": "drupal-library", <= hey composer, that's a drupal library, remember that type? "source": { "url": "https://github.com/woothemes/FlexSlider.git", <= git link "type": "git", "reference": "2.7.2" <= important !! see below } } }
okay, so this is a nice library, it has numbered versions.
the name is important, in the example above, modules will look for the name "flexslider" in the libraries folder. even if you load the right library, if it has a different name than expected by the module, it will not work.
in some cases, the libraries are names just "cycle", but modules will expect jquery.cycle
fill the expected name into that field and all will be fine.
next part, the version. it is a required field in composer, but you can write what you want in there. it will be used when libraries will be installed or updated.
the trickiest part is "reference". IF the library has a LABEL that IS a version number, use that one. Like in the example above, 2.7.2 looks like a fine version number, use it and it will work.
now if the label does not look like a properly formatted version number, for example "v1.10.2" (notice the V ?) OR if there is NO LABEL, then using the version number will not work, nor will omitting it.
in this case, the field "reference" needs the name of a GIT branch. Look at this example:
https://github.com/briancherne/jquery-hoverIntent.git
the correct procedure here is to use the branch name "master" as reference:
{ "type": "package", "package": { "name": "briancherne/jquery.hoverintent", "version": "1.10.2", "type": "drupal-library", "source": { "url": "https://github.com/briancherne/jquery-hoverIntent.git", "type": "git", "reference": "master" } } },
and don't forget the indentations and commas.
composer loves indentations and can't live without the proper commas.
and part 3 is - the last step - to require the library.
using both examples above, I require these libraries by adding them into the "require" section of composer.json as follows:
"require": { "woothemes/flexslider": "^2.7", "briancherne/jquery.hoverintent": "*" },
the "require" section contains all your installed modules, so there should be a small list of them already. As always, never forget the indentations and commas.
I used an asterisk as a required version, to tell composer that anything goes, when the library only have one release anyway.
I could also have used (better) :
"require": { "woothemes/flexslider": "^2.7", "briancherne/jquery.hoverintent": "^1.10" },
- Log in to post comments
Comments