使用Composer给Drupal打Patch补丁

大伟哥 提交于 2020-06-23,周二, 10:58

使用Composer可以方便地管理Drupal核心版本和模块版本的更新,那如果是已经有Drupal的补丁解决了你急需要解决的问题,而Drupal核心团队或者模块作者还没来得及或者压根不想把补丁打包进新的版本呢?使用Composer追求自动化的你一定不想在Composer每次更新版本之后,还要再次手动给Drupal打补丁。既然用了Composer,那就让Composer全部代劳吧,连补丁一块儿。

要想让Composer干好这活, 得给它额外安装个工具,叫composer-patches。安装很简单,在项目根目录里require一下就行了:

composer require cweagans/composer-patches

然后在项目的composer.json文件的extra部分,定义需要使用的补丁,格式如下:

"extra": {
    "enable-patching": true,
    "patches": {
        "drupal/core": {
            "patch1 information": "patch1 file path",
            "patch2 information": "patch2 file path"
        },
        "drupal/module_name": {
            "patch information": "patch file path"
        }
     },
}

其中,Drupal核心模块的补丁定义在drupal/core部分,其他模块的补丁定义在相关模块drupal/module_name部分,patch1 information可以填写补丁的相关信息和作用,patch file path是补丁文件的网址。

下面是一个给模块Menu token打补丁的示例:

        "patches":{
         "drupal/menu_token":{
            "get the right url path for a views page":"https://www.drupal.org/files/issues/2020-05-03/menu_token_2921178_27_route-resolved-links.patch"
               }
            },

保存好composer.json文件以后,需要再运行composer install命令来打上补丁:

composer install
Gathering patches for root package.
Removing package drupal/menu_token so that it can be re-installed and re-patched.
  - Removing drupal/menu_token (1.0.0-alpha3)
Deleting web/modules/contrib/menu_token - deleted
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Warning: The lock file is not up to date with the latest changes in composer.json. 
You may be getting outdated dependencies. It is recommended that you run 
`composer update` or `composer update <package name>`.
Package operations: 1 install, 0 updates, 0 removals
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing drupal/menu_token (1.0.0-alpha3): Loading from cache
  - Applying patches for drupal/menu_token
    https://www.drupal.org/files/issues/2020-05-03/menu_token_2921178_27_route-
resolved-links.patch (get the right url path for a views page)

Package container-interop/container-interop is abandoned, you should avoid using it. 
Use psr/container instead.
Generating autoload files
28 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
  * Homepage: https://www.drupal.org/project/drupal
  * Support:
    * docs: https://www.drupal.org/docs/user_guide/en/index.html
    * chat: https://www.drupal.org/node/314178

从上面的输出来看,composer先是卸载了已经安装的menu token模块,又重新安装它,然后再在新安装的模块上打上了补丁。

这时候清除Drupal的缓存以后再打开网站的相应页面,已经可以确定补丁生效了。但是还没完,我们需要再运行composer update --lock命令,来更新composer.lock文件:

composer update --lock
Gathering patches for root package.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
28 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
文章分类

标签

添加新评论

此字段内容将保密,不会被其他人看见。

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
发布评论前,请通过下面的验证,以免被当作垃圾评论机器人屏蔽。
18 + 1 =
计算出这道简单的算术题并键入答案。例如、1+3,就输入 4。