You are here

function _entity_rules_invoke_rules in Entity Rules 7

Invokes Rules for an entity given an operation.

_type

Parameters

$entity:

$op:

Return value

Array for values returned from invoked Rules

5 calls to _entity_rules_invoke_rules()
entity_rules_entity_delete in ./entity_rules.module
Implements hook_entity_delete().
entity_rules_entity_form_validation in ./entity_rules.module
Validation callback for forms to be validated via Rules.
entity_rules_entity_insert in ./entity_rules.module
Implements hook_entity_insert().
entity_rules_entity_update in ./entity_rules.module
Implements hook_entity_update().
entity_rules_form_alter in ./entity_rules.module
Implements hook_form_alter().

File

./entity_rules.module, line 929
Module file for the Entity Rules.

Code

function _entity_rules_invoke_rules($entity, $entity_type, $op) {
  $return_values = array();
  $ids = entity_extract_ids($entity_type, $entity);
  $bundle = array_pop($ids);
  $rule_settings = entity_rules_load_settings_for_op($entity_type, $bundle, $op, TRUE);
  foreach ($rule_settings as $invoke_settings) {

    // Only invoke if Rules exists.
    // rules_invoke_component will always return false if the Rule doesn't exist.
    $rule_name = $invoke_settings->loaded_rules_config->name;
    if (rules_get_cache('comp_' . $rule_name)) {
      $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
      $args = array(
        $entity_wrapper,
      );
      $conditional = is_subclass_of($invoke_settings->loaded_rules_config, 'RulesConditionContainer');
      $default_rule_args = _entity_rules_get_var_items($op, $conditional);
      foreach ($default_rule_args as $default_rule_arg) {
        $args[] = $default_rule_arg['entity_rules_settings']['default_value'];
      }
      $vars = _entity_rules_get_extra_vars($invoke_settings->loaded_rules_config, $op);
      foreach ($vars as $var_name => $var_info) {
        if (isset($invoke_settings->args[$var_name])) {
          $args[] = $invoke_settings->args[$var_name];
        }
        else {
          $args[] = NULL;
        }
      }
      $return_value = entity_rules_invoke_component($rule_name, $args);

      // For condition componenents display message on FALSE
      if (is_subclass_of($invoke_settings->loaded_rules_config, 'RulesConditionContainer')) {
        if ($return_value === FALSE && !empty($invoke_settings->false_msg['value'])) {
          _entity_rules_show_message($invoke_settings->false_msg, $entity, $entity_type);
        }
      }
      $return_values[] = $return_value;

      // Abort at first Rule that doesn't pass or sets continue FALSE
      if ($return_value === FALSE || is_array($return_value) && isset($return_value[0]) && $return_value[0] == FALSE) {
        break;
      }
    }
    else {

      // Warn that Rule doesn't exist
      $msg = t('Entityform Type %form references non-existing Rule component %component', array(
        '%form' => $entityform->type,
        '%component' => $rule_name,
      ));
      watchdog('entityform', $msg);
      if (user_access('administer entityform types')) {

        // Warn on screen if user can fix this.
        drupal_set_message($msg, 'warning');
      }
    }
  }
  return $return_values;
}