如何使用composer在drupal 8中应用补丁

hrs 提交于 2022/05/16 - 01:26 , 周一

Sometimes, when we use a contrib module in Drupal, we encounter a bug. Chances are that someone has already fixed it and contributed a patch to the Drupal community.

有时,当我们在Drupal中使用contrib模块时,会遇到一个错误。 很有可能有人已经修复它并为Drupal社区贡献了补丁。

First, let’s assume our site root folder is /var/www/public/drupal_site.

首先,假设我们的站点根文件夹为/ var / www / public / drupal_site 

This example will patch the module Scroll to destination anchors (https://www.drupal.org/project/scroll_to_destination_anchors)

此示例将修补模块滚动到目标锚点 ( https://www.drupal.org/project/scroll_to_destination_anchors )

These are the steps to apply a patch to a module using Composer:

这些是使用Composer将补丁应用到模块的步骤:

使用Composer添加模块 (Add the module with Composer)

To download the module to our site, all we have to do is run this command in a terminal while being in the site root folder:

要将模块下载到我们的站点,我们要做的就是在站点根目录中的终端中运行以下命令:

composer require drupal/scroll_to_destination_anchors

作曲家需要drupal / scroll_to_destination_anchors

This command will download the module to /var/www/public/drupal_site/modules or /var/www/public/drupal_site/modules/contrib (if we have created this directory, which is recommended).

此命令会将模块下载到/ var / www / public / drupal_site / modules/ var / www / public / drupal_site / modules / contrib (如果我们已创建此目录,建议使用)。

查找补丁 (Find a patch)

Finding a patch for the module is easy. It can be done from the module page (see example link above). There’s a column that says: “Issues for XXX”, in my example case “Issues for Scroll to destination anchors”.

查找模块的补丁很容易。 可以从模块页面完成(请参见上面的示例链接)。 有一列显示为:“ XXX的问题”,在我的示例中为“滚动到目标锚点的问题”。

This is the issue I was having:

这是我遇到的问题:

https://www.drupal.org/project/scroll_to_destination_anchors/issues/2916996

https://www.drupal.org/project/scroll_to_destination_anchors/issues/2916996

And there’s a patch for it already:

并且已经有一个补丁:

https://www.drupal.org/files/issues/2018-04-18/stda-working_for_84-2916996-8_0.patch

https://www.drupal.org/files/issues/2018-04-18/stda-working_for_84-2916996-8_0.patch

应用补丁 (Apply the patch)

To apply the patch to the contrib module, we have to edit the composer.json file in the site root folder.

要将补丁应用到contrib模块,我们必须在站点根文件夹中编辑composer.json文件。

Then look for this line: “enable-patching”: “true”,

然后查找以下行: “ enable-patching”:“ true”,

Then add something like this after it:

然后在其后添加如下内容:

"patches": {    "drupal/scroll_to_destination_anchors": {        "Working for 8.4?": "https://www.drupal.org/files/issues/2018-04-18/stda-working_for_84-2916996-8_0.patch"    }}

This is the structure:

结构如下:

"patches": {    "drupal/module_machine_name": {        "Description of the issue": "Path to patch in Drupal.org"    }}

And you will end up with something like this:

您将得到如下结果:

"extra": {    "drupal-scaffold": {        "locations": {            "web-root": "./"        }    },    "installer-paths": {        "core": [            "type:drupal-core"        ],        "libraries/{$name}": [            "type:drupal-library",            "type:bower-asset",            "type:npm-asset"        ],        "modules/contrib/{$name}": [            "type:drupal-module"        ],        "profiles/contrib/{$name}": [            "type:drupal-profile"        ],        "themes/contrib/{$name}": [            "type:drupal-theme"        ],        "drush/Commands/contrib/{$name}": [            "type:drupal-drush"        ],        "modules/custom/{$name}": [            "type:drupal-custom-module"        ],        "themes/custom/{$name}": [            "type:drupal-custom-theme"        ]    },    "drupal-core-project-message": {        "include-keys": [            "homepage",            "support"        ],        "post-create-project-cmd-message": [            "<bg=blue;fg=white>                                                         </>",            "<bg=blue;fg=white>  Congratulations, you’ve installed the Drupal codebase  </>",            "<bg=blue;fg=white>  from the drupal/legacy-project template!               </>",            "<bg=blue;fg=white>                                                         </>",            "",            "<bg=yellow;fg=black>Next steps</>:",            "  * Install the site: https://www.drupal.org/docs/8/install",            "  * Read the user guide: https://www.drupal.org/docs/user_guide/en/index.html",            "  * Get support: https://www.drupal.org/support",            "  * Get involved with the Drupal community:",            "      https://www.drupal.org/getting-involved",            "  * Remove the plugin that prints this message:",            "      composer remove drupal/core-project-message"        ]    },    "installer-types": [        "bower-asset",        "npm-asset"    ],    "patchLevel": {        "drupal/core": "-p2"    },    "enable-patching": "true","patches": {        "drupal/scroll_to_destination_anchors": {            "Working for 8.4?": "https://www.drupal.org/files/issues/2018-04-18/stda-working_for_84-2916996-8_0.patch"        }    }}

Then, all we have to do is run:

然后,我们要做的就是运行:

composer update

作曲家更新

And that’s it. The module will be downloaded and the patch automatically applied.

就是这样。 该模块将被下载并自动应用补丁。

标签