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