每次保存节点后如何在 drupal 中执行操作?

发布于 2024-08-31 18:47:19 字数 222 浏览 2 评论 0原文

我正在 Drupal 中开发一个 Action,该 Action 应该在保存节点后激活,将内容导出到 XML(其中包括刚刚保存的节点中的数据),使用“触发器:保存更新后的帖子”触发器。

不幸的是,此操作实际上发生在最近保存的帖子中的信息保存到数据库之前。 IE。稍后查看 XML 时,我发现我最近所做的更改并未包含在内。编辑不同节点后保存将恢复之前丢失的数据。

保存过程完成后如何才能触发我的操作?

I'm developing an Action in Drupal which is supposed to activate after saving a node, exporting content to XML (which includes data from the node that was just saved), using the "Trigger: After saving an updated post" trigger.

Unfortunately this action actually happens right before the information from the recently saved post is saved to the database. ie. when looking at the XML later, I find that the most recent change I made was not included. Saving after editing a different node will restore the previously missing data.

How can I get my action to fire after the saving process is complete?

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

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

发布评论

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

评论(2

夏有森光若流苏 2024-09-07 18:47:19

无论您是通过 hook_nodeapi() 使用触发器还是 Mike Munroes 建议 (+1),在这种情况下都存在一个常见的陷阱:

只要您的导出逻辑在处理的同一页面周期上运行更新,并且它使用 node_load() 获取节点数据,node_load() 可能会返回更新之前节点的静态缓存版本尚未包含更改。如果这是您遇到的问题,您可以通过两种方式解决:

  1. 通过将 TRUE 作为第三个参数传递给 node_load()。这将确保节点从数据库中重新填充(以一些额外的数据库查询为代价,因此请注意潜在的性能影响)。
  2. 如果您要使用 hook_nodeapi() 路线,并且传递可用的 $node 对象,则可以完全避免调用 node_load()直接到您的导出函数,因为它将代表更新的状态。

There is a common pitfall in this context, regardless of whether you use a trigger or Mike Munroes suggestion via hook_nodeapi() (+1):

As long as your export logic runs on the same page cycle that processed the update, and it uses node_load() to get the nodes data, node_load()might return a statically cached version of the node from before the update that does not contain the changes yet. If this is the problem in your case, you can work around it in two ways:

  1. Force a reset of the static node cache by passing TRUE as the third parameter to node_load(). This would ensure that the node gets populated freshly from the database (at the price of some additional db queries, so be aware of a potential performance impact).
  2. If you are going the hook_nodeapi() route, you could avoid the need to call node_load() altogether if you pass the $node object available there directly to your export function, as it will be a representation of the updated state.
寂寞陪衬 2024-09-07 18:47:19

您应该使用 hook_nodeapi 并调用插入和更新操作。查看 hook_nodeapi 的文档,了解可以调用导出逻辑的其他实例。

模块名称 = 'export_to_xml' 的示例:

 /**
 * Implementation of hook_nodeapi().
 */
function export_to_xml_nodeapi(&$node, $op, $a3, $a4) {
  if ($op == 'update' || $op == 'insert') {
    export_logic_function();
  }
}

You should use hook_nodeapi and invoke your action on insert and update. Look over the documenation for hook_nodeapi for other instances where you could call your export logic.

example where module name = 'export_to_xml':

 /**
 * Implementation of hook_nodeapi().
 */
function export_to_xml_nodeapi(&$node, $op, $a3, $a4) {
  if ($op == 'update' || $op == 'insert') {
    export_logic_function();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文