You are here

function uc_dropdown_attributes_form_alter in Dropdown Attributes 8

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

Implements hook_form_alter().

File

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

Code

function uc_dropdown_attributes_form_alter(&$form, $form_state, $form_id) {
  if (!isset($form['node']['#value'])) {
    return;
  }
  $nid = $form['node']['#value']
    ->id();
  $node = Node::load($nid);
  if (!uc_product_is_product($nid) && 'product_kit' != $node
    ->getType()) {
    return;
  }
  if (isset($form['products'])) {
    foreach ($form['products'] as $key => $value) {
      if (is_numeric($key)) {
        $type = uc_dropdown_attributes_dependency_type($key);
        if (!is_null($type)) {
          $input =& $form_state
            ->getUserInput();
          if (!isset($input['products'][$key])) {
            $input['products'][$key] = array();
          }
          uc_dropdown_attributes_product_alter($key, $form['products'][$key], $input['products'][$key], $type, $form_state
            ->isRebuilding());

          // Make sure these have not been added more than once.
          if (!isset($form['#after_build']) || !in_array('_uc_dropdown_attributes_kit_build', $form['#after_build'])) {
            $form['#after_build'][] = '_uc_dropdown_attributes_kit_build';
          }
        }
      }
    }
  }
  else {
    $type = uc_dropdown_attributes_dependency_type($nid);
    if (!is_null($type)) {
      $values = $form_state
        ->getValues();
      uc_dropdown_attributes_product_alter($nid, $form, $values, $type, $form_state
        ->isRebuilding());
      $form['node_id'] = array(
        '#type' => 'hidden',
        '#value' => $nid,
      );
      switch ($type) {
        case 'node':
          $form['#after_build'][] = '_uc_dropdown_attributes_product_build';
          break;
        case 'class':
          $form['#after_build'][] = '_uc_dropdown_attributes_class_build';
          break;
      }
    }
  }
}