Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| UserContactListBuilder | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| createInstance | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| render | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| buildHeader | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| buildRow | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getDefaultOperations | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm; |
| 4 | |
| 5 | use Drupal\Core\Database\Connection; |
| 6 | use Drupal\Core\Datetime\DateFormatterInterface; |
| 7 | use Drupal\Core\Entity\EntityInterface; |
| 8 | use Drupal\Core\Entity\EntityListBuilder; |
| 9 | use Drupal\Core\Entity\EntityStorageInterface; |
| 10 | use Drupal\Core\Entity\EntityTypeInterface; |
| 11 | use Drupal\Core\Routing\RedirectDestinationInterface; |
| 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Provides a list controller for the relation entity type. |
| 16 | */ |
| 17 | class UserContactListBuilder extends EntityListBuilder { |
| 18 | |
| 19 | /** |
| 20 | * The date formatter service. |
| 21 | * |
| 22 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
| 23 | */ |
| 24 | protected $dateFormatter; |
| 25 | |
| 26 | /** |
| 27 | * The redirect destination service. |
| 28 | * |
| 29 | * @var \Drupal\Core\Routing\RedirectDestinationInterface |
| 30 | */ |
| 31 | protected $redirectDestination; |
| 32 | |
| 33 | /** |
| 34 | * The database connection. |
| 35 | * |
| 36 | * @var \Drupal\Core\Database\Connection |
| 37 | */ |
| 38 | protected $database; |
| 39 | |
| 40 | /** |
| 41 | * Constructs a new UserContactListBuilder object. |
| 42 | * |
| 43 | * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
| 44 | * The entity type definition. |
| 45 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
| 46 | * The entity storage class. |
| 47 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
| 48 | * The date formatter service. |
| 49 | * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination |
| 50 | * The redirect destination service. |
| 51 | * @param \Drupal\Core\Database\Connection $database |
| 52 | * The database connection. |
| 53 | */ |
| 54 | public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination, Connection $database) { |
| 55 | parent::__construct($entity_type, $storage); |
| 56 | $this->dateFormatter = $date_formatter; |
| 57 | $this->redirectDestination = $redirect_destination; |
| 58 | $this->database = $database; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * {@inheritdoc} |
| 63 | */ |
| 64 | public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { |
| 65 | return new self( |
| 66 | $entity_type, |
| 67 | $container->get('entity_type.manager')->getStorage($entity_type->id()), |
| 68 | $container->get('date.formatter'), |
| 69 | $container->get('redirect.destination'), |
| 70 | $container->get('database'), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritdoc} |
| 76 | */ |
| 77 | public function render() { |
| 78 | $total = $this->database |
| 79 | ->query('SELECT COUNT(*) FROM {crm_user_contact}') |
| 80 | ->fetchField(); |
| 81 | |
| 82 | $build['summary']['#markup'] = $this->t('Total relations: @total', ['@total' => $total]); |
| 83 | |
| 84 | $build['table'] = parent::render(); |
| 85 | |
| 86 | return $build; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritdoc} |
| 91 | */ |
| 92 | public function buildHeader() { |
| 93 | $header['id'] = $this->t('ID'); |
| 94 | $header['user'] = $this->t('User'); |
| 95 | $header['crm_contact'] = $this->t('Person'); |
| 96 | |
| 97 | return $header + parent::buildHeader(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * {@inheritdoc} |
| 102 | */ |
| 103 | public function buildRow(EntityInterface $entity) { |
| 104 | /** @var \Drupal\crm\Entity\CrmUserContact $entity */ |
| 105 | $row['id'] = $entity->id(); |
| 106 | $row['user'] = $entity->getUser()?->label(); |
| 107 | $row['crm_contact'] = $entity->getContact()?->label(); |
| 108 | |
| 109 | return $row + parent::buildRow($entity); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * {@inheritdoc} |
| 114 | */ |
| 115 | protected function getDefaultOperations(EntityInterface $entity) { |
| 116 | $operations = parent::getDefaultOperations($entity); |
| 117 | $destination = $this->redirectDestination->getAsArray(); |
| 118 | foreach ($operations as $key => $operation) { |
| 119 | $operations[$key]['query'] = $destination; |
| 120 | } |
| 121 | return $operations; |
| 122 | } |
| 123 | |
| 124 | } |