You are here

function hook_inline_conditions_build_alter in Inline Conditions 7

Alter a field value, just before building a rules condition from it. Specifically: if the structure of the 'condition_settings' array as saved in the field does not exactly match the parameters expected by the corresponding rules condition, this hook allows transforming the former into the latter.

Parameters

array $value: A single field value from an "inline_conditions" type field. This means it typically contains two keys:

  • 'condition_name': the condition name as defined in hook_inline_conditions_info() and hook_rules_condition_info().
  • 'condition_settings': an array containing all configured settings; typically this will match the values of the form elements defined in the 'configure' callback. It should be transformed into an array of parameter values as the rules condition needs them. (Not including the line item.) Your 'build' callback will be passed the same parameter values.

See also

inline_conditions_build().

1 function implements hook_inline_conditions_build_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

inline_conditions_inline_conditions_build_alter in ./inline_conditions.inline_conditions.inc
Implements hook_inline_conditions_build_alter().
1 invocation of hook_inline_conditions_build_alter()
inline_conditions_build in ./inline_conditions.module
Defines a callback to add condition(s) to the given rule.

File

./inline_conditions.api.php, line 94
Hooks provided by the Inline Conditions module.

Code

function hook_inline_conditions_build_alter(&$value) {
  if ($value['condition_name'] == 'commerce_order_contains_products') {

    // 'products' is a text field in the configure form: comma-separated SKUs.
    // It has a #validate function that turns submitted input into form value:
    //     array( array('product_id' => P1), array('product_id' => P2), ...)
    // so that's how it gets stored into 'condition_settings'.
    // Load the products...
    $entity_ids = array();
    foreach ($value['condition_settings']['products'] as $delta) {
      $entity_ids[] = reset($delta);
    }
    $products = commerce_product_load_multiple($entity_ids);

    // ...so we can turn it back into a comma-separated string of SKUs.
    $value['condition_settings']['products'] = '';
    foreach ($products as $product) {
      $value['condition_settings']['products'] .= $product->sku;
      if ($product !== end($products)) {
        $value['condition_settings']['products'] .= ', ';
      }
    }
  }
}