You are here

function uc_dropdown_attributes_product_alter in Dropdown Attributes 7

Same name and namespace in other branches
  1. 8 uc_dropdown_attributes.module \uc_dropdown_attributes_product_alter()
  2. 6 uc_dropdown_attributes.module \uc_dropdown_attributes_product_alter()

Alter products in preparation for drop down attributes.

Adds the 'Please select' and removes the default value. Ubercart does this for required attributes but since these attributes can no longer be required if the attributes are dependent then this reproduces the same thing.

Parameters

int $nid: Node ID.

array $form: Product form.

array $form_values: Values from $form_state.

string $type: 'node' for dependencies defined on the node level; 'class' for dependencies defined on the product class.

bool $rebuild: Whether the form is being rebuilt.

1 call to uc_dropdown_attributes_product_alter()
uc_dropdown_attributes_form_alter in ./uc_dropdown_attributes.module
Implements hook_form_alter().

File

./uc_dropdown_attributes.module, line 125
Show/hide attributes based on the values of other attributes.

Code

function uc_dropdown_attributes_product_alter($nid, &$form, $form_values, $type, $rebuild) {
  switch ($type) {
    case 'node':
      $sql = 'SELECT aid, parent_aid, parent_values, required
        FROM {uc_dropdown_attributes}
        WHERE nid=:nid';
      $attributes = db_query($sql, array(
        ':nid' => $nid,
      ));
      break;
    case 'class':
      $sql = 'SELECT aid, parent_aid, parent_values, required
        FROM {uc_dropdown_classes}
        WHERE pcid=:pcid';
      $pcid = uc_dropdown_attributes_get_type($nid);
      $attributes = db_query($sql, array(
        ':pcid' => $pcid,
      ));
      break;
  }
  $parent_aids = array();
  if (!$rebuild) {
    $data = $form['node']['#value']->data;
  }
  $load = FALSE;
  foreach ($attributes as $attribute) {
    $parent_aids[$attribute->parent_aid] = $attribute->parent_aid;
    if (isset($form['attributes'][$attribute->aid]['#options']) && count($form['attributes'][$attribute->aid]['#options']) && $attribute->required) {
      switch ($form['attributes'][$attribute->aid]['#type']) {
        case 'select':
          $form['attributes'][$attribute->aid]['#options'] = array(
            '' => t('Please select'),
          ) + $form['attributes'][$attribute->aid]['#options'];
          $form['attributes'][$attribute->aid]['#default_value'] = '';
          if (!$rebuild && isset($data['attributes'][$attribute->aid])) {
            $data['attributes'][$attribute->aid] = '';
            $load = TRUE;
          }
          break;
        case 'radios':
          $form['attributes'][$attribute->aid]['#default_value'] = NULL;
          if (!$rebuild && isset($data['attributes'][$attribute->aid])) {
            $data['attributes'][$attribute->aid] = '';
            $load = TRUE;
          }

          // Validation does not work if required set later.
          if (isset($form_values['attributes'][$attribute->parent_aid])) {
            $parent_value = $form_values['attributes'][$attribute->parent_aid];
            $values = unserialize($attribute->parent_values);
            $parent_aid = $attribute->parent_aid;
            if (in_array($parent_value, $values)) {
              $form['attributes'][$attribute->aid]['#required'] = TRUE;
            }
          }
          break;
        case 'checkboxes':
          $form['attributes'][$attribute->aid]['#default_value'] = array();
          if (!$rebuild && isset($data['attributes'][$attribute->aid])) {
            $data['attributes'][$attribute->aid] = array();
            $load = TRUE;
          }
          break;
      }
    }
  }
  if ($load) {
    $form['node']['#value'] = uc_product_load_variant($nid, $data);
  }
  $update_node = variable_get('uc_product_update_node_view', 0);
  if (!$update_node) {

    // If Ubercart update is not enabled then ajax needs to be attached to
    // parent attributes.
    foreach ($parent_aids as $aid) {
      $form['attributes'][$aid]['#ajax'] = array(
        'callback' => 'uc_dropdown_attributes_ajax_callback',
        'wrapper' => $form['attributes']['#id'],
      );
    }
  }
}