You are here

function _uc_attribute_alter_form in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
  2. 5 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
  3. 7.3 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()

Helper function for uc_attribute_form_alter().

See also

theme_uc_attribute_add_to_cart()

3 calls to _uc_attribute_alter_form()
hook_uc_form_alter in docs/hooks.php
Allows modules to modify forms before Drupal invokes hook_form_alter().
uc_attribute_form_alter in uc_attribute/uc_attribute.module
Implements hook_form_alter().
uc_attribute_uc_form_alter in uc_attribute/uc_attribute.module
Implements hook_uc_form_alter().

File

uc_attribute/uc_attribute.module, line 1271

Code

function _uc_attribute_alter_form($product) {

  // If the product doesn't have attributes, return the form as it is.
  if (!isset($product->attributes)) {
    $product->attributes = uc_product_get_attributes($product->nid);
  }
  if (!is_array($product->attributes) || empty($product->attributes)) {
    return NULL;
  }
  $nid = $product->nid;

  // Load all product attributes for the given nid.
  $priced_attributes = uc_attribute_priced_attributes($nid);
  $attributes = $product->attributes;
  $form_attributes = array();
  $context = array(
    'revision' => 'formatted',
  );

  // Loop through each product attribute and generate its form element.
  foreach ($attributes as $attribute) {
    $context['subject']['attribute'] = $attribute;

    // Build the attribute's options array.
    $options = array();
    foreach ($attribute->options as $option) {
      $context['subject']['option'] = $option;
      $display_price = '';
      if (in_array($attribute->aid, $priced_attributes)) {
        switch (variable_get('uc_attribute_option_price_format', 'adjustment')) {
          case 'total':
            $context['type'] = 'product';
            $context['subject']['node'] = $product;
            if (count($priced_attributes) == 1 && $attribute->display != 3) {
              $display_price = uc_price($product->sell_price + $option->price, $context);
              break;
            }
          case 'adjustment':
            $context['type'] = 'attribute_option';
            if ($option->price != 0) {
              $display_price = $option->price > 0 ? '+' : '-';
              $display_price .= uc_price(abs($option->price), $context);
            }
            break;
        }
      }

      // Select options are check_plain()ed, but radio button labels are not.
      $options[$option->oid] = theme('uc_attribute_option', $attribute->display == 2 ? check_plain($option->name) : $option->name, $display_price);
    }
    if (count($attribute->options) && $attribute->display > 0) {
      if ($attribute->required) {
        if ($attribute->display == 1) {
          $options = array(
            '' => t('Please select'),
          ) + $options;
        }
        $attribute->default_option = '';
      }
      $attr_type = '';
      switch ($attribute->display) {
        case 1:
          $attr_type = 'select';
          break;
        case 2:
          $attr_type = 'radios';
          break;
        case 3:
          $attr_type = 'checkboxes';
          break;
      }
      $form_attributes[$attribute->aid] = array(
        '#type' => $attr_type,
        '#description' => check_markup($attribute->description),
        '#default_value' => $attr_type == "checkboxes" ? array() : $attribute->default_option,
        '#options' => $options,
        '#required' => $attribute->required,
      );
    }
    else {
      $form_attributes[$attribute->aid] = array(
        '#type' => 'textfield',
        '#description' => check_markup($attribute->description),
        '#default_value' => '',
        '#required' => $attribute->required,
      );
      if (!$attribute->required && isset($attribute->options[$attribute->default_option])) {
        $form_attributes[$attribute->aid]['#default_value'] = $attribute->options[$attribute->default_option]->name;
      }
    }
    $name = _uc_attribute_get_name($attribute, FALSE);
    if (!is_null($name)) {
      $form_attributes[$attribute->aid]['#title'] = check_plain($name);
    }
    $form_attributes['#theme'] = 'uc_attribute_add_to_cart';
  }
  return $form_attributes;
}