You are here

function _uc_dropdown_attributes_product_build in Dropdown Attributes 8

Same name and namespace in other branches
  1. 7 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 237
A module for uc_dropdown_attributes.

Code

function _uc_dropdown_attributes_product_build($form, &$form_state) {
  $nid = $form['node']['#value']
    ->id();
  $fields = array(
    'aid',
    'parent_aid',
    'parent_values',
    'required',
  );
  $attributes = \Drupal::database()
    ->select('uc_dropdown_products', 'products')
    ->fields('products', $fields)
    ->condition('products.nid', $nid)
    ->execute();
  $trigger = $form_state
    ->getTriggeringElement();
  $form_values = $form_state
    ->getValues();
  if (!is_null($trigger)) {
    $parents = $trigger['#parents'];
    $parent_aid = $parents[1];
    $parent_value = $trigger['#value'];
    uc_dropdown_attributes_remove_values($parent_aid, $parent_value, $nid, 'node', $form_values);
  }
  foreach ($attributes as $attribute) {
    if ($attribute->parent_aid == 0) {
      continue;
    }
    $parent_value = $form_values['attributes'][$attribute->parent_aid];
    $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)) == 0) {
        uc_dropdown_attributes_hide_attribute($form, $attribute->aid);
        if ($attribute->required) {
          uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
        }
      }
    }
    else {
      if (is_numeric($parent_value)) {
        if (!in_array($parent_value, $values)) {
          uc_dropdown_attributes_hide_attribute($form, $attribute->aid);
          if ($attribute->required) {
            uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
          }
        }
      }
      else {
        uc_dropdown_attributes_hide_attribute($form, $attribute->aid);
        if ($attribute->required) {
          uc_dropdown_attributes_clear_input($form, $form_state, $attribute->aid);
        }
      }
    }
  }
  return $form;
}