summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-11-30 19:31:47 +0000
committerDries Buytaert <dries@buytaert.net>2010-11-30 19:31:47 +0000
commit1fb5f62ba56bdbc6ff9f8a4045bccca8c5b9decd (patch)
tree2160f4d43f9da6385d200a04ee470c08b022c69e /includes
parent96b74a1594456dc1879df03222656e0dec815ad6 (diff)
downloadbrdo-1fb5f62ba56bdbc6ff9f8a4045bccca8c5b9decd.tar.gz
brdo-1fb5f62ba56bdbc6ff9f8a4045bccca8c5b9decd.tar.bz2
- Patch #651240 by fago, sun: allow modules to react to changes to an entity.
Diffstat (limited to 'includes')
-rw-r--r--includes/common.inc22
-rw-r--r--includes/entity.inc17
-rw-r--r--includes/file.inc6
3 files changed, 42 insertions, 3 deletions
diff --git a/includes/common.inc b/includes/common.inc
index 56e276980..972fc2319 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -7334,6 +7334,28 @@ function entity_load($entity_type, $ids = FALSE, $conditions = array(), $reset =
}
/**
+ * Loads the unchanged, i.e. not modified, entity from the database.
+ *
+ * Unlike entity_load() this function ensures the entity is directly loaded from
+ * the database, thus bypassing any static cache. In particular, this function
+ * is useful to determine changes by comparing the entity being saved to the
+ * stored entity.
+ *
+ * @param $entity_type
+ * The entity type to load, e.g. node or user.
+ * @param $id
+ * The id of the entity to load.
+ *
+ * @return
+ * The unchanged entity, or FALSE if the entity cannot be loaded.
+ */
+function entity_load_unchanged($entity_type, $id) {
+ entity_get_controller($entity_type)->resetCache(array($id));
+ $result = entity_get_controller($entity_type)->load(array($id));
+ return reset($result);
+}
+
+/**
* Get the entity controller class for an entity type.
*/
function entity_get_controller($entity_type) {
diff --git a/includes/entity.inc b/includes/entity.inc
index 1c3e7494e..5fb36e2dd 100644
--- a/includes/entity.inc
+++ b/includes/entity.inc
@@ -24,8 +24,12 @@ interface DrupalEntityControllerInterface {
/**
* Resets the internal, static entity cache.
+ *
+ * @param $ids
+ * (optional) If specified, the cache is reset for the entities with the
+ * given ids only.
*/
- public function resetCache();
+ public function resetCache(array $ids = NULL);
/**
* Loads one or more entities.
@@ -139,8 +143,15 @@ class DrupalDefaultEntityController implements DrupalEntityControllerInterface {
/**
* Implements DrupalEntityControllerInterface::resetCache().
*/
- public function resetCache() {
- $this->entityCache = array();
+ public function resetCache(array $ids = NULL) {
+ if (isset($ids)) {
+ foreach ($ids as $id) {
+ unset($this->entityCache[$id]);
+ }
+ }
+ else {
+ $this->entityCache = array();
+ }
}
/**
diff --git a/includes/file.inc b/includes/file.inc
index 742c20e72..e8db63448 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -574,6 +574,11 @@ function file_save(stdClass $file) {
$file->timestamp = REQUEST_TIME;
$file->filesize = filesize($file->uri);
+ // Load the stored entity, if any.
+ if (!empty($file->fid) && !isset($file->original)) {
+ $file->original = entity_load_unchanged('file', $file->fid);
+ }
+
if (empty($file->fid)) {
drupal_write_record('file_managed', $file);
// Inform modules about the newly added file.
@@ -587,6 +592,7 @@ function file_save(stdClass $file) {
module_invoke_all('entity_update', $file, 'file');
}
+ unset($file->original);
return $file;
}