Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.00% covered (success)
94.00%
47 / 50
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContactMethodListBuilder
94.00% covered (success)
94.00%
47 / 50
71.43% covered (warning)
71.43%
5 / 7
12.03
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
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 buildHeader
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 buildRow
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 render
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultOperations
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getReferenceCount
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\ListBuilder;
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 method entity type.
16 */
17class ContactMethodListBuilder 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 ContactMethodListBuilder 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\ContactMethodInterface $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 method types available. <a href=":link">Add crm method type</a>.',
90      [':link' => Url::fromRoute('entity.crm_method_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 methods: @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 method.
118   *
119   * @param mixed $id
120   *   The ID of the contact method.
121   * @param string $bundle
122   *   The bundle of the contact method.
123   *
124   * @return int
125   *   The number of references to the contact method.
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}