Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
DetailTypeSelection | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
buildEntityQuery | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\crm\Plugin\EntityReferenceSelection; |
6 | |
7 | use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; |
8 | |
9 | /** |
10 | * Filters config entities based on the referencing entity's bundle. |
11 | * |
12 | * @EntityReferenceSelection( |
13 | * id = "default:crm_detail_type", |
14 | * label = @Translation("Default: CRM Detail Type"), |
15 | * entity_types = {"crm_detail_type"}, |
16 | * group = "default", |
17 | * weight = 0 |
18 | * ) |
19 | */ |
20 | class DetailTypeSelection extends DefaultSelection { |
21 | |
22 | /** |
23 | * {@inheritdoc} |
24 | */ |
25 | public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { |
26 | |
27 | $query = parent::buildEntityQuery($match, $match_operator); |
28 | /** @var \Drupal\Core\Entity\ContentEntityInterface $referencing_entity */ |
29 | $referencing_entity = $this->configuration['entity']; |
30 | $entity_type_id = $referencing_entity->getEntityTypeId(); |
31 | if ($entity_type_id !== 'crm_contact_detail') { |
32 | return $query; |
33 | } |
34 | |
35 | // This assumes the referencing entity is available during form building. |
36 | $bundle = $referencing_entity->bundle(); |
37 | |
38 | $query->condition('status', TRUE); |
39 | |
40 | // Case 1: Bundles is empty. |
41 | $empty = $query->andConditionGroup() |
42 | ->condition('bundles.0', NULL, 'IS NULL'); |
43 | |
44 | // Case 2: negate = FALSE and bundle is in bundles. |
45 | $match_bundle = $query->andConditionGroup() |
46 | ->condition('negate', FALSE) |
47 | ->condition('bundles.*', $bundle, 'CONTAINS'); |
48 | |
49 | // Case 3: negate = TRUE and bundle is in bundles. |
50 | // Entities excluded by bundle. |
51 | $not_query = parent::buildEntityQuery($match, $match_operator); |
52 | $not_query->condition('negate', TRUE) |
53 | ->condition('bundles.*', $bundle, 'CONTAINS'); |
54 | |
55 | $not_ids = $not_query->execute(); |
56 | |
57 | // The entities that are negated and do not match the bundle. |
58 | $ton_query = parent::buildEntityQuery($match, $match_operator); |
59 | $ton_query->condition('negate', TRUE) |
60 | ->condition('id', $not_ids, 'NOT IN'); |
61 | |
62 | $not_bundle = $query->andConditionGroup() |
63 | ->condition('id', $ton_query->execute(), 'IN'); |
64 | |
65 | $or = $query->orConditionGroup() |
66 | ->condition($empty) |
67 | ->condition($not_bundle) |
68 | ->condition($match_bundle); |
69 | |
70 | $query->condition($or); |
71 | |
72 | return $query; |
73 | } |
74 | |
75 | } |