1

I have used this function hook_node_insert() , but it does not help me because this hook runs before the node is actually saved in the database. I am indexing nodes to a third party application whenever a node is inserted, updated or deleted. The application sends request to the node URL, but using hook_node_insert(). It's giving me an invalid document location error because the node is not actually inserted yet.

Is there any hook invoked after a node is saved or updated in the database?

I followed this approach, without success.

function  my_module_node_insert($node) {
  if (!$node->type == 'article' || !$node->type == 'page') { 
    return;
  }
  my_module_do_index($node);
}
  • 2
    This question appears to be off-topic because it is about code you wrote, for which you want to write better code, or you want to understand why it doesn't work, but you didn't show the code you are using, or the part of the code that is relevant for the question. See Help Center. – Mołot Sep 2 '14 at 13:24
  • have you aware about rules module ? – Rupesh Sep 2 '14 at 13:42
  • No. I am new to drupal – Adeel Sep 2 '14 at 13:44
  • 1
    Unfortunately there's no such thing. Perhaps the best way to do this (like all the other indexing modules) is to do it via cron jobs? – Beebee Sep 2 '14 at 15:11
0

You may be able to use Hook Post Action and then use this hook:

function my_module_node_postinsert($node) {
  if(!$node->type == 'article' || !$node->type == 'page' ) 
      return;
  my_module_do_index($node) ; // indexes the node
}

Give it a try and see if it helps!

The project link: https://www.drupal.org/project/hook_post_action

|improve this answer|||||
0

What you can do then is register a function with Drupal that will get called just about when Drupal is finished with the request. It's actually very simple. You just call drupal_register_shutdown_function() with the name of the function you want to be called. Additionally, you can pass as further arguments whatever it is in your context you want passed to it. It's kinda like subscribing a function to an event.

So now when the request is finishing, we know the node_save() database transaction has completed and we can rely on the data from the database. Using path_load() now will guarantee that the latest path for that node is being loaded.

Source: Drupal 7 post insert hooks as a shutdown function - https://www.webomelette.com/drupal-7-post-insert-hooks-shutdown-function

|improve this answer|||||
-1

What you want to use is this function

hook_entity_insert($entity, $type)

This function is called after the hook_node_insert and trigger your my_module_do_index( $node )

Same with the update function use hook_entity_update instead.

Other way to do it could be via hook_cron() and update your index with the cron system.

|improve this answer|||||
  • Does this hook_entity_insert(), runs after the node is saved in the database. ? – Adeel Sep 2 '14 at 15:05
  • Unfortunately not @Adeel - node_save() invokes this hook itself before the transaction has been committed. Using this hook won't make any difference. The cron job is probably the best way to go, unless you want to hack core and add a line to the end of node_save() (invoke a custom node_post_save() hook or something) – Clive Sep 2 '14 at 15:08
  • Actually both hook_node_inset and hook_entity_insert work after the node is inserted (drupal_write_record()) the issue might be on the cache system. That's the reason you don't see the node. As I said before a better solution is using the hook_cron. – killua99 Sep 2 '14 at 15:22
  • @Clive Thanks for the reply. I wonder why drupal does not give a hook for post node insert . To hack the core for a plugin is, obviously not the right way to go. – Adeel Sep 2 '14 at 15:28
  • 1
    Yeah it's always felt like an oversight - try the Hook Post Action module; I've never used it but it claims to add post update hooks for entities, exactly what you're looking for – Clive Sep 2 '14 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.