-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathOrderTotalSummary.php
More file actions
111 lines (99 loc) · 3.43 KB
/
OrderTotalSummary.php
File metadata and controls
111 lines (99 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?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;
/**
* Plugin implementation of the 'commerce_order_total_summary' formatter.
*
* @FieldFormatter(
* id = "commerce_order_total_summary",
* label = @Translation("Order total summary"),
* field_types = {
* "commerce_price",
* },
* )
*/
class OrderTotalSummary extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* The order total summary service.
*
* @var \Drupal\commerce_order\OrderTotalSummaryInterface
*/
protected $orderTotalSummary;
/**
* Constructs a new OrderTotalSummary object.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\commerce_order\OrderTotalSummaryInterface $order_total_summary
* The order total summary service.
*/
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;
}
/**
* {@inheritdoc}
*/
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')
);
}
/**
* {@inheritdoc}
*/
public function view(FieldItemListInterface $items, $langcode = NULL) {
// Check first if the total price is not empty.
if ($items->isEmpty()) {
return [];
}
return parent::view($items, $langcode);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $items->getEntity();
$elements = [];
if (!$items->isEmpty()) {
$elements[0] = [
'#theme' => 'commerce_order_total_summary',
'#totals' => $this->orderTotalSummary->buildTotals($order),
];
}
return $elements;
}
/**
* {@inheritdoc}
*/
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';
}
}