Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AgeFieldItemList
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 computeValue
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\Field;
6
7use Drupal\Core\Field\FieldItemList;
8use Drupal\Core\TypedData\ComputedItemListTrait;
9
10/**
11 * The age field item list.
12 */
13class AgeFieldItemList extends FieldItemList {
14  use ComputedItemListTrait;
15
16  /**
17   * {@inheritdoc}
18   */
19  protected function computeValue() {
20    $entity = $this->getEntity();
21
22    $start = $entity->get('start_date')->value;
23    $end = $entity->get('end_date')->value;
24
25    if ($start) {
26      $start_date = new \DateTime($start);
27      $end_date = new \DateTime($end ?: 'now');
28
29      $interval = $start_date->diff($end_date);
30      $days = (int) $interval->format('%a');
31
32      $this->list[0] = $this->createItem(0, $days);
33    }
34  }
35
36}