How to install third party libraries in Drupal 8 with Composer that are not on packagist?
Asked Answered
F

3

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?

Firestone answered 30/3, 2017 at 14:50 Comment(1)
Composer is built to manage PHP packages, you can manager JS package with Bower for example bower.ioInkblot
I
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,
Izawa answered 30/3, 2017 at 21:41 Comment(1)
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.)Postulate
M
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

Marrufo answered 19/3, 2021 at 5:40 Comment(0)
H
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

Hallagan answered 16/3, 2019 at 16:48 Comment(2)
was not able to get this running.Tollmann
@mogio, can you supply your composer.json for review?Hallagan

© 2022 - 2024 — McMap. All rights reserved.