Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactDetailListBuilder
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 createInstance
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 buildHeader
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 buildRow
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 render
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getDefaultOperations
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getReferenceCount
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm;
6
7use Drupal\Core\Entity\EntityInterface;
8use Drupal\Core\Entity\EntityListBuilder;
9use Drupal\Core\Url;
10use Drupal\Core\Entity\EntityTypeInterface;
11use Drupal\Core\Entity\EntityStorageInterface;
12use Symfony\Component\DependencyInjection\ContainerInterface;
13
14/**
15 * Provides a list controller for the CRM contact detail entity type.
16 */
17class ContactDetailListBuilder extends EntityListBuilder {
18
19  /**
20   * The entity storage class for the contact entity.
21   *
22   * @var \Drupal\Core\Entity\EntityStorageInterface
23   */
24  protected $contactStorage;
25
26  /**
27   * Constructs a new ContactDetailListBuilder object.
28   *
29   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30   *   The entity type definition.
31   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
32   *   The entity storage class.
33   * @param \Drupal\Core\Entity\EntityStorageInterface $contact_storage
34   *   The entity storage class for the contact entity.
35   */
36  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityStorageInterface $contact_storage) {
37    parent::__construct($entity_type, $storage);
38    $this->contactStorage = $contact_storage;
39  }
40
41  /**
42   * {@inheritdoc}
43   */
44  final public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
45    return new self(
46      $entity_type,
47      $container->get('entity_type.manager')->getStorage($entity_type->id()),
48      $container->get('entity_type.manager')->getStorage('crm_contact'),
49    );
50  }
51
52  /**
53   * {@inheritdoc}
54   */
55  public function buildHeader(): array {
56    $header['id'] = $this->t('ID');
57    $header['label'] = $this->t('Label');
58    $header['type'] = $this->t('Type');
59    $header['reference'] = $this->t('Reference');
60    $header['status'] = $this->t('Status');
61    $header['created'] = $this->t('Created');
62    $header['changed'] = $this->t('Updated');
63    return $header + parent::buildHeader();
64  }
65
66  /**
67   * {@inheritdoc}
68   */
69  public function buildRow(EntityInterface $entity): array {
70    $bundle = $entity->get('bundle')->entity;
71    /** @var \Drupal\crm\CRMContactDetailInterface $entity */
72    $row['id'] = $entity->id();
73    $row['label'] = $entity->label();
74    $row['type'] = $bundle->label();
75    $row['references'] = $this->getReferenceCount($entity->id(), $bundle->id());
76    $row['status'] = $entity->get('status')->value ? $this->t('Enabled') : $this->t('Disabled');
77    $row['created']['data'] = $entity->get('created')->view(['label' => 'hidden']);
78    $row['changed']['data'] = $entity->get('changed')->view(['label' => 'hidden']);
79    return $row + parent::buildRow($entity);
80  }
81
82  /**
83   * {@inheritdoc}
84   */
85  public function render() {
86    $build = parent::render();
87
88    $build['table']['#empty'] = $this->t(
89      'No crm detail types available. <a href=":link">Add crm detail type</a>.',
90      [':link' => Url::fromRoute('entity.crm_detail_type.add_form')->toString()]
91    );
92    $total = $this->getStorage()
93      ->getQuery()
94      ->accessCheck(FALSE)
95      ->count()
96      ->execute();
97
98    $build['summary']['#markup'] = $this->t('Total details: @total', ['@total' => $total]);
99
100    return $build;
101  }
102
103  /**
104   * {@inheritdoc}
105   */
106  public function getDefaultOperations(EntityInterface $entity) {
107    $operations = parent::getDefaultOperations($entity);
108    // Place the edit operation after the operations added by field_ui.module
109    // which have the weights 15, 20, 25.
110    if (isset($operations['edit'])) {
111      $operations['edit']['weight'] = 30;
112    }
113    return $operations;
114  }
115
116  /**
117   * Get the number of references to a contact detail.
118   *
119   * @param mixed $id
120   *   The ID of the contact detail.
121   * @param string $bundle
122   *   The bundle of the contact detail.
123   *
124   * @return int
125   *   The number of references to the contact detail.
126   */
127  protected function getReferenceCount($id, string $bundle) : int {
128    $query = $this->contactStorage->getQuery();
129
130    switch ($bundle) {
131      case 'email':
132        $query->condition('emails', $id);
133        break;
134
135      case 'telephone':
136        $query->condition('telephones', $id);
137        break;
138
139      case 'address':
140        $query->condition('addresses', $id);
141        break;
142    }
143
144    return $query->accessCheck(FALSE)->count()->execute();
145  }
146
147}