function commerce_pricelist_item_form in Commerce Pricelist 7
Form function to create an commerce_pricelist_list entity.
The pattern is:
- Set up the form for the data that is specific to your entity: the columns of your base table.
- Call on the Field API to pull in the form elements for fields attached to the entity.
2 string references to 'commerce_pricelist_item_form'
- commerce_pricelist_item_add in includes/
commerce_pricelist.admin.inc - Provides a wrapper on the edit form to add a new entity.
- commerce_pricelist_menu in ./
commerce_pricelist.module - Implements hook_menu().
File
- includes/
commerce_pricelist.admin.inc, line 574 - Summary
Code
function commerce_pricelist_item_form($form, &$form_state, $pricelist_item, $pricelist = NULL) {
if (!$pricelist_item) {
// This is a new list item being created.
$pricelist_item = commerce_pricelist_item_new($pricelist->list_id);
}
if ($pricelist) {
// This item is being created from the pricelist > add price tab.
$pricelist_item->pricelist_id = $pricelist->list_id;
}
if ($pricelist_item->pricelist_id) {
$form['pricelist_id'] = array(
'#type' => 'value',
'#value' => $pricelist_item->pricelist_id,
);
}
else {
$pricelists = commerce_pricelist_list_load_multiple();
$currencies = array();
foreach ($pricelists as $pricelist) {
$currencies[$pricelist->list_id] = $pricelist->title;
}
$form['pricelist_id'] = array(
'#type' => 'select',
'#options' => $currencies,
'#required' => TRUE,
'#title' => t('Price list'),
'#element_validate' => array(
'_commerce_pricelist_validate_pricelist',
),
);
}
$form_fields = array(
'sku',
'valid_from',
'valid_to',
'quantity',
);
foreach ($form_fields as $field) {
$form[$field] = array(
'#type' => 'textfield',
'#title' => ucwords(str_replace('_', ' ', $field)),
'#required' => TRUE,
'#default_value' => isset($pricelist_item->{$field}) ? $pricelist_item->{$field} : '',
);
}
$form['price'] = array(
'#title' => t('Price'),
'#tree' => TRUE,
'#type' => 'item',
'#element_validate' => array(
'commerce_price_field_widget_validate',
),
);
// If a price has already been set for this item prepare default values.
if (isset($pricelist_item->price_amount)) {
$currency = commerce_currency_load($pricelist_item->currency_code);
// Convert the price amount to a user friendly decimal value.
$default_amount = commerce_currency_amount_to_decimal($pricelist_item->price_amount, $currency['code']);
// Run it through number_format() to ensure it has the proper number of
// decimal places.
$default_amount = number_format($default_amount, $currency['decimals'], '.', '');
}
else {
$default_amount = NULL;
}
$form['price']['amount'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 10,
'#default_value' => $default_amount,
'#prefix' => '<div class="container-inline">',
);
$form['price']['currency_code'] = array(
'#type' => 'select',
'#title' => '',
'#options' => array_filter(variable_get('commerce_enabled_currencies', array(
'USD' => 'USD',
))),
'#default_value' => isset($pricelist_item->{$field}) ? $pricelist_item->{$field} : commerce_default_currency(),
'#suffix' => '</div>',
);
foreach (array(
'valid_from',
'valid_to',
) as $date) {
$default_date = isset($pricelist_item->{$date}) ? $pricelist_item->{$date} : 0;
$form[$date]['#required'] = FALSE;
$form[$date]['#element_validate'] = array(
'_commerce_pricelist_date_validate',
);
$form[$date]['#description'] = t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC.', array(
'%time' => date('Y-m-d H:i:s O', $default_date),
'%timezone' => date('O', $default_date),
));
$form[$date]['#default_value'] = _commerce_pricelist_display_date($default_date, 'Y-m-d H:i:s O');
}
$form['quantity']['#element_validate'] = array(
'element_validate_number',
);
$form['entity'] = array(
'#type' => 'value',
'#value' => $pricelist_item,
);
$form['actions'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array(
'form-actions',
'container-inline',
),
),
'#weight' => 100,
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
// Check if this is an existing item, add delete button
if ($pricelist_item->item_id) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#submit' => array(
'commerce_pricelist_item_edit_delete',
),
'#weight' => 200,
);
}
return $form;
}