From f9fa50b6e85fddebc04aae93addf0c92a281d4a5 Mon Sep 17 00:00:00 2001
From: Klaus Purer <klaus.purer@gmail.com>
Date: Fri, 25 Feb 2011 15:22:11 +0100
Subject: [PATCH] Fixed entity_delete and entity_save actions for arbitrary entity types

---
 modules/data.eval.inc  |    7 +++-
 modules/data.rules.inc |   92 ++++++++++++++++++++++-------------------------
 modules/events.inc     |    3 +-
 3 files changed, 50 insertions(+), 52 deletions(-)

diff --git a/modules/data.eval.inc b/modules/data.eval.inc
index 557b382..0bfc867 100644
--- a/modules/data.eval.inc
+++ b/modules/data.eval.inc
@@ -172,7 +172,12 @@ function rules_action_entity_save($wrapper, $immediate = FALSE, $settings, $stat
  * Action: Delete entities.
  */
 function rules_action_entity_delete($wrapper, $settings, $state, $element) {
-  $wrapper->delete();
+  try {
+    $wrapper->delete();
+  }
+  catch (EntityMetadataWrapperException $e) {
+    throw new RulesException($e->getMessage(), array(), $element);
+  }
 }
 
 /**
diff --git a/modules/data.rules.inc b/modules/data.rules.inc
index eec36a4..192717e 100644
--- a/modules/data.rules.inc
+++ b/modules/data.rules.inc
@@ -139,57 +139,51 @@ function rules_data_action_info() {
     );
   }
 
-  // Get an array of types that are savable.
-  if ($types = array_keys(rules_entity_type_options('save'))) {
-    $return['entity_save'] = array(
-      'label' => t('Save entity'),
-      'parameter' => array(
-        'data' => array(
-          'type' => $types,
-          'label' => t('Entity'),
-          'description' => t('Specifies the entity, which should be saved permanently.'),
-          'restriction' => 'selector',
-          'wrapped' => TRUE,
-         ),
-         'immediate' => array(
-           'type' => 'boolean',
-           'label' => t('Force saving immediately'),
-           'description' => t('Usually saving is postponed till the end of the evaluation, so that multiple saves can be fold into one. If this set, saving is forced to happen immediately.'),
-           'default value' => FALSE,
-           'optional' => TRUE,
-           'restriction' => 'input',
-         ),
-      ),
-      'group' => t('Entities'),
-      'access callback' => 'rules_data_action_access',
-      'base' => 'rules_action_entity_save',
-      'callbacks' => array(
-        'access' => 'rules_action_entity_savedelete_access',
-      ),
-    );
-  }
+  $return['entity_save'] = array(
+    'label' => t('Save entity'),
+    'parameter' => array(
+      'data' => array(
+        'type' => 'entity',
+        'label' => t('Entity'),
+        'description' => t('Specifies the entity, which should be saved permanently.'),
+        'restriction' => 'selector',
+        'wrapped' => TRUE,
+       ),
+       'immediate' => array(
+         'type' => 'boolean',
+         'label' => t('Force saving immediately'),
+         'description' => t('Usually saving is postponed till the end of the evaluation, so that multiple saves can be fold into one. If this set, saving is forced to happen immediately.'),
+         'default value' => FALSE,
+         'optional' => TRUE,
+         'restriction' => 'input',
+       ),
+    ),
+    'group' => t('Entities'),
+    'access callback' => 'rules_data_action_access',
+    'base' => 'rules_action_entity_save',
+    'callbacks' => array(
+      'access' => 'rules_action_entity_savedelete_access',
+    ),
+  );
 
-  // Get an array of types that are deletable.
-  if ($types = array_keys(rules_entity_type_options('delete'))) {
-    $return['entity_delete'] = array(
-      'label' => t('Delete entity'),
-      'parameter' => array(
-        'data' => array(
-          'type' => $types,
-          'label' => t('Entity'),
-          'description' => t('Specifies the entity, which should be deleted permanently.'),
-          'restriction' => 'selector',
-          'wrapped' => TRUE,
-        ),
-      ),
-      'group' => t('Entities'),
-      'access callback' => 'rules_data_action_access',
-      'base' => 'rules_action_entity_delete',
-      'callbacks' => array(
-        'access' => 'rules_action_entity_savedelete_access',
+  $return['entity_delete'] = array(
+    'label' => t('Delete entity'),
+    'parameter' => array(
+      'data' => array(
+        'type' => 'entity',
+        'label' => t('Entity'),
+        'description' => t('Specifies the entity, which should be deleted permanently.'),
+        'restriction' => 'selector',
+        'wrapped' => TRUE,
       ),
-    );
-  }
+    ),
+    'group' => t('Entities'),
+    'access callback' => 'rules_data_action_access',
+    'base' => 'rules_action_entity_delete',
+    'callbacks' => array(
+      'access' => 'rules_action_entity_savedelete_access',
+    ),
+  );
   $return['list_add'] = array(
     'label' => t('Add list item'),
     'parameter' => array(
diff --git a/modules/events.inc b/modules/events.inc
index bd0a900..20cd93b 100644
--- a/modules/events.inc
+++ b/modules/events.inc
@@ -175,8 +175,7 @@ function rules_data_action_access($type, $name) {
     $op = $name == 'entity_create' ? 'create' : 'view';
   }
   elseif ($name == 'entity_save' || $name == 'entity_delete') {
-    $cache = rules_get_cache();
-    $types = $cache['action_info'][$name]['parameter']['data']['type'];
+    $types = array_keys(entity_get_info());
     $op = $name == 'entity_save' ? 'save' : 'delete';
   }
   foreach ($types as $key => $type) {
-- 
1.7.1

