You are here

function _uc_dropdown_attributes_product_build in Dropdown Attributes 7

Same name and namespace in other branches
  1. 8 uc_dropdown_attributes.module \_uc_dropdown_attributes_product_build()

Form build for products.

Callback for $form['#after_build'] for products. Adds the CSS to hide the dependent attributes.

1 string reference to '_uc_dropdown_attributes_product_build'
uc_dropdown_attributes_form_alter in ./uc_dropdown_attributes.module
Implements hook_form_alter().

File

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

Code

function _uc_dropdown_attributes_product_build($form, &$form_state) {
  $nid = $form['nid']['#value'];
  $sql = 'SELECT aid, parent_aid, parent_values, required
    FROM {uc_dropdown_attributes} WHERE nid=:nid';
  $attributes = db_query($sql, array(
    ':nid' => $nid,
  ));
  if (isset($form_state['triggering_element'])) {
    $parents = $form_state['triggering_element']['#parents'];
    $parent_aid = $parents[count($parents) - 1];
    $parent_value = $form_state['triggering_element']['#value'];
    uc_dropdown_attributes_remove_values($parent_aid, $parent_value, $nid, 'node', $form_state['values']);
  }
  foreach ($attributes as $attribute) {
    $parent_value = $form_state['values']['attributes'][$attribute->parent_aid];
    if ($form['attributes'][$attribute->parent_aid]['#type'] == 'checkboxes') {
      $parent_values = array_diff($parent_value, array(
        0,
      ));
      $values = unserialize($attribute->parent_values);
      if (count(array_intersect($parent_values, $values))) {

        // Show dependent attribute.
        if ($attribute->required) {
          $form['attributes'][$attribute->aid]['#required'] = TRUE;
        }
      }
      else {

        // Hide dependent attribute.
        $form['attributes'][$attribute->aid]['#post_render'][] = 'uc_dropdown_attributes_post_render';
        if ($attribute->required) {
          uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
        }
      }
    }
    else {
      if ($parent_value) {

        // A value has been entered in parent attribute.
        $values = unserialize($attribute->parent_values);
        if (array_key_exists($parent_value, $values)) {

          // Show dependent attribute.
          if ($attribute->required) {
            $form['attributes'][$attribute->aid]['#required'] = TRUE;
          }
        }
        else {

          // Hide dependent attribute.
          $form['attributes'][$attribute->aid]['#post_render'][] = 'uc_dropdown_attributes_post_render';
          if ($attribute->required) {
            uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
          }
        }
      }
      else {
        $form['attributes'][$attribute->aid]['#post_render'][] = 'uc_dropdown_attributes_post_render';
        if ($attribute->required) {
          uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
        }
      }
    }
  }
  return $form;
}