View source
<?php
namespace Drupal\commerce_order\Plugin\Field\FieldFormatter;
use Drupal\commerce_order\OrderTotalSummaryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OrderTotalSummary extends FormatterBase implements ContainerFactoryPluginInterface {
protected $orderTotalSummary;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, OrderTotalSummaryInterface $order_total_summary) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->orderTotalSummary = $order_total_summary;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('commerce_order.order_total_summary'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$order = $items
->getEntity();
$elements = [];
if (!$items
->isEmpty()) {
$elements[0] = [
'#theme' => 'commerce_order_total_summary',
'#order_entity' => $order,
'#totals' => $this->orderTotalSummary
->buildTotals($order),
];
}
return $elements;
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
$entity_type = $field_definition
->getTargetEntityTypeId();
$field_name = $field_definition
->getName();
return $entity_type == 'commerce_order' && $field_name == 'total_price';
}
}