entity.eval.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @file
  4. * Contains rules integration for entities needed during evaluation.
  5. *
  6. * @addtogroup rules
  7. *
  8. * @{
  9. */
  10. /**
  11. * Action: Fetch data.
  12. */
  13. function rules_action_entity_fetch($type, $id, $revision) {
  14. $info = entity_get_info($type);
  15. // Support the revision parameter, if applicable.
  16. if (!empty($info['entity keys']['revision']) && isset($revision)) {
  17. $conditions = array($info['entity keys']['revision'] => $revision);
  18. }
  19. $return = entity_load($type, array($id), isset($conditions) ? $conditions : array());
  20. $entity = reset($return);
  21. if (!$entity) {
  22. throw new RulesEvaluationException('Unable to load @entity with id "@id"', array('@id' => $id, '@entity' => $type));
  23. }
  24. return array('entity_fetched' => $entity);
  25. }
  26. /**
  27. * Info alteration callback for the entity fetch action.
  28. */
  29. function rules_action_entity_fetch_info_alter(&$element_info, RulesAbstractPlugin $element) {
  30. $element->settings += array('type' => NULL);
  31. $info = entity_get_info($element->settings['type']);
  32. // Fix the type of the identifier.
  33. $element_info['parameter']['id']['type'] = isset($info['entity keys']['name']) ? 'text' : 'integer';
  34. // Add an optional revision parameter, if supported.
  35. if (!empty($info['entity keys']['revision'])) {
  36. $element_info['parameter']['revision_id'] = array(
  37. 'type' => 'integer',
  38. 'label' => t('Revision identifier'),
  39. 'optional' => TRUE,
  40. );
  41. }
  42. $element_info['provides']['entity_fetched']['type'] = $element->settings['type'];
  43. }
  44. /**
  45. * Action: Query entities.
  46. */
  47. function rules_action_entity_query($type, $property, $value, $limit) {
  48. $return = entity_property_query($type, $property, $value, $limit);
  49. return array('entity_fetched' => array_values($return));
  50. }
  51. /**
  52. * Info alteration callback for the entity query action.
  53. */
  54. function rules_action_entity_query_info_alter(&$element_info, RulesAbstractPlugin $element) {
  55. $element->settings += array('type' => NULL, 'property' => NULL);
  56. if ($element->settings['type']) {
  57. $element_info['parameter']['property']['options list'] = 'rules_action_entity_query_property_options_list';
  58. if ($element->settings['property']) {
  59. $wrapper = rules_get_entity_metadata_wrapper_all_properties($element);
  60. if (isset($wrapper->{$element->settings['property']}) && $property = $wrapper->{$element->settings['property']}) {
  61. $property_type = $property->type();
  62. // If the cardinality of the property > 1, i.e. of type 'list<{type}>',
  63. // we will also accept a parameter of type {type}.
  64. if (substr($property_type, 0, strlen('list<')) === 'list<' && substr($property_type, -strlen('>')) === '>') {
  65. $property_type = array($property_type, substr($property_type, strlen('list<'), strlen($property_type) - strlen('list<>')));
  66. }
  67. $element_info['parameter']['value']['type'] = $property_type;
  68. $element_info['parameter']['value']['options list'] = $property->optionsList() ? 'rules_action_entity_query_value_options_list' : FALSE;
  69. }
  70. }
  71. }
  72. $element_info['provides']['entity_fetched']['type'] = 'list<' . $element->settings['type'] . '>';
  73. }
  74. /**
  75. * Action: Create entities.
  76. */
  77. function rules_action_entity_create($args, $element) {
  78. $values = array();
  79. foreach ($element->pluginParameterInfo() as $name => $info) {
  80. if ($name != 'type') {
  81. // Remove the parameter name prefix 'param_'.
  82. $values[substr($name, 6)] = $args[$name];
  83. }
  84. }
  85. try {
  86. $data = entity_property_values_create_entity($args['type'], $values);
  87. return array('entity_created' => $data);
  88. }
  89. catch (EntityMetadataWrapperException $e) {
  90. throw new RulesEvaluationException('Unable to create entity @type": ' . $e->getMessage(), array('@type' => $args['type']), $element);
  91. }
  92. }
  93. /**
  94. * Info alteration callback for the entity create action.
  95. */
  96. function rules_action_entity_create_info_alter(&$element_info, RulesAbstractPlugin $element) {
  97. if (!empty($element->settings['type']) && entity_get_info($element->settings['type'])) {
  98. $wrapper = entity_metadata_wrapper($element->settings['type']);
  99. // Add the data type's needed parameter for loading to the parameter info.
  100. foreach ($wrapper as $name => $child) {
  101. $info = $child->info();
  102. if (!empty($info['required'])) {
  103. $info += array('type' => 'text');
  104. // Prefix parameter names to avoid name clashes
  105. // with existing parameters.
  106. $element_info['parameter']['param_' . $name] = array_intersect_key($info, array_flip(array('type', 'label', 'description')));
  107. $element_info['parameter']['param_' . $name]['options list'] = $child->optionsList() ? 'rules_action_entity_parameter_options_list' : FALSE;
  108. }
  109. }
  110. $element_info['provides']['entity_created']['type'] = $element->settings['type'];
  111. if (($bundleKey = $wrapper->entityKey('bundle')) && isset($element->settings['param_' . $bundleKey])) {
  112. $element_info['provides']['entity_created']['bundle'] = $element->settings['param_' . $bundleKey];
  113. }
  114. }
  115. }
  116. /**
  117. * Action: Save entities.
  118. */
  119. function rules_action_entity_save($wrapper, $immediate = FALSE, $settings, $state, $element) {
  120. $state->saveChanges($settings['data:select'], $wrapper, $immediate);
  121. }
  122. /**
  123. * Action: Delete entities.
  124. */
  125. function rules_action_entity_delete($wrapper, $settings, $state, $element) {
  126. try {
  127. $wrapper->delete();
  128. }
  129. catch (EntityMetadataWrapperException $e) {
  130. throw new RulesEvaluationException($e->getMessage(), array(), $element);
  131. }
  132. }
  133. /**
  134. * Condition: Entity is new.
  135. */
  136. function rules_condition_entity_is_new($wrapper, $settings, $state, $element) {
  137. return !$wrapper->getIdentifier() || !empty($wrapper->value()->is_new);
  138. }
  139. /**
  140. * Condition: Entity has field.
  141. */
  142. function rules_condition_entity_has_field($wrapper, $field_name, $settings, $state) {
  143. return isset($wrapper->$field_name) || isset($wrapper->value()->$field_name);
  144. }
  145. /**
  146. * Condition: Entity is of type.
  147. */
  148. function rules_condition_entity_is_of_type($wrapper, $type) {
  149. return $wrapper->type() == $type;
  150. }
  151. /**
  152. * Condition: Entity is of type and bundle.
  153. */
  154. function rules_condition_entity_is_of_bundle($wrapper, $type, $bundles) {
  155. return $wrapper->type() == $type && in_array($wrapper->getBundle(), $bundles);
  156. }
  157. /**
  158. * Condition: User has access to field.
  159. */
  160. function rules_condition_entity_field_access(EntityDrupalWrapper $wrapper, $field_name, $op, $account = NULL) {
  161. $field = field_info_field($field_name);
  162. return !empty($field) && field_access($op, $field, $wrapper->type(), $wrapper->value(), $account = NULL);
  163. }
  164. /**
  165. * @}
  166. */