6

What is the best way to install a third party library in Drupal 8 that is not on packagist?

For example I have the Color Field module, which can use the jQuery Simple Color Picker library to provide a better user experience.

The most obvious solution is to just add the library manually, but that's not really maintainable.

My second idea was to add the git repository to my composer.json, as shown below, but it doesn't work because the target repository doesn't have a composer.json file.

"repositories": [
    {
        "name": "jquery-simple-color",
        "type": "git",
        "url": "https://github.com/recurser/jquery-simple-color.git"
    }
],
"require": {
    "jquery-simple-color/": "1.2.1"
}

Should I just fork the git repository and add a composer.json file there?

Chris
  • 195
  • 1
  • 4

3 Answers3

12

You was on the right track, in your composer.json you can make your own "packages" for example:

"repositories": [
  {
    "type": "package",
    "package": {
      "name": "jquery/simplecolor",
      "version": "1.2.1",
      "dist": {
        "url": "https://github.com/recurser/jquery-simple-color/archive/v1.2.1.zip",
        "type": "zip"
      },
      "type": "drupal-library"
    }
  }
]

And then include it trough

  "jquery/simplecolor": "1.2.1,
melvin
  • 1,145
  • 8
  • 12
  • Or (after you added it to the repositories array manually) include it via the following command: `composer require jquery/simplecolor`. (Which will then add it into the dependency array automatically.) – leymannx May 31 '18 at 10:34
2

Last tip : using 'simplecolor' in 'repositories' leads to the creation of a folder named libraries/simplecolor, that is not what Drupal is waiting for. As the folder needs to be libraries/jquery-simple-color, it's simpler to add :

"repositories": [ {
"type": "package",
"package": {
  "name": "jquery/jquery-simple-color",
  "version": "1.2.1",

and use : composer require jquery/jquery-simple-color

Artatum
  • 57
  • 2
  • 7
0

The accepted answer is correct. However, I've just come across a tool for that: https://asset-packagist.org/. Instead of specifying a separate repository source for each library, specify just this one additional:

"repositories": [
    { . . . },
    {
        "type": "composer",
        "url": "https://asset-packagist.org"
    }
]

This will allow you to then require libraries as needed.

"require": {
    "bower-asset/bootstrap": "^3.3",
    "npm-asset/jquery": "^2.2"
}

Note: Take note of the paths above: they are not pathed by user, but by type.

For a Drupal project, you should specify the newly available installer types and that each type is to be downloaded to the Drupal libraries directory:

"extra": {
    "installer-types": [
        "component",
        "bower-asset",
        "npm-asset"
    ],
    "installer-paths": {
        "web/libraries": [
            "type:drupal-library",
            "type:component",
            "type:bower-asset",
            "type:npm-asset"
        ],
    }
}

Additionally, when necessary, as in the case of the colorbox module, you can specify a per-project library path. Instead of it being downloaded as libraries/jquery-colorbox, you can tell it to download to libraries/colorbox as required by the Drupal module, noted in its documentation.

For Drupal 8.x : Download the Colorbox plugin and unpack in /libraries (at the root of your site). Make sure the path to the plugin file becomes: "/libraries/colorbox/jquery.colorbox-min.js".

Here's how:

Note: the custom per-project libraries path is specified before the generic asset-type libraries path--first applicable, first used.

"extra": {
    ...
    "installer-paths": {
        "web/libraries/colorbox": ["npm-asset/jquery-colorbox"],
        "web/libraries": [
            "type:drupal-library",
            "type:component",
            "type:bower-asset",
            "type:npm-asset"
        ],
    }
}

Source: Hawkeye "Derek DeRaps" Tenderwolf

https://drupal.tv/external-video/2018-07-15/how-using-drupal-project-composer-craft-your-perfect-start-state

jcandan
  • 11
  • 2