Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ValidContactsSelection
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 buildEntityQuery
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\Plugin\EntityReferenceSelection;
6
7use Drupal\Core\Entity\Attribute\EntityReferenceSelection;
8use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
9use Drupal\Core\StringTranslation\TranslatableMarkup;
10
11/**
12 * Filters contacts to only those specified in valid_contact_ids.
13 */
14#[EntityReferenceSelection(
15  id: 'valid_contacts:crm_contact',
16  label: new TranslatableMarkup('Valid Contacts: CRM Contact'),
17  entity_types: ['crm_contact'],
18  group: 'valid_contacts',
19  weight: 0,
20)]
21class ValidContactsSelection extends DefaultSelection {
22
23  /**
24   * {@inheritdoc}
25   */
26  public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
27    $query = parent::buildEntityQuery($match, $match_operator);
28
29    // Check if valid_contact_ids is set in configuration.
30    if (!empty($this->configuration['valid_contact_ids'])) {
31      $valid_ids = $this->configuration['valid_contact_ids'];
32      // Ensure we have valid integers.
33      $valid_ids = array_map('intval', $valid_ids);
34      $valid_ids = array_filter($valid_ids);
35
36      if (!empty($valid_ids)) {
37        $query->condition('id', $valid_ids, 'IN');
38      }
39    }
40
41    return $query;
42  }
43
44}