【问题标题】:How to specify a code dependency in a Drupal 7 module如何在 Drupal 7 模块中指定代码依赖项
【发布时间】:2016-07-15 22:55:47
【问题描述】:

我目前正在制作一个 Drupal 模块,我想知道是否有任何机制可以让我指定一个 github 存储库,在安装时,我的模块将获得一些它所依赖的代码。

重要提示:我所依赖的本身也不是 Drupal 模块,而只是一个代码库。因此,除非它支持检查 github 存储库,否则我认为我的 info 文件中的 dependencies 不会有帮助?

【问题讨论】:

  • 我没用过,但Composer Manager 应该可以完成这项工作
  • 我熟悉 composer 并将其用于其他项目,但这取决于我的 drupal 项目是否使用它设置,这不是一个选项。

标签: drupal module drupal-7 dependencies libraries


【解决方案1】:

它们不是 Drupal 7 中管理外部依赖项的官方方式。

但没有什么(真的)阻止你使用像 bowercomposer 这样的包管理器。

对于作曲家来说,他们是两个贡献项目:

对于 Drupal 8,composer 可用于将依赖项添加到 Drupal 项目。查看文档:Using Composer in a Drupal project

【讨论】:

    【解决方案2】:

    大多数项目似乎在其项目页面、安装说明和 README 文件中清楚地列出了外部库依赖项。如果用户尚未安装所需的库,许多人使用库 API 来包含依赖项并显示配置警告。 CKeditor、colorbox 和 JSON2 都是我想到的例子。

    更新

    不,库模块不下载依赖项。它只是提供了一个安装位置以及在模块中包含和使用的方法。请参阅库项目页面

    安装库模块。在sites/all/libraries安装外部依赖。

    首先,使用 hook_libraries_info() 注册库。

    /**
     * Implements hook_libraries_info().
     */
    function mymodule_libraries_info() {
      $libraries['anet_php_sdk'] = array(
        'name' => 'Authorize.net PHP SDK',
        'vendor url' => 'https://developer.authorize.net/',
        'download url' => 'http://developer.authorize.net/downloads/',
        'version arguments' => array(
          'file' => 'README',
          'pattern' => '/Version (\d+)/',
          'lines' => 206,
          'version' => '1.1.8'
        ),
        'files' => array(
          'php' => array('AuthorizeNet.php'),
        ),
      );
      return $libraries;
    }
    

    然后在你的模块中包含依赖项。

    /**
     * Assemble and send DPM payment request to Authorize.net.
     */
    function mymodule_process_payment($reservation) {
      // Include/require the dependency using libraries_load().
      $library = libraries_load('anet_php_sdk');
      // Use the external dependency...
    }
    

    访问libraries 项目页面以获取更多文档,包括需要特定版本以及处理检查和通知的示例。

    【讨论】:

    • 库 API 是否允许自动下载?例如,如果我制作一个模块并将它需要的外部代码放在 github 上,我可以让库从 github 获取代码并将其设置为加载吗?
    • 没有。我想大多数模块开发人员不想处理检查和实现可下载的各种可能性(即 Composer、Git、wget、curl 等)。
    • 更新了我的答案,给出了在模块中包含 Authorize.net 的 PHP SDK 的示例。
    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多