You are here

function hook_commerce_bpc_form_element_alter in Commerce Bulk Product Creation 7

Same name and namespace in other branches
  1. 7.2 commerce_bpc.api.php \hook_commerce_bpc_form_element_alter()

Perform alterations on a field form element of the bulk product form.

This hook is called during the generation of the bulk create from, once for every field attached to the product type for which bulk products are being created.

This hook is mainly provided so that modules do not need to loop through the whole form array to do their field-specific alterations. The whole form and form_state are passed, as a module may want to move form elements to another place in the form. In most cases, it will be a good idea to either use the field type specific hook hook_commerce_bpc_FIELD_TYPE_form_element_alter(), or the field type module specific version hook_commerce_bpc_MODULE_NAME_form_element_alter().

A module that enables a field type to be used in the creation of combinations (such as list fields) should use this hook to move the form element to the 'combinations' fieldset of $form and perform any necessary alterations on the form fields.

In general, modules that move form elements out of $form['static_values'] should keep a record of what they did (by convention, in $form_state['commerce_bpc'][MODULE_NAME]), so they can act on these items at form validation and submission.

Care has to be taken, as this hook (and its more specific variants) can be called multiple times in case the form is rebuilt (e.g. after AJAX calls). Thus in recording which fields have been altered in form_state, the implementation should make sure it does not record things twice.

Parameters

array $form: Nested array of form elements that comprise the bulk creation form.

array $form_state: A keyed array containing the current state of the form.

array $path: An array of keys specifying where in the form the current element is to be found. This should be reset if the element is moved.

See also

hook_commerce_bpc_MODULE_NAME_form_element_alter()

hook_commerce_bpc_FIELD_TYPE_form_element_alter()

File

./commerce_bpc.api.php, line 73
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_form_element_alter(&$form, &$form_state, &$path) {
  $element = drupal_array_get_nested_value($form, $path);
  $lang = $element['#language'];
  $field_name = $element[$lang]['#field_name'];
  $instance = field_info_instance('commerce_product', $field_name, $form['product_type']['#value']);
  if (commerce_bpc_commerce_bpc_is_combination_field($instance)) {
    $element[$lang]['#type'] = 'checkboxes';

    // Get rid of 'none' option---user can just not pick any.
    unset($element[$lang]['#options']['_none']);

    // Move to comibinations-fieldset
    $form['combinations'][$field_name] = $element;
    drupal_array_set_nested_value($form, $path, NULL);

    // Change path to allow subsequent hooks operate on the form element.
    $path = array(
      'combinations',
      $field_name,
    );
    if (empty($form_state['commerce_bpc']['list']['combination_fields']) || !in_array($field_name, $form_state['commerce_bpc']['list']['combination_fields'])) {

      // Record what we have done. As this hook may be run multiple times
      // due to form rebuilds, we need to make sure that we record each field
      // only once.
      $form_state['commerce_bpc']['list']['combination_fields'][] = $field_name;
    }
  }
}