如何使用不在 packagist 上的 Composer 在 Drupal 8 中安装第三方库?

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

在 Drupal 8 中安装不在 packagist 上的第三方库的最佳方法是什么?

例如我有 Color Field module, which can use the jQuery Simple Color Picker library 来提供更好的用户体验。

最明显的解决方案是手动添加库,但这并不是真正可维护的。

我的第二个想法是将 git 存储库添加到我的 composer.json,如下所示,但它不起作用,因为目标存储库没有 composer.json文件。

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

我是否应该 fork git 存储库并在其中添加一个 composer.json 文件?

您走在正确的轨道上,在您的 composer.json 中您可以制作自己的 "packages" 例如:

"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"
    }
  }
]

然后通过

包含它
  "jquery/simplecolor": "1.2.1,

接受的答案是正确的。但是,我刚刚遇到了一个工具:https://asset-packagist.org/。无需为每个库指定一个单独的存储库源,只需额外指定一个:

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

这样您就可以根据需要要求库。

"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.

对于 Drupal 项目,您应该指定新的可用安装程序类型,并将每种类型下载到 Drupal libraries 目录:

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

此外,必要时,如 colorbox 模块,您可以指定每个项目的库路径。您可以告诉它按照 Drupal 模块的要求下载到 libraries/colorbox,而不是将其下载为 libraries/jquery-colorbox,这在其文档中有所说明。

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".

方法如下:

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

最后提示:在 'repositories' 中使用 'simplecolor' 会导致创建一个名为 libraries/simplecolor 的文件夹,这不是 Drupal 所等待的。 由于文件夹需要libraries/jquery-simple-color,添加更简单:

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

并使用: 作曲家需要 jquery/jquery-simple-color