entity.eval.inc 6.0 KB

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