Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       36 / 36  | 
               | 
       100.00%  | 
       4 / 4  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| AgeFormatter |         | 
       100.00%  | 
       36 / 36  | 
               | 
       100.00%  | 
       4 / 4  | 
       18 |         | 
       100.00%  | 
       1 / 1  | 
      
| viewElements |         | 
       100.00%  | 
       21 / 21  | 
               | 
       100.00%  | 
       1 / 1  | 
       15 | |||
| defaultSettings |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| settingsForm |         | 
       100.00%  | 
       13 / 13  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| settingsSummary |         | 
       100.00%  | 
       1 / 1  | 
               | 
       100.00%  | 
       1 / 1  | 
       1 | |||
| 1 | <?php | 
| 2 | |
| 3 | declare(strict_types=1); | 
| 4 | |
| 5 | namespace Drupal\crm\Plugin\Field\FieldFormatter; | 
| 6 | |
| 7 | use Drupal\Core\Field\Attribute\FieldFormatter; | 
| 8 | use Drupal\Core\Field\FieldItemListInterface; | 
| 9 | use Drupal\Core\Field\FormatterBase; | 
| 10 | use Drupal\Core\Form\FormStateInterface; | 
| 11 | use Drupal\Core\StringTranslation\TranslatableMarkup; | 
| 12 | |
| 13 | /** | 
| 14 | * The age formatter. | 
| 15 | */ | 
| 16 | #[FieldFormatter( | 
| 17 | id: "age", | 
| 18 | label: new TranslatableMarkup("Age"), | 
| 19 | description: new TranslatableMarkup("Display the age of the entity."), | 
| 20 | field_types: ["integer"] | 
| 21 | )] | 
| 22 | class AgeFormatter extends FormatterBase { | 
| 23 | |
| 24 | /** | 
| 25 | * {@inheritdoc} | 
| 26 | */ | 
| 27 | public function viewElements(FieldItemListInterface $items, $langcode) { | 
| 28 | $elements = []; | 
| 29 | |
| 30 | foreach ($items as $delta => $item) { | 
| 31 | // $item->value is an integer representing the number of days | 
| 32 | $days = (int) $item->value; | 
| 33 | |
| 34 | // Convert days to years, months for display. | 
| 35 | $years = floor($days / 365); | 
| 36 | $remaining_days_after_years = $days % 365; | 
| 37 | $months = floor($remaining_days_after_years / 30); | 
| 38 | $remaining_days = $remaining_days_after_years % 30; | 
| 39 | |
| 40 | // Optionally read formatter settings here. | 
| 41 | $granularity = $this->getSetting('granularity') ?? 'years'; | 
| 42 | |
| 43 | $output = match ($granularity) { | 
| 44 | 'years' => $years . ' ' . ($years == 1 ? 'year' : 'years'), | 
| 45 | 'months' => ($years * 12 + $months) . ' ' . (($years * 12 + $months) == 1 ? 'month' : 'months'), | 
| 46 | 'days' => $days . ' ' . ($days == 1 ? 'day' : 'days'), | 
| 47 | default => trim( | 
| 48 | ($years > 0 ? $years . ' ' . ($years == 1 ? 'year' : 'years') . ', ' : '') . | 
| 49 | ($months > 0 ? $months . ' ' . ($months == 1 ? 'month' : 'months') . ', ' : '') . | 
| 50 | ($remaining_days > 0 ? $remaining_days . ' ' . ($remaining_days == 1 ? 'day' : 'days') : ''), | 
| 51 | ', ' | 
| 52 | ), | 
| 53 | }; | 
| 54 | |
| 55 | $elements[$delta] = ['#markup' => $output]; | 
| 56 | } | 
| 57 | |
| 58 | return $elements; | 
| 59 | } | 
| 60 | |
| 61 | /** | 
| 62 | * {@inheritdoc} | 
| 63 | */ | 
| 64 | public static function defaultSettings() { | 
| 65 | return ['granularity' => 'years'] + parent::defaultSettings(); | 
| 66 | } | 
| 67 | |
| 68 | /** | 
| 69 | * {@inheritdoc} | 
| 70 | */ | 
| 71 | public function settingsForm(array $form, FormStateInterface $form_state) { | 
| 72 | return [ | 
| 73 | 'granularity' => [ | 
| 74 | '#title' => $this->t('Granularity'), | 
| 75 | '#type' => 'select', | 
| 76 | '#options' => [ | 
| 77 | 'years' => $this->t('Years'), | 
| 78 | 'months' => $this->t('Months'), | 
| 79 | 'days' => $this->t('Days'), | 
| 80 | 'full' => $this->t('Full (years, months, days)'), | 
| 81 | ], | 
| 82 | '#default_value' => $this->getSetting('granularity'), | 
| 83 | ], | 
| 84 | ]; | 
| 85 | } | 
| 86 | |
| 87 | /** | 
| 88 | * {@inheritdoc} | 
| 89 | */ | 
| 90 | public function settingsSummary() { | 
| 91 | return [t('Granularity: @g', ['@g' => $this->getSetting('granularity')])]; | 
| 92 | } | 
| 93 | |
| 94 | } |