You are here

function commerce_price_table_field_formatter_view in Commerce Price Table 7

Implements hook_field_formatter_view().

File

./commerce_price_table.module, line 407

Code

function commerce_price_table_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  if ($display['type'] == 'commerce_multiprice_default' && !empty($items)) {
    $header = array(
      isset($display['settings']['quantity_label']) ? $display['settings']['quantity_label'] : t('Quantity'),
    );
    $row = array(
      isset($display['settings']['price_label']) ? $display['settings']['price_label'] : t('Price'),
    );
    if ($entity_type == 'commerce_product') {
      foreach ($items as $delta => $item) {
        if (isset($item['min_qty']) && $item['max_qty'] && $item['amount']) {
          $header[] = commerce_price_table_display_quantity_headers($item);
          $line_item = commerce_product_line_item_new($entity, $item['min_qty']);
          $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
          $line_item_wrapper->commerce_unit_price->amount = $item['amount'];

          // Empty the price components to recalculate them.
          $line_item->commerce_unit_price[LANGUAGE_NONE][0]['data']['components'] = array();
          $price = array(
            'amount' => $item['amount'],
            'currency_code' => $item['currency_code'],
            'data' => array(),
          );

          // Alter the base price to the current one.
          $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
            ->value(), 'base_price', $price, TRUE);

          // Invoke the calculation rule event.
          rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
          $row[] = array(
            'data' => commerce_currency_format($line_item_wrapper->commerce_unit_price->amount
              ->value(), $line_item_wrapper->commerce_unit_price->currency_code
              ->value(), $entity),
          );
        }
      }
    }
    else {

      // Not a product. We're dealing with exotic stuff here.
      foreach ($items as $delta => $item) {
        if (isset($item['min_qty']) && $item['max_qty'] && $item['amount']) {
          $header[] = commerce_price_table_display_quantity_headers($item);
          $row[] = array(
            'data' => commerce_currency_format($item['amount'], $item['currency_code'], $entity),
          );
        }
      }
    }

    // By default, the price table is rendered horizontally. If vertical
    // orientation was choosen, flip the table.
    if (isset($display['settings']['table_orientation']) && $display['settings']['table_orientation'] == COMMERCE_PRICE_TABLE_VERTICAL) {
      $rows = array();
      $header_old = $header;
      $header = array(
        $header_old[0],
        $row[0],
      );
      for ($index = 1; $index < count($row); $index++) {
        $rows[] = array(
          'data' => array(
            $header_old[$index],
            $row[$index]['data'],
          ),
        );
      }
    }
    else {
      $rows = array(
        $row,
      );
    }
    $element[] = array(
      '#markup' => theme('table', array(
        'header' => $header,
        'rows' => $rows,
      )),
    );
  }
  return $element;
}