每次保存节点后如何在 drupal 中执行操作?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您是通过
hook_nodeapi()
使用触发器还是 Mike Munroes 建议 (+1),在这种情况下都存在一个常见的陷阱:只要您的导出逻辑在处理的同一页面周期上运行更新,并且它使用
node_load()
获取节点数据,node_load()
可能会返回更新之前节点的静态缓存版本尚未包含更改。如果这是您遇到的问题,您可以通过两种方式解决:TRUE
作为第三个参数传递给node_load()。这将确保节点从数据库中重新填充(以一些额外的数据库查询为代价,因此请注意潜在的性能影响)。
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:TRUE
as the third parameter tonode_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).hook_nodeapi()
route, you could avoid the need to callnode_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.您应该使用 hook_nodeapi 并调用插入和更新操作。查看 hook_nodeapi 的文档,了解可以调用导出逻辑的其他实例。
模块名称 = 'export_to_xml' 的示例:
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':