You are here

function _uc_dropdown_attributes_class_build in Dropdown Attributes 7

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

Form build for classes.

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

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

File

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

Code

function _uc_dropdown_attributes_class_build($form, &$form_state) {
  $sql = 'SELECT aid, parent_aid, parent_values, required
    FROM {uc_dropdown_classes} WHERE pcid=:pcid';
  $pcid = uc_dropdown_attributes_get_type($form['nid']['#value']);
  $attributes = db_query($sql, array(
    ':pcid' => $pcid,
  ));
  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, $pcid, 'class', $form_state['values']);
  }
  foreach ($attributes as $attribute) {
    if (isset($form_state['values']['attributes'])) {
      $parent_value = $form_state['values']['attributes'][$attribute->parent_aid];
    }
    if ($parent_value) {

      // A value has been entered in parent attribute.
      $values = unserialize($attribute->parent_values);
      if ($form['attributes'][$attribute->parent_aid]['#type'] == 'checkboxes') {
        $parent_values = array_diff($parent_value, array(
          0,
        ));
        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 && count($form['attributes'][$attribute->aid]['#value']) > 0) {
            $form['attributes'][$attribute->aid]['#value'] = array();
          }
        }
      }
      else {
        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 && $form['attributes'][$attribute->aid]['#value'] != '') {
            $form['attributes'][$attribute->aid]['#value'] = '';
          }
        }
      }
    }
    else {
      $form['attributes'][$attribute->aid]['#post_render'][] = 'uc_dropdown_attributes_post_render';
      if ($attribute->required && isset($form['attributes'][$attribute->aid]['#value']) && $form['attributes'][$attribute->aid]['#value'] != '') {
        $form['attributes'][$attribute->aid]['#value'] = '';
      }
    }
  }
  return $form;
}