每次更新节点时如何执行函数?我尝试使用 hook_nodeapi,但看起来该钩子已从 Drupal 7 中删除。
我想要做的是,
- 向我的内容类型添加一个名为“主图像(布尔)”的新字段,以让管理员设置主图像。
- 我将使用视图模块来显示节点,但按“主图像”字段和添加日期对节点进行排序。
- 以便第一个节点始终是主图像。
为此,我需要确保始终只有一个节点的“主图像”设置为 true。
How do I execute a function every time a node is being updated? I tried to use hook_nodeapi, but it looks like that the hook is removed from Drupal 7.
What I want to do is,
- add a new field to my content type called 'main image (bool)' to let the admin set a main image.
- I am going to use Views module to display nodes, but order nodes by 'main image' field and added date.
- so that the first node is always the main image.
to do this, I need to make sure that there is always only one node with 'main image' set to true.
发布评论
评论(5)
目前,Drupal 核心不提供任何钩子来在数据库中插入/更新/删除节点/实体后执行操作。例如,插入节点后您无法发送提及该节点的电子邮件,因为 Drupal 使用 SQL 事务,并且当调用钩子节点预保存时该节点尚未完全写入数据库,因此如果出于任何原因事务回滚,用户将收到虚假邮件。
因此,Hook Post Action 模块引入了几个新的 Drupal 钩子来克服此限制:
https://drupal.org/project/hook_post_action
Currently Drupal core does not offer any hook to do actions after a node/entity is inserted/updated/deleted in Database. For example, you can not send an email mentioning the node after the node is inserted because Drupal uses SQL transactions and the node is not yet fully written to database when hook node presave is called so if for any reason the transaction is rolled back, users will receive a false mail.
So Hook Post Action module introduces several new Drupal hooks to overcome this limitation:
https://drupal.org/project/hook_post_action
该钩子尚未被删除,而是为每个 $op 分成单独的钩子。
请参阅:http://api.drupal.org/api/search/7/hook_node
对于保存后,您需要 hook_node_insert() 和 hook_node_update()
The hook has not been removed but splitted up into separate hooks for each $op.
See: http://api.drupal.org/api/search/7/hook_node
For post-save, you want hook_node_insert() and hook_node_update()
我想
hook_entity_presave
可能是您正在寻找的挂钩:或者,如果您更喜欢在更新后进行操作,请查看
hook_entity_update
:I suppose
hook_entity_presave
could be the hook you're looking for, if you want to act before your node is updated :Or, if you prefer acting after it's updated, take a look at
hook_entity_update
:只是为了完成这一点,如果您需要在保存/更新节点后执行任何操作,您可以使用 @sina-salek 推荐的模块,或者您可以使用以下代码:
通过使用 drupal_register_shutdown_function 您确保在以下情况下调用您的自定义函数:钩子已完成,节点已持久化在数据库上。
Just to complete this a bit more and if you need to perform any operation after the node has been saved/updated you can use the module @sina-salek has recommended you or you can use this code:
By using the drupal_register_shutdown_function you are making sure to call your custom function when the hook has finished and the node has been persisted on the DB.
实现此目的的另一种方法是使用自定义类扩展 Node 实体并在
Node::postSave
方法内调用代码。当节点保存或更新时,将调用此方法。您可以通过实现名为
hook_entity_type_build
的钩子来指定自定义扩展类,并提供新类,例如:$entity_types['node']->setClass(NodeExtended::class)
在你的类中,你可以重写
postSave
方法。我通常只是在此处调度我的自定义事件,以便其他模块可以订阅此节点后保存事件,但这是另一个主题。Another way this can be achieved is by extending the Node entity with your custom class and calling your code inside the
Node::postSave
method. This method will get called when node gets saved or updated.You specify your custom extended class by implementing hook called
hook_entity_type_build
and provide your new class, e.g.:$entity_types['node']->setClass(NodeExtended::class)
Inside your class you than override
postSave
method. I usually just dispatch my custom event here so other modules can subscribe to this node post save event, but that's another topic.