Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
EmailTableFormatter | |
0.00% |
0 / 72 |
|
0.00% |
0 / 3 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
create | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
viewElements | |
0.00% |
0 / 60 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace Drupal\crm_field\Plugin\Field\FieldFormatter; |
4 | |
5 | use Drupal\Core\Datetime\DateFormatterInterface; |
6 | use Drupal\Core\Datetime\DrupalDateTime; |
7 | use Drupal\Core\Field\FieldDefinitionInterface; |
8 | use Drupal\Core\Field\FieldItemListInterface; |
9 | use Drupal\Core\Field\FormatterBase; |
10 | use Symfony\Component\DependencyInjection\ContainerInterface; |
11 | |
12 | /** |
13 | * Plugin implementation of the 'crm_email_table' formatter. |
14 | * |
15 | * @FieldFormatter( |
16 | * id = "crm_email_table", |
17 | * label = @Translation("Table"), |
18 | * field_types = {"crm_email"} |
19 | * ) |
20 | */ |
21 | class EmailTableFormatter extends FormatterBase { |
22 | |
23 | /** |
24 | * The date formatter service. |
25 | * |
26 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
27 | */ |
28 | protected $dateFormatter; |
29 | |
30 | /** |
31 | * Constructs a EmailKeyValueFormatter object. |
32 | * |
33 | * @param string $plugin_id |
34 | * The plugin_id for the formatter. |
35 | * @param mixed $plugin_definition |
36 | * The plugin implementation definition. |
37 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition |
38 | * The definition of the field to which the formatter is associated. |
39 | * @param array $settings |
40 | * The formatter settings. |
41 | * @param string $label |
42 | * The formatter label display setting. |
43 | * @param string $view_mode |
44 | * The view mode. |
45 | * @param array $third_party_settings |
46 | * Any third party settings. |
47 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
48 | * The date formatter service. |
49 | */ |
50 | public function __construct( |
51 | $plugin_id, |
52 | $plugin_definition, |
53 | FieldDefinitionInterface $field_definition, |
54 | array $settings, |
55 | $label, |
56 | $view_mode, |
57 | array $third_party_settings, |
58 | DateFormatterInterface $date_formatter, |
59 | ) { |
60 | |
61 | parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); |
62 | |
63 | $this->dateFormatter = $date_formatter; |
64 | } |
65 | |
66 | /** |
67 | * {@inheritdoc} |
68 | */ |
69 | final public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
70 | return new self( |
71 | $plugin_id, |
72 | $plugin_definition, |
73 | $configuration['field_definition'], |
74 | $configuration['settings'], |
75 | $configuration['label'], |
76 | $configuration['view_mode'], |
77 | $configuration['third_party_settings'], |
78 | $container->get('date.formatter') |
79 | ); |
80 | } |
81 | |
82 | /** |
83 | * {@inheritdoc} |
84 | */ |
85 | public function viewElements(FieldItemListInterface $items, $langcode) { |
86 | |
87 | $header[] = '#'; |
88 | $header[] = $this->t('Email'); |
89 | $header[] = $this->t('Location'); |
90 | $header[] = $this->t('Primary'); |
91 | $header[] = $this->t('Hold'); |
92 | $header[] = $this->t('Bulk'); |
93 | $header[] = $this->t('Hold Date'); |
94 | $header[] = $this->t('Reset Date'); |
95 | |
96 | $table = [ |
97 | '#type' => 'table', |
98 | '#header' => $header, |
99 | ]; |
100 | |
101 | foreach ($items as $delta => $item) { |
102 | $row = []; |
103 | |
104 | $row[]['#markup'] = $delta + 1; |
105 | |
106 | $row[]['#markup'] = $item->email; |
107 | |
108 | $row[]['#markup'] = $item->location_id; |
109 | |
110 | $row[]['#markup'] = $item->primary ? $this->t('Yes') : $this->t('No'); |
111 | |
112 | $row[]['#markup'] = $item->hold ? $this->t('Yes') : $this->t('No'); |
113 | |
114 | $row[]['#markup'] = $item->bulk ? $this->t('Yes') : $this->t('No'); |
115 | |
116 | if ($item->hold_date) { |
117 | $date = DrupalDateTime::createFromFormat('Y-m-d\TH:i:s', $item->hold_date); |
118 | $timestamp = $date->getTimestamp(); |
119 | $formatted_date = $this->dateFormatter->format($timestamp, 'long'); |
120 | $iso_date = $this->dateFormatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z'; |
121 | $row[] = [ |
122 | '#theme' => 'time', |
123 | '#text' => $formatted_date, |
124 | '#html' => FALSE, |
125 | '#attributes' => [ |
126 | 'datetime' => $iso_date, |
127 | ], |
128 | '#cache' => [ |
129 | 'contexts' => [ |
130 | 'timezone', |
131 | ], |
132 | ], |
133 | ]; |
134 | } |
135 | else { |
136 | $row[]['#markup'] = ''; |
137 | } |
138 | |
139 | if ($item->reset_date) { |
140 | $date = DrupalDateTime::createFromFormat('Y-m-d\TH:i:s', $item->reset_date); |
141 | $timestamp = $date->getTimestamp(); |
142 | $formatted_date = $this->dateFormatter->format($timestamp, 'long'); |
143 | $iso_date = $this->dateFormatter->format($timestamp, 'custom', 'Y-m-d\TH:i:s') . 'Z'; |
144 | $row[] = [ |
145 | '#theme' => 'time', |
146 | '#text' => $formatted_date, |
147 | '#html' => FALSE, |
148 | '#attributes' => [ |
149 | 'datetime' => $iso_date, |
150 | ], |
151 | '#cache' => [ |
152 | 'contexts' => [ |
153 | 'timezone', |
154 | ], |
155 | ], |
156 | ]; |
157 | } |
158 | else { |
159 | $row[]['#markup'] = ''; |
160 | } |
161 | |
162 | $table[$delta] = $row; |
163 | } |
164 | |
165 | return [$table]; |
166 | } |
167 | |
168 | } |