You are here

function _uc_dropdown_attributes_kit_build in Dropdown Attributes 8

Same name and namespace in other branches
  1. 7 uc_dropdown_attributes.module \_uc_dropdown_attributes_kit_build()

Form build for product kits.

Callback for $form['#after_build'] for product kits. Renders the dependent attributes and stores the html as a Javascript array.

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

File

./uc_dropdown_attributes.module, line 437
A module for uc_dropdown_attributes.

Code

function _uc_dropdown_attributes_kit_build($form, &$form_state) {
  foreach ($form['products'] as $key => $value) {
    if (is_numeric($key)) {
      $type = uc_dropdown_attributes_dependency_type($key);
      switch ($type) {
        case 'node':
          $id = $key;
          $fields = array(
            'aid',
            'parent_aid',
            'parent_values',
            'required',
          );
          $attributes = \Drupal::database()
            ->select('uc_dropdown_products', 'products')
            ->fields('products', $fields)
            ->condition('products.nid', $key)
            ->execute();
          break;
        case 'class':
          $pcid = uc_dropdown_attributes_get_type($key);
          $id = $pcid;
          $fields = array(
            'aid',
            'parent_aid',
            'parent_values',
            'required',
          );
          $attributes = \Drupal::database()
            ->select('uc_dropdown_classes', 'classes')
            ->fields('classes', $fields)
            ->condition('classes.pcid', $pcid)
            ->execute();
          break;
        default:
          $attributes = array();
      }
      $input =& $form_state
        ->getUserInput();
      $trigger = $form_state
        ->getTriggeringElement();
      if (!is_null($trigger)) {
        $parents = $trigger['#parents'];
        $parent_aid = $parents[3];
        $parent_value = $trigger['#value'];
        if (!is_null($type)) {
          uc_dropdown_attributes_remove_values($parent_aid, $parent_value, $id, $type, $input['products'][$key]);
        }
      }
      foreach ($attributes as $attribute) {
        $aid = $attribute->aid;
        $parent_aid = $attribute->parent_aid;
        if ($parent_aid == 0) {
          continue;
        }
        if (!isset($input['products'][$key]['attributes'][$parent_aid])) {
          $input['products'][$key]['attributes'][$parent_aid] = array();
        }
        $parent_value = $input['products'][$key]['attributes'][$parent_aid];
        $type = $form['products'][$key]['attributes'][$parent_aid]['#type'];
        $values = unserialize($attribute->parent_values);
        if ($type == 'checkboxes') {
          $parent_values = array_diff($parent_value, array(
            0,
          ));
          if (count(array_intersect($parent_values, $values)) == 0) {
            uc_dropdown_attributes_hide_attribute($form['products'][$key], $attribute->aid);
            if ($attribute->required) {
              uc_dropdown_attributes_clear_input($form['products'][$key], $form_state, $attribute->aid, $id);
            }
          }
        }
        else {
          if (!is_numeric($parent_value) || !in_array($parent_value, $values)) {
            uc_dropdown_attributes_hide_attribute($form['products'][$key], $attribute->aid);
            if ($attribute->required) {
              uc_dropdown_attributes_clear_input($form['products'][$key], $form_state, $attribute->aid, $id);
            }
          }
        }
      }
    }
  }
  return $form;
}