在 Drupal 中捕获块的更改/添加/删除事件

发布于 2024-10-05 07:23:02 字数 217 浏览 11 评论 0原文

当添加、移动、编辑或删除 Drupal 中的块时,我需要添加一些功能(刷新一些缓存等),是否有任何类型的钩子(或另一种有点 Drupal 本地方式),就像带有 < 的节点一样代码>hook_nodeapi?

我知道有 hook_block 但有 $op 总是 list,所以它实际上没有任何好处。

I need to add some functionality (flush some caches and such) when a Block in Drupal is added, moved, edited or deleted, is there ANY kind of hook for that (or another somewhat Drupal native way) like there is for nodes with hook_nodeapi?

I know there is hook_block but there $op is always list, so its not really any good.

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

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

发布评论

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

评论(1

毁梦 2024-10-12 07:23:02

不幸的是,区块没有这种信号机制。我会使用表单系统在您需要额外工作信号的地方添加提交回调。

/**
 * Implementation of hook_form_alter().
 */
function custom_form_alter(&$form, &$form_state, $form_id) {
  // Overview form.
  if ($form_id == 'block_admin_display_form') {
    $form['#submit'][] = 'custom_block_admin_display_form_submit';
  }
  // Individual block configuration form.
  elseif ($form_id == 'block_admin_configure') {
    $form['#submit'][] = 'custom_block_admin_configure_submit';
  }
}

/**
 * Submit handler for block overview form.
 */
function custom_block_admin_display_form_submit($form, &$form_state) {
  cache_clear_all();
}

/**
 * Submit handler for block configuration form.
 */
function custom_block_admin_configure_form_submit($form, &$form_state) {
  drupal_set_message(t('You have changed a block. Run for the hills!'));
}

这种方法的一个缺点是任何配置块的替代方法都不起作用。如果有人在块模块之外构建自定义表单,或者您使用上下文或面板来移动块,则不会有帮助。当然,由于任何这些备用配置点也将使用表单,因此您也可以使用 hook_form_alter() 侵入它们的提交流程。

Unfortunately blocks don't have that kind of signalling mechanism. I would use the forms system to add submit callbacks wherever you need a signal for additional work.

/**
 * Implementation of hook_form_alter().
 */
function custom_form_alter(&$form, &$form_state, $form_id) {
  // Overview form.
  if ($form_id == 'block_admin_display_form') {
    $form['#submit'][] = 'custom_block_admin_display_form_submit';
  }
  // Individual block configuration form.
  elseif ($form_id == 'block_admin_configure') {
    $form['#submit'][] = 'custom_block_admin_configure_submit';
  }
}

/**
 * Submit handler for block overview form.
 */
function custom_block_admin_display_form_submit($form, &$form_state) {
  cache_clear_all();
}

/**
 * Submit handler for block configuration form.
 */
function custom_block_admin_configure_form_submit($form, &$form_state) {
  drupal_set_message(t('You have changed a block. Run for the hills!'));
}

The one downside to this method is that any alternate approach to configuring blocks won't work. If someone builds a custom form outside the block module, or if you are using context or panels to move blocks around it won't help. Of course, since any of those alternate configuration points would also use a form, you can use hook_form_alter() to hack into their submit processes as well.

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