| 7 node.api.php | hook_node_presave($node) |
| 8 node.api.php | hook_node_presave(Drupal node Node $node) |
Act on a node being inserted or updated.
This hook is invoked from node_save() before the node is saved to the database.
Parameters
$node: The node that is being inserted or updated.
Related topics
5 functions implement hook_node_presave()
1 invocation of hook_node_presave()
File
- modules/
node/ node.api.php, line 675 - Hooks provided by the Node module.
Code
function hook_node_presave($node) {
if ($node->nid && $node->moderate) {
// Reset votes when node is updated:
$node->score = 0;
$node->users = '';
$node->votes = 0;
}
}
Login or register to post comments
Comments
original node
You can access the original contents of the node at:
<?php$node->original
?>
Rename the File field name
I have created a field in a content type and added a image field with that. After this i created a node for this and uploaded a image in image field. Now i want to change the filename of image using my custom module function like
custom_changes_node_presave($node). Exactly i want to replace the space from filename while saving the node. My code is mentioned below. Right now i am not able to rename the filename of image.
<?php
function custom_changes_node_presave($node){
if ($node->type ==='designer_background_images'){
$filename =$node->original->field_db_image['und']['0']['filename'];
$uri =$node->original->field_db_image['und']['0']['uri'];
$filename = str_replace (" ", "_", $filename);
$uri = str_replace (" ", "_", $uri);
$node->original->field_db_image['und']['0']['filename']= $filename;
$node->original->field_db_image['und']['0']['uri'] = $uri;
//field_attach_presave('node', $node);
// And the actual field change for the given node (e.g. entity).
//field_attach_update('node', $node);
// echo "<pre>";
// print_r($node);
// exit;
// print $node->original->field_db_image['und']['0']['filename'];
// print $node->original->field_db_image['und']['0']['uri'];
}
}
?>
use $node instead of $node->original?
To change the new value, shouldn't you be using:
$node->field_db_image['und']['0']['filename'] = $filename;Hi, I need a function like
Hi,
I need a function like this one, but i don't know where i have to put it.
Where did you put this code?
Thanks!
It goes in a custom module
IN the case above, the module is called custome_changes.
can we redirect from normal node/[nid] page
How can we redirect from the default results page after editing a node?
Something like $form['#redirect'] = 'redirect/path' ?
Use form alter
To updated the form state you need to use the hook_form_alter or hook_form_FORM_ID_alter to add an additional submit handler. An example:
function example_form_node_form_alter(&$form, &$form_state, $form_id) {
$form['#submit'][] = 'example_node_form_submit_redirect';
}
function example_node_form_submit_redirect( $form, &$form_state ){
$form_state['redirect'] = "redirect/after_submit";
}