You are here

function hook_commerce_bpc_get_combinations in Commerce Bulk Product Creation 7.2

Same name and namespace in other branches
  1. 7 commerce_bpc.api.php \hook_commerce_bpc_get_combinations()

Create combinations for fields that support it.

Modules implementing this hook will typically have implemented one of the hook_commerce_bpc_*_form_element_alter()-hooks to prepare some of the form elements to be usable for the creation of combinations.

This hook is invoked on form validation and submission to figure out for which combinations of values products should be created.

Parameters

array $form: The Form API representation of the form.

array $form_state: An array holding the current state of the form, in particular the submitted values.

array $combinations: An array holding the combinations that have been created so far. Each combination is an array keyed by field names with full field api value arrays as values (i.e. a nested array that accomodates languages and multiple values). The hook should take this parameter by reference and alter it. In the usual case, if a field managed by the implementing module has values that indicate that n variants should be created, the module should replace every member of $combinations with n copies of that member, one for each variant of the module. The first hook implementation that will be called will have a value of array(array()) for this parameter, meaning there is only one (empty) combination.

2 functions implement hook_commerce_bpc_get_combinations()

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

bpc_taxonomy_commerce_bpc_get_combinations in modules/bpc_taxonomy/bpc_taxonomy.module
Implements hook_commmerce_bpc_get_combinations().
commerce_bpc_commerce_bpc_get_combinations in ./commerce_bpc.hooks_list.inc
Implements hook_commmerce_bpc_get_combinations().

File

./commerce_bpc.api.php, line 247
This file contains no working PHP code; it exists to provide additional documentation for doxygen as well as to document hooks in the standard Drupal manner.

Code

function hook_commerce_bpc_get_combinations($form, &$form_state, &$combinations) {
  $new_combinations = array();
  $fields = $form_state['commerce_bpc']['list']['combination_fields'];
  foreach ($fields as $field_name) {
    $new_combinations = array();
    $langs = array_keys($form_state['values']['combinations'][$field_name]);
    $lang = reset($langs);
    $values = $form_state['values']['combinations'][$field_name][$lang];
    foreach ($combinations as $combination) {
      foreach ($values as $columns) {
        if (!list_field_is_empty($columns, $field_name)) {
          $new_combinations[] = $combination + array(
            $field_name => array(
              $lang => array(
                $columns,
              ),
            ),
          );
        }
        else {
          $new_combinations[] = $combination;

          // If the values for a field are empty, we make sure that
          // we (re)-add the original combination only once.
          break;
        }
      }
    }
    $combinations = $new_combinations;
  }
}