You are here

function money_widget in Money field 5

Same name and namespace in other branches
  1. 6 money.module \money_widget()

Implementation of hook_widget().

File

./money.module, line 277
This module defines the "money" CCK field. It uses the Currency API, which is included in the Currency module, to get a list of valid currencies.

Code

function money_widget($op, &$node, $field, &$items) {
  if ($field['widget']['type'] == 'money_default') {
    switch ($op) {
      case 'prepare form values':
        $decimal_separator = _money_get_decimal_separator($field['decimal_separator']);
        $digit_group_separator = _money_get_digit_group_separator($field['digit_group_separator']);
        $displayed_decimals = _money_get_displayed_decimals($field['displayed_decimals']);
        if (!count($items)) {
          $items[0] = array();
        }
        else {
          foreach ($items as $delta => $item) {
            $items[$delta]['amount'] = check_plain(number_format($item['amount'] / 100, $displayed_decimals, $decimal_separator, $digit_group_separator));
          }
        }
        break;
      case 'form':
        drupal_add_css(drupal_get_path('module', 'money') . '/money.css');
        $decimal_separator = _money_get_decimal_separator($field['widget']['decimal_separator']);
        $allowed_currencies = _money_parse_currencies($field['allowed_currencies']);

        // Variables to be used in the "currency" form item.
        $currency_options = array_combine($allowed_currencies, $allowed_currencies);

        // Variables to be used in the "amount" form item.
        if (isset($field['widget']['default_value'][0]['amount'])) {
          $amount_default = check_plain(number_format($field['widget']['default_value'][0]['amount'] / 100, 2, $decimal_separator, $digit_group_separator));
        }
        else {
          $amount_default = check_plain(number_format("0{$decimal_separator}00" / 100, 2, $decimal_separator, $digit_group_separator));
        }
        $amount_description = t('Use "@decimal_separator" as the decimal separator and (optionally)
          "@digit_group_separator" as the digit group separator. You can
          only enter two decimals.', array(
          '@decimal_separator' => $field['decimal_separator'],
          '@digit_group_separator' => $field['digit_group_separator'],
        ));

        // If this field is configured as a multiple value field, make sure
        // that there are at least 3 form items.
        while ($field['multiple'] && count($items) < 3) {
          $items[] = array();
        }

        // Create the prefix in which we'll store first the label, then a
        // container div in which we'll put the actual form elements.
        $prefix = '<div class="form-item">';
        $prefix .= '<label>' . t($field['widget']['label']);
        if (!empty($field['required'])) {
          $prefix .= '<span class="form-required" title="' . t('This field is required.') . '">*</span>';
        }
        $prefix .= '</label>';

        // Actual form creation begins here.
        $form = array();
        $form[$field['field_name']]['#tree'] = TRUE;
        $form[$field['field_name']]['#prefix'] = $prefix;
        $form[$field['field_name']]['#type'] = $field['multiple'] ? 'fieldset' : 'markup';
        $form[$field['field_name']]['#suffix'] = '</div>';
        $description = $amount_description;
        $description .= !empty($field['widget']['description']) ? '<br />' . $field['widget']['description'] : '';
        foreach ($items as $delta => $item) {

          // These are the actual form items for each money field.
          $form[$field['field_name']][$delta]['#tree'] = TRUE;
          $form[$field['field_name']][$delta]['currency'] = array(
            '#type' => 'select',
            '#options' => $currency_options,
            '#default_value' => isset($item['currency']) ? $item['currency'] : $field['widget']['default_value'][0]['currency'],
            '#attributes' => array(
              'class' => 'money-field money-field-currency',
            ),
            '#prefix' => '<div class="container-inline money-field-form-items">',
          );
          $form[$field['field_name']][$delta]['amount'] = array(
            '#type' => 'textfield',
            '#size' => 20,
            '#maxlength' => 25,
            '#default_value' => isset($item['amount']) ? $item['amount'] : $amount_default,
            '#attributes' => array(
              'class' => 'money-field money-field-amount',
            ),
            '#description' => $delta == end(array_keys($items)) ? $description : NULL,
            '#suffix' => '</div>',
          );
        }
        return $form;
      case 'process form values':
        foreach ($items as $delta => $item) {
          if (empty($item['amount'])) {
            unset($items[$delta]['amount']);
          }
          else {
            $items[$delta]['amount'] = _money_amount_to_integer($item['amount'], $field['decimal_separator'], $field['digit_group_separator']);
          }
        }
        break;
    }
  }
}