diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-12-15 03:39:42 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-12-15 03:39:42 +0000 |
commit | 71c4e3ec62427e562965561f5a7b13aa64737595 (patch) | |
tree | 312ab891d1692d08008ab3bb20a8bb22f44d1f06 /modules | |
parent | 0afbd86d32a94c3b11c6cbe4204e66d8cfa1da3a (diff) | |
download | brdo-71c4e3ec62427e562965561f5a7b13aa64737595.tar.gz brdo-71c4e3ec62427e562965561f5a7b13aa64737595.tar.bz2 |
#968458 by Dave Reid, sun, fago: Add Missing hook_entity_presave().
Diffstat (limited to 'modules')
-rw-r--r-- | modules/comment/comment.module | 1 | ||||
-rw-r--r-- | modules/node/node.module | 1 | ||||
-rw-r--r-- | modules/simpletest/tests/entity_crud_hook_test.module | 53 | ||||
-rw-r--r-- | modules/simpletest/tests/entity_crud_hook_test.test | 24 | ||||
-rw-r--r-- | modules/system/system.api.php | 31 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.module | 2 | ||||
-rw-r--r-- | modules/user/user.module | 27 |
7 files changed, 133 insertions, 6 deletions
diff --git a/modules/comment/comment.module b/modules/comment/comment.module index d2286f333..e78f6ec5c 100644 --- a/modules/comment/comment.module +++ b/modules/comment/comment.module @@ -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. diff --git a/modules/node/node.module b/modules/node/node.module index 68b33781d..f52823c31 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -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 diff --git a/modules/simpletest/tests/entity_crud_hook_test.module b/modules/simpletest/tests/entity_crud_hook_test.module index 2d1b144c9..12252d144 100644 --- a/modules/simpletest/tests/entity_crud_hook_test.module +++ b/modules/simpletest/tests/entity_crud_hook_test.module @@ -2,6 +2,59 @@ // $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 // diff --git a/modules/simpletest/tests/entity_crud_hook_test.test b/modules/simpletest/tests/entity_crud_hook_test.test index 93f8c759f..17b9f23b1 100644 --- a/modules/simpletest/tests/entity_crud_hook_test.test +++ b/modules/simpletest/tests/entity_crud_hook_test.test @@ -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'); diff --git a/modules/system/system.api.php b/modules/system/system.api.php index 3d73c0647..aed02ae2c 100644 --- a/modules/system/system.api.php +++ b/modules/system/system.api.php @@ -281,6 +281,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. * * @param $entity @@ -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) { } diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index a78436e15..d7417928e 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -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'; diff --git a/modules/user/user.module b/modules/user/user.module index 7682f2522..d464a7a7b 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -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)) { |