Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
DetailTypeListBuilder
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
4 / 4
7
100.00% covered (success)
100.00%
1 / 1
 createInstance
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 buildHeader
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 buildRow
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Drupal\crm;
4
5use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6use Drupal\Core\Entity\EntityInterface;
7use Drupal\Core\Entity\EntityStorageInterface;
8use Drupal\Core\Entity\EntityTypeInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
11/**
12 * Provides a listing of Detail types.
13 */
14class DetailTypeListBuilder extends ConfigEntityListBuilder {
15
16  /**
17   * Contact Detail Type storage.
18   *
19   * @var \Drupal\Core\Entity\EntityStorageInterface
20   */
21  protected $contactDetailTypeStorage;
22
23  /**
24   * {@inheritdoc}
25   */
26  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
27    return new static(
28      $entity_type,
29      $container->get('entity_type.manager')->getStorage($entity_type->id()),
30      $container->get('entity_type.manager')->getStorage('crm_contact_detail_type'),
31    );
32  }
33
34  /**
35   * Constructs a new EntityListBuilder object.
36   *
37   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
38   *   The entity type definition.
39   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
40   *   The entity storage class.
41   * @param \Drupal\Core\Entity\EntityStorageInterface $contact_detail_type_storage
42   *   The entity storage class.
43   */
44  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, EntityStorageInterface $contact_detail_type_storage) {
45    parent::__construct($entity_type, $storage);
46
47    $this->contactDetailTypeStorage = $contact_detail_type_storage;
48  }
49
50  /**
51   * {@inheritdoc}
52   */
53  public function buildHeader() {
54    $header['label'] = $this->t('Label');
55    $header['id'] = $this->t('Machine name');
56    $header['status'] = $this->t('Status');
57    $header['bundles'] = $this->t('Bundles');
58
59    return $header + parent::buildHeader();
60  }
61
62  /**
63   * {@inheritdoc}
64   */
65  public function buildRow(EntityInterface $entity) {
66
67    $bundle_ids = $entity->get('bundles');
68    $contact_detail_types = $this->contactDetailTypeStorage->loadMultiple($bundle_ids) ?? [];
69    $bundles = array_map(
70      fn($contact_detail_type): string => $contact_detail_type->label(),
71       $contact_detail_types);
72
73    if (empty($bundles)) {
74      $bundle = $this->t('Any');
75    }
76    elseif ($entity->get('negate')) {
77      $bundle = $this->t('Except: @bundles', ['@bundles' => implode(',', $bundles)]);
78    }
79    else {
80      $bundle = $this->t('Only: @bundles', ['@bundles' => implode(',', $bundles)]);
81    }
82
83    /** @var \Drupal\crm\CrmDetailTypeInterface $entity */
84    $row['label'] = $entity->label();
85    $row['id'] = $entity->id();
86    $row['status'] = $entity->status() ? $this->t('Enabled') : $this->t('Disabled');
87    $row['bundles'] = $bundle;
88
89    return $row + parent::buildRow($entity);
90  }
91
92}