You are here

function commerce_price_decimals_formatter_field_formatter_view in Commerce Price Decimals Formatter 7

Implements hook_field_formatter_view().

File

./commerce_price_decimals_formatter.module, line 91
Provides a display formatter for the price field in which you can specify the decimal places are displayed.

Code

function commerce_price_decimals_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'] + field_info_formatter_settings($display['type']);
  $settings['currencies'] += commerce_price_decimals_formatter_get_default_currencies_settings();

  // Loop through each price value in this field.
  foreach ($items as $delta => $item) {

    // Do not render a price if the amount is NULL (i.e. non-zero empty value).
    if (is_null($item['amount'])) {

      // TODO: Consider if we should render as N/A or something indicating a
      // price was not available as opposed to just leaving a blank.
      continue;
    }
    switch ($display['type']) {
      case 'commerce_price_decimals_formatter':
        $element[] = array(
          '#markup' => commerce_price_decimals_formatter_currency_format($item['amount'], $item['currency_code'], $settings, $entity),
        );
        break;
      case 'commerce_price_decimals_formatter_components':

        // Build an array of component display titles and their prices.
        $components = array();
        $weight = 0;
        foreach ($item['data']['components'] as $key => $component) {
          $component_type = commerce_price_component_type_load($component['name']);

          // Hook in with the Commerce Discount module
          if (isset($component['price']['data']['discount_component_title'])) {
            $component_type['display_title'] = $component['price']['data']['discount_component_title'];
          }
          if (empty($components[$component['name']])) {
            $components[$component['name']] = array(
              'title' => check_plain($component_type['display_title']),
              'price' => commerce_price_component_total($item, $component['name']),
              'weight' => $component_type['weight'],
            );
            $weight = max($weight, $component_type['weight']);
          }
        }

        // If there is only a single component and its price equals the field's,
        // then remove it and just show the actual price amount.
        if (count($components) == 1 && in_array('base_price', array_keys($components))) {
          $components = array();
        }

        // If the i18n_field module is available, we'll use it to translate
        // user-configurable field labels.
        if (module_exists('i18n_field') && !empty($instance['label'])) {
          $instance['label'] = i18n_field_translate_property($instance, 'label');
        }

        // Add the actual field value to the array.
        $components['commerce_price_formatted_amount'] = array(
          'title' => check_plain($instance['label']),
          'price' => $item,
          'weight' => $weight + 1,
        );

        // Sort the components by weight.
        uasort($components, 'drupal_sort_weight');

        // Format the prices for display.
        foreach ($components as $key => &$component) {
          $component['formatted_price'] = commerce_price_decimals_formatter_currency_format($component['price']['amount'], $component['price']['currency_code'], $settings, $entity);
        }
        $element[$delta] = array(
          '#markup' => theme('commerce_price_formatted_components', array(
            'components' => $components,
            'price' => $item,
          )),
        );
        break;
    }
  }
  return $element;
}