0

I placed the following function in my custom module.

function MYMODULE_node_update($node) {
  if ($node->type === 'page') {
    $node->title = 'test';
  }
}

When editing and saving a node of content type page nothing happens. What can be the issue?


Problem solved. It seemed to be caused by the following:

my custom module wasn't ending with ?> and above all my module name was custom_site.module and my hooks started with custom. This didn't give problems before with my other hooks so I never noticed. (This helped: https://drupal.stackexchange.com/a/111272/85560)

My module name now is custom.module and now custom_node_presave() works.

5
  • node_save($node); right after $node->title = 'test';. And flush cache. But maybe better rely on hook_node_insert. Not sure.
    – leymannx
    Jan 14, 2020 at 16:05
  • 1
    No need to call node_save() in a hook_node_update() as the hook is invoked from the node_save function itself. api.drupal.org/api/drupal/modules%21node%21node.api.php/…
    – XenoVkl
    Jan 14, 2020 at 16:21
  • The hook name would be custom_site_ and not custom_. I would advise devising a better naming strategy.
    – Kevin
    Jan 15, 2020 at 19:47
  • Add your question update as answer so other can benefict from your findings. Thanks!
    – d70rr3s
    Jan 16, 2020 at 8:52
  • I am actually already using another more identic name for custom. I just used it as an example. My module name is MYMODULE.module and the hook name is MYMODULE_node_presave().
    – sndr
    Jan 16, 2020 at 9:06

1 Answer 1

2

You should use hook_node_presave if you want to change a field's value as the hook is invoked from node_save() before the node is saved to the database. And as leymannx stated, make sure you clear the cache before implementing a new hook.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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