Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.14% covered (warning)
82.14%
23 / 28
66.67% covered (warning)
66.67%
8 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
RelationshipType
82.14% covered (warning)
82.14%
23 / 28
66.67% covered (warning)
66.67%
8 / 12
18.65
0.00% covered (danger)
0.00%
0 / 1
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDescription
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 isLocked
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 calculateDependencies
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 getLimitA
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getLimitB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isLimitActiveOnly
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidContactsA
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getValidContactsB
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isReadonlyContactA
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isReadonlyContactB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 save
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Drupal\crm\Entity;
4
5use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6use Drupal\Core\Entity\Attribute\ConfigEntityType;
7use Drupal\Core\Entity\EntityDeleteForm;
8use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider;
9use Drupal\Core\StringTranslation\TranslatableMarkup;
10use Drupal\crm\ListBuilder\RelationshipTypeListBuilder;
11use Drupal\crm\Form\RelationshipTypeForm;
12
13/**
14 * Defines the CRM Relationship type configuration entity.
15 */
16#[ConfigEntityType(
17  id: 'crm_relationship_type',
18  label: new TranslatableMarkup('Relationship type'),
19  label_collection: new TranslatableMarkup('Relationship types'),
20  label_singular: new TranslatableMarkup('relationship type'),
21  label_plural: new TranslatableMarkup('relationships types'),
22  label_count: [
23    'singular' => '@count relationships type',
24    'plural' => '@count relationships types',
25  ],
26  handlers: [
27    'form' => [
28      'add' => RelationshipTypeForm::class,
29      'edit' => RelationshipTypeForm::class,
30      'delete' => EntityDeleteForm::class,
31    ],
32    'list_builder' => RelationshipTypeListBuilder::class,
33    'route_provider' => [
34      'html' => AdminHtmlRouteProvider::class,
35    ],
36  ],
37  admin_permission: 'administer crm',
38  bundle_of: 'crm_relationship',
39  config_prefix: 'crm_relationship_type',
40  entity_keys: [
41    'id' => 'id',
42    'label' => 'label',
43    'uuid' => 'uuid',
44  ],
45  links: [
46    'add-form' => '/admin/structure/crm/relationship_types/add',
47    'edit-form' => '/admin/structure/crm/relationship_types/manage/{crm_relationship_type}',
48    'delete-form' => '/admin/structure/crm/relationship_types/manage/{crm_relationship_type}/delete',
49    'collection' => '/admin/structure/crm/relationship_types',
50  ],
51  config_export: [
52    'id',
53    'label',
54    'description',
55    'asymmetric',
56    'uuid',
57    'contact_type_a',
58    'contact_type_b',
59    'label_a',
60    'label_a_plural',
61    'label_b',
62    'label_b_plural',
63    'limit_a',
64    'limit_b',
65    'limit_active_only',
66    'valid_contacts_a',
67    'valid_contacts_b',
68    'readonly_contact_a',
69    'readonly_contact_b',
70  ],
71)]
72class RelationshipType extends ConfigEntityBundleBase implements RelationshipTypeInterface {
73
74  /**
75   * The machine name of this crm relationship type.
76   *
77   * @var string
78   */
79  protected $id;
80
81  /**
82   * The human-readable name of the crm relationship type.
83   *
84   * @var string
85   */
86  protected $label;
87
88  /**
89   * The description of the crm relationship type.
90   *
91   * @var string
92   */
93  protected $description;
94
95  /**
96   * Is the bundle and label the same for both sides of the relationship?
97   *
98   * @var bool
99   */
100  protected $asymmetric;
101
102  /**
103   * The human-readable name of the crm relationship type.
104   *
105   * @var string
106   */
107  protected $label_a;
108
109  /**
110   * The plural form of label_a.
111   *
112   * @var string
113   */
114  protected $label_a_plural;
115
116  /**
117   * The human-readable name of the crm relationship type.
118   *
119   * @var string
120   */
121  protected $label_b;
122
123  /**
124   * The plural form of label_b.
125   *
126   * @var string
127   */
128  protected $label_b_plural;
129
130  /**
131   * The contact types for the first contact in the relationship.
132   *
133   * @var array
134   */
135  protected $contact_type_a;
136
137  /**
138   * The contact types for the second contact in the relationship.
139   *
140   * @var array
141   */
142  protected $contact_type_b;
143
144  /**
145   * Maximum number of relationships where a contact can be in position A.
146   *
147   * @var int|null
148   */
149  protected $limit_a;
150
151  /**
152   * Maximum number of relationships where a contact can be in position B.
153   *
154   * @var int|null
155   */
156  protected $limit_b;
157
158  /**
159   * Whether to only count active relationships toward the limit.
160   *
161   * @var bool
162   */
163  protected $limit_active_only = FALSE;
164
165  /**
166   * Valid contact IDs for the first contact in the relationship.
167   *
168   * @var array
169   */
170  protected $valid_contacts_a = [];
171
172  /**
173   * Valid contact IDs for the second contact in the relationship.
174   *
175   * @var array
176   */
177  protected $valid_contacts_b = [];
178
179  /**
180   * Whether Contact A is read-only after being set.
181   *
182   * @var bool
183   */
184  protected $readonly_contact_a = TRUE;
185
186  /**
187   * Whether Contact B is read-only after being set.
188   *
189   * @var bool
190   */
191  protected $readonly_contact_b = TRUE;
192
193  /**
194   * {@inheritdoc}
195   */
196  public function getDescription() {
197    return $this->description ?? '';
198  }
199
200  /**
201   * {@inheritdoc}
202   */
203  public function setDescription($description) {
204    $this->description = $description;
205
206    return $this;
207  }
208
209  /**
210   * Is the relationship type locked?
211   */
212  public function isLocked() {
213    $locked = \Drupal::state()->get('crm.relationship_type.locked');
214    return $locked[$this->id()] ?? FALSE;
215  }
216
217  /**
218   * {@inheritdoc}
219   */
220  public function calculateDependencies() {
221    parent::calculateDependencies();
222
223    if ($type_a = $this->get('contact_type_a')) {
224      foreach ($type_a as $type) {
225        $this->addDependency('config', 'crm.crm_contact_type.' . $type);
226      }
227    }
228    if ($type_b = $this->get('contact_type_b')) {
229      foreach ($type_b as $type) {
230        $this->addDependency('config', 'crm.crm_contact_type.' . $type);
231      }
232    }
233
234    return $this;
235  }
236
237  /**
238   * {@inheritdoc}
239   */
240  public function getLimitA(): ?int {
241    return $this->limit_a;
242  }
243
244  /**
245   * {@inheritdoc}
246   */
247  public function getLimitB(): ?int {
248    return $this->limit_b;
249  }
250
251  /**
252   * {@inheritdoc}
253   */
254  public function isLimitActiveOnly(): bool {
255    return (bool) $this->limit_active_only;
256  }
257
258  /**
259   * {@inheritdoc}
260   */
261  public function getValidContactsA(): array {
262    return $this->valid_contacts_a ?? [];
263  }
264
265  /**
266   * {@inheritdoc}
267   */
268  public function getValidContactsB(): array {
269    return $this->valid_contacts_b ?? [];
270  }
271
272  /**
273   * {@inheritdoc}
274   */
275  public function isReadonlyContactA(): bool {
276    return (bool) ($this->readonly_contact_a ?? TRUE);
277  }
278
279  /**
280   * {@inheritdoc}
281   */
282  public function isReadonlyContactB(): bool {
283    return (bool) ($this->readonly_contact_b ?? TRUE);
284  }
285
286  /**
287   * {@inheritdoc}
288   */
289  public function save() {
290    if (!$this->get('asymmetric')) {
291      $this->set('label_b', $this->get('label_a'));
292      $this->set('label_b_plural', $this->get('label_a_plural'));
293      $this->set('contact_type_b', $this->get('contact_type_a'));
294      $this->set('limit_b', $this->get('limit_a'));
295      $this->set('valid_contacts_b', $this->get('valid_contacts_a'));
296      $this->set('readonly_contact_b', $this->get('readonly_contact_a'));
297    }
298
299    return parent::save();
300  }
301
302}