你能在 Drupal 中创建自己的 Hook 吗?

发布于 2024-10-17 11:43:21 字数 664 浏览 1 评论 0原文

是否可以在 Drupal 模块中创建您自己的挂钩以供其他 Drupal 模块使用?如果没有,Drupal 中有没有一种机制可以让第三方开发者提供钩子?如果到目前为止一切都是“否”,那么钩子列表在核心的哪里实现的?

据我了解,Drupal 模块在一个名为 hooks。当您创建新模块时,您将创建实现挂钩的函数。例如,有一个 hook_delete 挂钩。如果您在模块中实现一个函数,则

function mymodule_delete($node)
{
}

每当删除节点时都会调用该函数。

我想知道的是,作为第三方模块开发人员,我是否有办法创建我自己的挂钩。例如,类似 hook_alanskickbutthook 的内容,以便其他模块开发人员可以订阅此挂钩。

如果这是可能的,你会怎么做?我查遍了官方文档,并没有找到太多东西,当我开始研究 Drupal 源代码时,我仍然感到有点头晕(我了解递归,但没有花足够的时间思考递归问题)。欢迎提供完整的解决方案,但我很高兴能指出正确的方向。

Is it possible to create your own hook in a Drupal module for other Drupal modules to consume? If not, is there a mechanism in Drupal for third party developers to provide hooks? If everything's been a no so far, where in the core are the list of hooks implemented?

As I understand things, Drupal modules work on a event like system called hooks. When you create a new module, you create functions that implement a hook. For example, there's a hook_delete hook. If you implement a function in your module

function mymodule_delete($node)
{
}

this function will be called whenever a node is deleted.

What I want to know is, is there a way or me, as a third party module developer, to create my own hooks. Say, something like hook_alanskickbutthook so that other module developers could subscribe to this hook.

If this is possible, how do you do it? I've looked around the official docs and haven't found much there, and I still get a little dizzy when I start poking around the Drupal source code (I understand recursion, but don't spend enough time thinking about recursive problems). Full solutions are welcome, but I'm happy to just be pointed in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恏ㄋ傷疤忘ㄋ疼 2024-10-24 11:43:21

Module_invoke_all() 是您创建自己的钩子的门票:

请参阅 API:

http:// /api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all

然后看看这篇精彩的文章:

http://web.archive.org/web/20101227170201/http://himerus.com/blog/himerus /creating-hooks-your-drupal-modules

(编辑:位于 http:// /himerus.com/blog/himerus/creating-hooks-your-drupal-modules 但这现在已经消失了)

一旦你创建了你的钩子,就可以使用以下方法在另一个模块中调用它:

/**
 * Implementation of hook_myhookname()
 */

function THISMODULENAME_myhookname(args){
  //do stuff
}

Module_invoke_all() is your ticket to creating your own hooks:

see the API:

http://api.drupal.org/api/drupal/includes--module.inc/function/module_invoke_all

and then look at this great writeup:

http://web.archive.org/web/20101227170201/http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules

(edit: was at http://himerus.com/blog/himerus/creating-hooks-your-drupal-modules but this is now gone)

Once you've made your hook, it can be called in another module using:

/**
 * Implementation of hook_myhookname()
 */

function THISMODULENAME_myhookname(args){
  //do stuff
}
季末如歌 2024-10-24 11:43:21

例如,假设您想创建 hook_my_custom_goodness() 供其他人使用。然后只需将这样的代码放在模块中您希望挂钩发生的位置:

$variables['msg'] = 'foo';

// Make sure at least one module implements our hook.
if (sizeof(module_implements('my_custom_goodness')) > 0) {
  // Call modules that implement the hook, and let them change $variables.
  $variables = module_invoke_all('my_custom_goodness', $variables);
}

drupal_set_message($variables['msg']); // Will display 'bar' instead.

现在,如果有人想使用您的挂钩,那么他们可以在自己的模块中这样做,如下所示:

/**
 * Implements hook_my_custom_goodness().
 */
function SOME_OTHER_MODULE_my_custom_goodness($variables) {
  $variables['msg'] = 'bar';
  return $variables;
}

这里有更完整的解释:

http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-其他模块

For example, say you wanted to create hook_my_custom_goodness() for others to use. Then just place code like this in your module at the point where you want the hook to happen:

$variables['msg'] = 'foo';

// Make sure at least one module implements our hook.
if (sizeof(module_implements('my_custom_goodness')) > 0) {
  // Call modules that implement the hook, and let them change $variables.
  $variables = module_invoke_all('my_custom_goodness', $variables);
}

drupal_set_message($variables['msg']); // Will display 'bar' instead.

Now, if anybody wanted to use your hook, then they could do so in their own module like this:

/**
 * Implements hook_my_custom_goodness().
 */
function SOME_OTHER_MODULE_my_custom_goodness($variables) {
  $variables['msg'] = 'bar';
  return $variables;
}

There is a more complete explanation here:

http://tylerfrankenstein.com/code/drupal-create-custom-hook-for-other-modules

万劫不复 2024-10-24 11:43:21

您需要实现两个钩子 1. hook_token_info() & 2. 模块文件中的 hook_tokens() 下面我给出了创建自定义令牌“query-param-all”的代码,并在视图 - Textarea 字段中使用该令牌......

/**
* Implements hook_token_info().
*/
function mycustom_token_info() {
  $type = [
      'name' => ('Custom Token'),
      'description' => ('Tokens for custom things.'),
  ];
  $node['query-param-all'] = [
      'name' => ("Get all URL query string"),
      'description' => ('Get all URL query string'),
  ];
  return [
      'types' => ['customtoken' => $type],
      'tokens' => ['customtoken' => $node],
  ];
}

/**
* Implements hook_tokens().
*/
function mycustom_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  //print '<pre>'; print_r($data);exit;
  $current_path = \Drupal::request()->query->all();
  $query_param = '';
  if( count($current_path) > 0) {
    $amper = '';
    $query_param = '?';
    foreach($current_path as $key => $value){
      $query_param .= $amper.$key.'='.$value;
      $amper = '&';
    }
  }

  if ($type == 'customtoken') {
      foreach ($tokens as $name => $original) {
          switch ($name) {
              case 'query-param-all':
                  $replacements[$original] = $query_param;
              break;
          }
      }
  }
  return $replacements;
}

You need to implement two hooks 1. hook_token_info() & 2. hook_tokens() in your module file Below i have given code to create my custom token "query-param-all" and used that token in views- Textarea field.....

/**
* Implements hook_token_info().
*/
function mycustom_token_info() {
  $type = [
      'name' => ('Custom Token'),
      'description' => ('Tokens for custom things.'),
  ];
  $node['query-param-all'] = [
      'name' => ("Get all URL query string"),
      'description' => ('Get all URL query string'),
  ];
  return [
      'types' => ['customtoken' => $type],
      'tokens' => ['customtoken' => $node],
  ];
}

/**
* Implements hook_tokens().
*/
function mycustom_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  //print '<pre>'; print_r($data);exit;
  $current_path = \Drupal::request()->query->all();
  $query_param = '';
  if( count($current_path) > 0) {
    $amper = '';
    $query_param = '?';
    foreach($current_path as $key => $value){
      $query_param .= $amper.$key.'='.$value;
      $amper = '&';
    }
  }

  if ($type == 'customtoken') {
      foreach ($tokens as $name => $original) {
          switch ($name) {
              case 'query-param-all':
                  $replacements[$original] = $query_param;
              break;
          }
      }
  }
  return $replacements;
}
七分※倦醒 2024-10-24 11:43:21

如果我记得... http ://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_delete/7

这有帮助吗?自从我搞乱 Drupal 以来已经有一段时间了。

要创建/提供自定义 Drupal 挂钩,您必须以某种方式实现,以便使用 module_invoke 或 module_invoke_all 调用挂钩不会与其他模块挂钩产生任何冲突。挂钩的名称应该是唯一的,并且应该以通用方式提供所有/特定功能,不需要对代码进行任何类型的调整。所有配置都必须位于管理页面上,并且应将这些配置存储在单独的表中或由 Drupal 或模块所依赖的模块创建的任何现有表中。钩子应该很容易被其他模块实现,并且实现起来不应该太复杂。当您创建自定义挂钩时,您的模块充当 API 提供者。

If i recall... http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_delete/7

does ths help? been a while since I messed with Drupal.

To create/offer custom Drupal hook, you must implement in a ways such that calling the hook with module_invoke or module_invoke_all does not make any conflicts with other module hooks. The name of the hook should be unique and it should offer all/specific feature in such a general way that it doesn't require any type of adjustments with code. All the configuration must go on admin pages and should store those configurations in a separate table or any existing tables create by Drupal or modules on which your modules depends. The hook should be easy to implment by other modules and it should not be much complex to implement. When you create custom hooks, your module(s) act(s) as API provider.

朦胧时间 2024-10-24 11:43:21

对于 Drupal 6 和7、 drupal_alter() 可能是最好的选择。

module_invoke_all()文档,

所有参数均按值传递。如果需要,请使用 drupal_alter()
通过引用传递参数。

在 Drupal 8 中,使用 ModuleHandler::alter

将可变变量传递给特定的 hook_TYPE_alter()
实现。

For Drupal 6 & 7, drupal_alter() is probably the best option.

As stated in the module_invoke_all() documentation,

All arguments are passed by value. Use drupal_alter() if you need to
pass arguments by reference.

In Drupal 8, use ModuleHandler::alter.

Passes alterable variables to specific hook_TYPE_alter()
implementations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文