You are here

function uc_dropdown_attributes_order_product_alter in Dropdown Attributes 8

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

Alter products on oder page in preparation for drop down attributes.

Adds the 'Please select' and removes the default value. Ubercart does this for required attributes but since these attributes can no longer be required if the attributes are dependent then this reproduces the same thing.

Parameters

int $nid: Node ID.

array $form_attributes: Attributes part of the product form.

array $form_values: Form state.

string $type: For dependencies defined on the node level use 'node'; 'class' for dependencies defined on the product class.

1 call to uc_dropdown_attributes_order_product_alter()
uc_dropdown_attributes_form_uc_order_edit_form_alter in ./uc_dropdown_attributes.module
Implements hook_form_FORM_ID_alter() for uc_order_edit_form().

File

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

Code

function uc_dropdown_attributes_order_product_alter($nid, &$form_attributes, $form_values, $type) {
  $fields = array(
    'aid',
    'parent_aid',
    'parent_values',
    'required',
  );
  switch ($type) {
    case 'node':
      $attributes = \Drupal::database()
        ->select('uc_dropdown_products', 'products')
        ->fields('products', $fields)
        ->condition('products.nid', $nid)
        ->execute();
      break;
    case 'class':
      $pcid = uc_dropdown_attributes_get_type($nid);
      $attributes = \Drupal::database()
        ->select('uc_dropdown_classes', 'classes')
        ->fields('classes', $fields)
        ->condition('classes.pcid', $pcid)
        ->execute();
      break;
  }
  $parent_aids = array();
  foreach ($attributes as $attribute) {
    if ($attribute->parent_aid == 0) {
      continue;
    }
    $parent_aid = $attribute->parent_aid;
    $parent_aids[$parent_aid] = $parent_aid;
    $aid = $attribute->aid;
    if ($form_attributes[$aid]['#type'] == 'textfield' || isset($form_attributes[$aid]['#options']) && count($form_attributes[$aid]['#options']) && $attribute->required) {
      if (isset($form_values['attributes'][$parent_aid])) {
        $values = unserialize($attribute->parent_values);
        switch ($form_attributes[$parent_aid]['#type']) {
          case 'select':
            $parent_value = $form_values['attributes'][$parent_aid];
            if (in_array($parent_value, $values)) {
              $form_attributes[$aid]['#required'] = TRUE;
            }
            break;
          case 'radios':
            $parent_value = $form_values['attributes'][$parent_aid];
            if (in_array($parent_value, $values)) {
              $form_attributes[$aid]['#required'] = TRUE;
            }
            break;
          case 'checkboxes':
            $parent_values = $form_values['attributes'][$parent_aid];
            if (count(array_intersect($parent_values, $values)) > 0) {
              $form_attributes[$aid]['#required'] = TRUE;
            }
            break;
        }
      }
      switch ($form_attributes[$aid]['#type']) {
        case 'select':
          $form_attributes[$aid]['#options'] = array(
            '' => t('- Select -'),
          ) + $form_attributes[$aid]['#options'];
          $form_attributes[$aid]['#default_value'] = '';
          break;
        case 'radios':
          $form_attributes[$aid]['#default_value'] = NULL;
          break;
        case 'checkboxes':
          $form_attributes[$aid]['#default_value'] = array();
          break;
      }
    }
  }
  foreach ($parent_aids as $aid) {
    $form_attributes[$aid]['#ajax'] = array(
      'callback' => 'uc_dropdown_attributes_order_ajax_callback',
      'wrapper' => $form_attributes['#attributes']['id'],
      'event' => 'change',
    );
  }
}