Commit 71c4e3ec authored by webchick's avatar webchick

#968458 by Dave Reid, sun, fago: Add Missing hook_entity_presave().

parent 0afbd86d
......@@ -579,6 +579,9 @@ function file_save(stdClass $file) {
$file->original = entity_load_unchanged('file', $file->fid);
}
module_invoke_all('file_presave', $file);
module_invoke_all('entity_presave', $file, 'file');
if (empty($file->fid)) {
drupal_write_record('file_managed', $file);
// Inform modules about the newly added file.
......
......@@ -1452,6 +1452,7 @@ function comment_save($comment) {
// Allow modules to alter the comment before saving.
module_invoke_all('comment_presave', $comment);
module_invoke_all('entity_presave', $comment, 'comment');
if ($comment->cid) {
// Update the comment in the database.
......
......@@ -1058,6 +1058,7 @@ function node_save($node) {
// Let modules modify the node before it is saved to the database.
module_invoke_all('node_presave', $node);
module_invoke_all('entity_presave', $node, 'node');
if ($node->is_new || !empty($node->revision)) {
// When inserting either a new node or a new node revision, $node->log
......
<?php
// $Id$
//
// Presave hooks
//
/**
* Implements hook_entity_presave().
*/
function entity_crud_hook_test_entity_presave($entity, $type) {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called for type ' . $type);
}
/**
* Implements hook_comment_presave().
*/
function entity_crud_hook_test_comment_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_file_presave().
*/
function entity_crud_hook_test_file_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_node_presave().
*/
function entity_crud_hook_test_node_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_taxonomy_term_presave().
*/
function entity_crud_hook_test_taxonomy_term_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_taxonomy_vocabulary_presave().
*/
function entity_crud_hook_test_taxonomy_vocabulary_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
/**
* Implements hook_user_presave().
*/
function entity_crud_hook_test_user_presave() {
$_SESSION['entity_crud_hook_test'][] = (__FUNCTION__ . ' called');
}
//
// Insert hooks
//
......
......@@ -81,6 +81,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
comment_save($comment);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
$this->assertHookMessage('entity_crud_hook_test_comment_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type comment');
$this->assertHookMessage('entity_crud_hook_test_comment_insert called');
......@@ -94,6 +96,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$comment->subject = 'New subject';
comment_save($comment);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type comment');
$this->assertHookMessage('entity_crud_hook_test_comment_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type comment');
$this->assertHookMessage('entity_crud_hook_test_comment_update called');
......@@ -123,6 +127,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
file_save($file);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
$this->assertHookMessage('entity_crud_hook_test_file_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type file');
$this->assertHookMessage('entity_crud_hook_test_file_insert called');
......@@ -136,6 +142,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$file->filename = 'new.entity_crud_hook_test.file';
file_save($file);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type file');
$this->assertHookMessage('entity_crud_hook_test_file_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type file');
$this->assertHookMessage('entity_crud_hook_test_file_update called');
......@@ -165,6 +173,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
node_save($node);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
$this->assertHookMessage('entity_crud_hook_test_node_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type node');
$this->assertHookMessage('entity_crud_hook_test_node_insert called');
......@@ -178,6 +188,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$node->title = 'New title';
node_save($node);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type node');
$this->assertHookMessage('entity_crud_hook_test_node_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type node');
$this->assertHookMessage('entity_crud_hook_test_node_update called');
......@@ -209,6 +221,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
taxonomy_term_save($term);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_term');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_term_insert called');
......@@ -222,6 +236,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$term->name = 'New name';
taxonomy_term_save($term);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_term');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_term_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_term');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_term_update called');
......@@ -245,6 +261,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
taxonomy_vocabulary_save($vocabulary);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type taxonomy_vocabulary');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_insert called');
......@@ -258,6 +276,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$vocabulary->name = 'New name';
taxonomy_vocabulary_save($vocabulary);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type taxonomy_vocabulary');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type taxonomy_vocabulary');
$this->assertHookMessage('entity_crud_hook_test_taxonomy_vocabulary_update called');
......@@ -283,6 +303,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$_SESSION['entity_crud_hook_test'] = array();
$account = user_save($account, $edit);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
$this->assertHookMessage('entity_crud_hook_test_user_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_insert called for type user');
$this->assertHookMessage('entity_crud_hook_test_user_insert called');
......@@ -296,6 +318,8 @@ class EntityCrudHookTestCase extends DrupalWebTestCase {
$edit['name'] = 'New name';
$account = user_save($account, $edit);
$this->assertHookMessage('entity_crud_hook_test_entity_presave called for type user');
$this->assertHookMessage('entity_crud_hook_test_user_presave called');
$this->assertHookMessage('entity_crud_hook_test_entity_update called for type user');
$this->assertHookMessage('entity_crud_hook_test_user_update called');
......
......@@ -280,6 +280,18 @@ function hook_entity_load($entities, $type) {
}
}
/**
* Act on an entity before it is about to be created or updated.
*
* @param $entity
* The entity object.
* @param $type
* The type of entity being saved (i.e. node, user, comment).
*/
function hook_entity_presave($entity, $type) {
$entity->changed = REQUEST_TIME;
}
/**
* Act on entities when inserted.
*
......@@ -2545,7 +2557,7 @@ function hook_file_validate(&$file) {
}
/**
* Respond to a file being added.
* Act on a file being inserted or updated.
*
* This hook is called when a file has been added to the database. The hook
* doesn't distinguish between files created as a result of a copy or those
......@@ -2556,6 +2568,23 @@ function hook_file_validate(&$file) {
*
* @see file_save()
*/
function hook_file_presave($file) {
// Change the file timestamp to an hour prior.
$file->timestamp -= 3600;
}
/**
* Respond to a file being added.
*
* This hook is called before a file has been added to the database. The hook
* doesn't distinguish between files created as a result of a copy or those
* created by an upload.
*
* @param $file
* The file that is about to be saved.
*
* @see file_save()
*/
function hook_file_insert($file) {
}
......
......@@ -404,6 +404,7 @@ function taxonomy_vocabulary_save($vocabulary) {
}
module_invoke_all('taxonomy_vocabulary_presave', $vocabulary);
module_invoke_all('entity_presave', $vocabulary, 'taxonomy_vocabulary');
if (!empty($vocabulary->vid) && !empty($vocabulary->name)) {
$status = drupal_write_record('taxonomy_vocabulary', $vocabulary, 'vid');
......@@ -554,6 +555,7 @@ function taxonomy_term_save($term) {
field_attach_presave('taxonomy_term', $term);
module_invoke_all('taxonomy_term_presave', $term);
module_invoke_all('entity_presave', $term, 'taxonomy_term');
if (empty($term->tid)) {
$op = 'insert';
......
......@@ -397,6 +397,8 @@ function user_load_by_name($name) {
*
* @return
* A fully-loaded $user object upon successful save or FALSE if the save failed.
*
* @todo D8: Drop $edit and fix user_save() to be consistent with others.
*/
function user_save($account, $edit = array(), $category = 'account') {
$transaction = db_transaction();
......@@ -422,11 +424,6 @@ function user_save($account, $edit = array(), $category = 'account') {
$account->original = entity_load_unchanged('user', $account->uid);
}
// Presave field allowing changing of $edit.
$edit = (object) $edit;
field_attach_presave('user', $edit);
$edit = (array) $edit;
if (empty($account)) {
$account = new stdClass();
}
......@@ -438,8 +435,28 @@ function user_save($account, $edit = array(), $category = 'account') {
if (!empty($account->data)) {
$edit['data'] = !empty($edit['data']) ? array_merge($account->data, $edit['data']) : $account->data;
}
// Invoke hook_user_presave() for all modules.
user_module_invoke('presave', $edit, $account, $category);
// Invoke presave operations of Field Attach API and Entity API. Those APIs
// require a fully-fledged (and updated) entity object, so $edit is not
// necessarily sufficient, as it technically contains submitted form values
// only. Therefore, we need to clone $account into a new object and copy any
// new property values of $edit into it.
$account_updated = clone $account;
foreach ($edit as $key => $value) {
$account_updated->$key = $value;
}
field_attach_presave('user', $account_updated);
module_invoke_all('entity_presave', $account_updated, 'user');
// Update $edit with any changes modules might have applied to the account.
foreach ($account_updated as $key => $value) {
if (!property_exists($account, $key) || $value !== $account->$key) {
$edit[$key] = $value;
}
}
if (is_object($account) && !$account->is_new) {
// Process picture uploads.
if (!$delete_previous_picture = empty($edit['picture']->fid)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment