I'm trying to send out mail about new content and track who has been sent mail in a field of the node. The nid value isn't there from hook_node_presave(). It's set in hook_node_insert(), but the automatic path alias has not been set yet, so links look like /node/123, which the client doesn't like. I can run from hook_entity_insert() and make my hook run after path's. But how do I update the field value at that point? I tried field_attach_update() but I guess it's too late in the process for that. I'm pretty sure that calling node_save() inside any those hooks would be a bad idea.
Most helpful answers
Hi brad.bulger,
I'm pretty sure that calling node_save() inside any those hooks would be a bad idea.
I'd agree. At that point the entity is about to get inserted. Because of that storing values in a field of the very same node seems like a bad idea.
How about a custom database table (created with hook_schema) and you insert data there? You probably don't need many table columns...
- A serial field as primary id
- A field for the nid
- A field for your own data (possibly serialized?)
Not sure if that fits your workflow, though.
Comments
Hi brad.bulger,
I'm pretty sure that calling node_save() inside any those hooks would be a bad idea.
I'd agree. At that point the entity is about to get inserted. Because of that storing values in a field of the very same node seems like a bad idea.
How about a custom database table (created with hook_schema) and you insert data there? You probably don't need many table columns...
- A serial field as primary id
- A field for the nid
- A field for your own data (possibly serialized?)
Not sure if that fits your workflow, though.
That's probably what I'll have to do. Or update the node in a presave function and then act on it in a later hook, or use a form submit function entirely after the node_save() process is complete. Though I'm trying not to tie it directly to the form.
There are some notification type contrib modules, I think? I should look how they're doing this.
Thanks.
You could also try using the Hook Post Action contrib module:
https://github.com/backdrop-contrib/hook_post_action
I've been using the D7 edition on one site for a few years without any problems.
Thanks. That module actually looks familiar from past cases of needing to do this sort of thing. But I think modifying the node in one of those hooks is still a bad idea. Adding custom tables extends this module more than intended.
I ended up making the node modifications in presave and then acting on them in a later hook, using module_implements_alter to push my hook to the end of the list so that the alias will be defined.
You could also try using the Hook Post Action contrib module:
https://github.com/backdrop-contrib/hook_post_action
I've been using the D7 edition on one site for a few years without any problems.