function _uc_attribute_alter_form in Ubercart 7.3
Same name and namespace in other branches
- 8.4 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
- 5 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
- 6.2 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
Helper function for uc_attribute_form_alter().
Parameters
$id: The unique id to use to wrap these form elements.
&$product: The product node for which the attribute form elements are to be attached.
bool $use_ajax: TRUE to add ajax to the form. Note that ajax may be added even if this is FALSE, if there are multiple attributes and one or more of them is set to display total price.
Return value
array Form API array with attribute elements to add to the product form.
See also
theme_uc_attribute_add_to_cart()
2 calls to _uc_attribute_alter_form()
- hook_uc_form_alter in uc_store/
uc_store.api.php - Allows modules to modify forms before Drupal invokes 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 1292 - Ubercart Attribute module.
Code
function _uc_attribute_alter_form($id, &$product, $use_ajax) {
// If the product doesn't have attributes, return the form as it is.
if (empty($product->attributes) || !is_array($product->attributes)) {
return array();
}
$nid = $product->nid;
$attributes = $product->attributes;
$priced_attributes = uc_attribute_priced_attributes($nid);
// At this point, $product->data is the node author's userdata
// as a string, as populated by user_node_load(). We don't need that.
if (empty($product->data) || !is_array($product->data)) {
$product->data = array();
}
// If the form is being built for the first time, populate attributes
// with their default values.
if (!isset($product->data['attributes'])) {
$values = array();
foreach ($priced_attributes as $aid) {
if (!$attributes[$aid]->required && ($attributes[$aid]->display == 1 || $attributes[$aid]->display == 2)) {
$values[$aid] = $attributes[$aid]->default_option;
}
}
if (!empty($values)) {
$data = $product->data;
$data['attributes'] = $values;
if (isset($product->qty)) {
// Preserve the quantity (for product-kit sub-products).
$qty = $product->qty;
}
$product = uc_product_load_variant($product->nid, $data);
if (isset($qty)) {
$product->qty = $qty;
}
}
}
// Initialize the form element.
$form_attributes = array(
'#theme' => 'uc_attribute_add_to_cart',
'#id' => $id,
);
$price_format = variable_get('uc_attribute_option_price_format', 'adjustment');
// Loop through each product attribute and generate its form element.
foreach ($attributes as $attribute) {
// Build the attribute's options array.
$options = array();
foreach ($attribute->options as $option) {
$display_price = '';
if (in_array($attribute->aid, $priced_attributes)) {
$data = array(
'display_only' => TRUE,
) + $product->data;
if (empty($data['attributes'])) {
$data['attributes'] = array();
}
switch ($price_format) {
case 'total':
// Only display total price for non-checkbox options.
// !TODO Fix attribute option total price display for product kits.
if ($attribute->display != 3 && !isset($product->data['kit_id'])) {
$use_ajax = $use_ajax || count($priced_attributes) > 1;
$data['attributes'] = array(
$attribute->aid => $option->oid,
) + $data['attributes'];
$variant = node_view(uc_product_load_variant($product->nid, $data), 'teaser');
$display_price = uc_currency_format($variant['display_price']['#value']);
break;
}
case 'adjustment':
if ($attribute->display == 3 || !$use_ajax) {
// For checkboxes, or if the node totals are not being updated,
// we compare this attribute against base price.
if (empty($base)) {
// only build the base once.
unset($data['attributes']);
$base = node_view(uc_product_load_variant($product->nid, $data), 'teaser');
}
$data['attributes'] = array(
$attribute->aid => $option->oid,
);
$variant = node_view(uc_product_load_variant($product->nid, $data), 'teaser');
$adjustment = $variant['display_price']['#value'] - $base['display_price']['#value'];
}
else {
// Otherwise we compare against current total price.
if (empty($selected)) {
$selected = node_view(uc_product_load_variant($product->nid, $data), 'teaser');
}
$data['attributes'] = array(
$attribute->aid => $option->oid,
) + $data['attributes'];
$variant = node_view(uc_product_load_variant($product->nid, $data), 'teaser');
$adjustment = $variant['display_price']['#value'] - $selected['display_price']['#value'];
}
if ($adjustment != 0) {
$display_price = $adjustment > 0 ? '+' : '-';
$display_price .= uc_currency_format(abs($adjustment));
}
break;
}
}
// Select options are check_plain()ed, but radio button labels are not.
$options[$option->oid] = theme('uc_attribute_option', array(
'option' => $attribute->display == 2 ? check_plain($option->name) : $option->name,
'price' => $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 = NULL;
}
$attr_type = '';
switch ($attribute->display) {
case 1:
$attr_type = 'select';
break;
case 2:
$attr_type = 'radios';
break;
case 3:
$attr_type = 'checkboxes';
$attribute->default_option = array();
break;
}
$form_attributes[$attribute->aid] = array(
'#type' => $attr_type,
'#default_value' => $attribute->default_option,
'#options' => $options,
);
if ($use_ajax) {
$form_attributes[$attribute->aid]['#ajax'] = array(
'callback' => 'uc_attribute_option_ajax',
'wrapper' => $id,
);
}
}
elseif ($attribute->display > 0) {
$form_attributes[$attribute->aid] = array(
'#type' => 'textfield',
'#default_value' => '',
);
if (!$attribute->required && isset($attribute->options[$attribute->default_option])) {
$form_attributes[$attribute->aid]['#default_value'] = $attribute->options[$attribute->default_option]->name;
}
}
else {
$form_attributes[$attribute->aid] = array(
'#type' => 'textfield',
'#default_value' => '',
);
}
$name = _uc_attribute_get_name($attribute, FALSE);
if (!is_null($name)) {
$form_attributes[$attribute->aid]['#title'] = check_plain($name);
}
$form_attributes[$attribute->aid]['#description'] = filter_xss($attribute->description);
$form_attributes[$attribute->aid]['#required'] = $attribute->required;
}
return $form_attributes;
}