Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
RelationshipListBuilder | |
0.00% |
0 / 46 |
|
0.00% |
0 / 6 |
90 | |
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 / 17 |
|
0.00% |
0 / 1 |
6 | |||
buildHeader | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
buildRow | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getEntityListQuery | |
0.00% |
0 / 10 |
|
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 Drupal\Core\Entity\Query\QueryInterface; |
11 | use Symfony\Component\DependencyInjection\ContainerInterface; |
12 | |
13 | /** |
14 | * Provides a list controller for the crm relationship entity type. |
15 | */ |
16 | class RelationshipListBuilder extends EntityListBuilder { |
17 | |
18 | /** |
19 | * The date formatter service. |
20 | * |
21 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
22 | */ |
23 | protected $dateFormatter; |
24 | |
25 | /** |
26 | * Constructs a new RelationshipListBuilder object. |
27 | * |
28 | * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
29 | * The entity type definition. |
30 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
31 | * The entity storage class. |
32 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
33 | * The date formatter service. |
34 | */ |
35 | public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) { |
36 | parent::__construct($entity_type, $storage); |
37 | $this->dateFormatter = $date_formatter; |
38 | } |
39 | |
40 | /** |
41 | * {@inheritdoc} |
42 | */ |
43 | final public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { |
44 | return new self( |
45 | $entity_type, |
46 | $container->get('entity_type.manager')->getStorage($entity_type->id()), |
47 | $container->get('date.formatter') |
48 | ); |
49 | } |
50 | |
51 | /** |
52 | * {@inheritdoc} |
53 | */ |
54 | public function render() { |
55 | |
56 | $status = 1; |
57 | $build['status'] = [ |
58 | '#type' => 'select', |
59 | '#title' => $this->t('Status'), |
60 | '#options' => [ |
61 | -1 => $this->t('All'), |
62 | 1 => $this->t('Active'), |
63 | 0 => $this->t('Inactive'), |
64 | ], |
65 | '#default_value' => $status, |
66 | ]; |
67 | $build['table'] = parent::render(); |
68 | |
69 | $query = $this->getStorage()->getQuery(); |
70 | if ($status > -1) { |
71 | $query->condition($this->entityType->getKey('status'), $status); |
72 | } |
73 | $total = $query->accessCheck(FALSE)->count()->execute(); |
74 | |
75 | $build['summary']['#markup'] = $this->t('Total crm relationships: @total', ['@total' => $total]); |
76 | return $build; |
77 | } |
78 | |
79 | /** |
80 | * {@inheritdoc} |
81 | */ |
82 | public function buildHeader() { |
83 | $header['id'] = $this->t('ID'); |
84 | $header['label'] = $this->t('Label'); |
85 | $header['status'] = $this->t('Status'); |
86 | $header['created'] = $this->t('Created'); |
87 | $header['changed'] = $this->t('Updated'); |
88 | return $header + parent::buildHeader(); |
89 | } |
90 | |
91 | /** |
92 | * {@inheritdoc} |
93 | */ |
94 | public function buildRow(EntityInterface $entity) { |
95 | /** @var \Drupal\crm\CrmRelationshipInterface $entity */ |
96 | $row['id'] = $entity->id(); |
97 | $row['label'] = $entity->toLink(); |
98 | $row['status'] = $entity->get('status')->value ? $this->t('Enabled') : $this->t('Disabled'); |
99 | $row['created'] = $this->dateFormatter->format($entity->get('created')->value); |
100 | $row['changed'] = $this->dateFormatter->format($entity->getChangedTime()); |
101 | return $row + parent::buildRow($entity); |
102 | } |
103 | |
104 | /** |
105 | * Returns a query object for loading entity IDs from the storage. |
106 | * |
107 | * @return \Drupal\Core\Entity\Query\QueryInterface |
108 | * A query object used to load entity IDs. |
109 | */ |
110 | protected function getEntityListQuery() : QueryInterface { |
111 | $query = $this |
112 | ->getStorage() |
113 | ->getQuery() |
114 | ->condition($this->entityType->getKey('published'), TRUE) |
115 | ->accessCheck(TRUE) |
116 | ->sort($this->entityType->getKey('id')); |
117 | |
118 | // Only add the pager if a limit is specified. |
119 | if ($this->limit) { |
120 | $query |
121 | ->pager($this->limit); |
122 | } |
123 | return $query; |
124 | } |
125 | |
126 | } |