You are here

function inline_conditions_build in Inline Conditions 7

Defines a callback to add condition(s) to the given rule.

When a rule is being built, it goes over the $field_values, and for each condition it calls the conditions "build" function.

Parameters

RulesReactionRule $rule: The parent rule.

array $field_values: An array of values from an inline_conditions field.

See also

hook_inline_conditions_build_alter()

File

./inline_conditions.module, line 343
Extends Drupal 7 with a new field type to manage rules conditions directly from a field.

Code

function inline_conditions_build(RulesReactionRule $rule, $field_values) {
  if (!empty($field_values)) {

    // Initialising the variables needed to later build the rule.
    $or = $and = $temp = NULL;

    // Loop over field values.
    foreach ($field_values as $delta => $value) {

      // Give a chance to others module to alter the current field value.
      drupal_alter('inline_conditions_build', $value);

      // Get the condition info.
      $info = inline_conditions_get_info($value['condition_name']);

      // Ensure we got the condition and we have settings for the rule
      // condition.
      if (!$info || empty($value['condition_settings'])) {
        continue;
      }

      // Fulfill the parameters variable with the expecting values.
      $parameters = array(
        'entity:select' => $info['entity type'],
      ) + $value['condition_settings'];

      // Find the condition name in order to be attached on the passed rule.
      $name = isset($info['rule condition name']) ? $info['rule condition name'] : $value['condition_name'];
      $condition = rules_condition($name, $parameters)
        ->negate(!empty($value['condition_negate']));

      // Find out if we need to add a OR / AND condition before the one defined
      // in the current field value.
      if (isset($value['condition_logic_operator'])) {
        switch ($value['condition_logic_operator']) {
          case INLINE_CONDITIONS_AND:
            if (is_null($and)) {
              $and = rules_and();
            }

            // Attach the condition in the "AND" group.
            $condition
              ->setParent($and);

            // Try to add the condition stored in temp variable in the current
            // group.
            if (isset($temp)) {
              $temp
                ->setParent($and);
              unset($temp);
            }
            break;
          case INLINE_CONDITIONS_OR:
            if (is_null($or)) {
              $or = rules_or();
            }

            // Attach the condition in the "OR" group.
            $condition
              ->setParent($or);

            // Try to add the condition stored in temp variable in the current
            // group.
            if (isset($temp)) {
              $temp
                ->setParent($or);
              unset($temp);
            }
            break;
        }
        continue;
      }

      // No logical operator found, so we put the condition in the temp array.
      $temp = $condition;
    }

    // Add conditions based on logical operators groups to passed rule.
    if (!is_null($and)) {
      $rule
        ->condition($and);
    }
    if (!is_null($or)) {
      $rule
        ->condition($or);
    }

    // If a condition is still present in the temp var, attach it to the rule
    // using an AND operator.
    if (isset($temp)) {
      $rule
        ->condition($temp);
    }
  }
}