Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ContactListBuilder | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
42 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
createInstance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
render | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
buildHeader | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
buildRow | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Drupal\crm; |
4 | |
5 | use Drupal\Core\Datetime\DateFormatterInterface; |
6 | use Drupal\Core\Entity\EntityInterface; |
7 | use Drupal\Core\Entity\EntityListBuilder; |
8 | use Drupal\Core\Entity\EntityStorageInterface; |
9 | use Drupal\Core\Entity\EntityTypeInterface; |
10 | use Symfony\Component\DependencyInjection\ContainerInterface; |
11 | |
12 | /** |
13 | * Provides a list controller for the contact entity type. |
14 | */ |
15 | class ContactListBuilder extends EntityListBuilder { |
16 | |
17 | /** |
18 | * The date formatter service. |
19 | * |
20 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
21 | */ |
22 | protected $dateFormatter; |
23 | |
24 | /** |
25 | * Constructs a new ContactListBuilder object. |
26 | * |
27 | * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
28 | * The entity type definition. |
29 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
30 | * The entity storage class. |
31 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
32 | * The date formatter service. |
33 | */ |
34 | public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) { |
35 | parent::__construct($entity_type, $storage); |
36 | $this->dateFormatter = $date_formatter; |
37 | } |
38 | |
39 | /** |
40 | * {@inheritdoc} |
41 | */ |
42 | final public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { |
43 | return new self( |
44 | $entity_type, |
45 | $container->get('entity_type.manager')->getStorage($entity_type->id()), |
46 | $container->get('date.formatter') |
47 | ); |
48 | } |
49 | |
50 | /** |
51 | * {@inheritdoc} |
52 | */ |
53 | public function render() { |
54 | $build['table'] = parent::render(); |
55 | |
56 | $total = $this->getStorage() |
57 | ->getQuery() |
58 | ->accessCheck(FALSE) |
59 | ->count() |
60 | ->execute(); |
61 | |
62 | $build['summary']['#markup'] = $this->t('Total contacts: @total', ['@total' => $total]); |
63 | return $build; |
64 | } |
65 | |
66 | /** |
67 | * {@inheritdoc} |
68 | */ |
69 | public function buildHeader() { |
70 | $header['id'] = $this->t('ID'); |
71 | $header['name'] = $this->t('Name'); |
72 | $header['type'] = $this->t('Type'); |
73 | $header['status'] = $this->t('Status'); |
74 | $header['created'] = $this->t('Created'); |
75 | $header['changed'] = $this->t('Updated'); |
76 | |
77 | return $header + parent::buildHeader(); |
78 | } |
79 | |
80 | /** |
81 | * {@inheritdoc} |
82 | */ |
83 | public function buildRow(EntityInterface $entity) { |
84 | /** @var \Drupal\crm\CrmContactInterface $entity */ |
85 | $row['id'] = $entity->id(); |
86 | $row['name'] = $entity->toLink(); |
87 | $row['type'] = $entity->bundle->entity->label(); |
88 | $row['status'] = $entity->get('status')->value ? $this->t('Active') : $this->t('Inactive'); |
89 | $row['created'] = $this->dateFormatter->format($entity->get('created')->value); |
90 | $row['changed'] = $this->dateFormatter->format($entity->getChangedTime()); |
91 | |
92 | return $row + parent::buildRow($entity); |
93 | } |
94 | |
95 | } |