Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
199 / 199
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Contact
100.00% covered (success)
100.00%
199 / 199
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 preSave
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 baseFieldDefinitions
100.00% covered (success)
100.00%
191 / 191
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\crm\Entity;
4
5use Drupal\Core\Entity\EditorialContentEntityBase;
6use Drupal\Core\Entity\EntityStorageInterface;
7use Drupal\Core\Entity\EntityTypeInterface;
8use Drupal\Core\Field\BaseFieldDefinition;
9use Drupal\crm\CrmContactInterface;
10use Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex;
11use Drupal\user\EntityOwnerTrait;
12
13/**
14 * Defines the contact entity class.
15 *
16 * @ContentEntityType(
17 *   id = "crm_contact",
18 *   label = @Translation("CRM Contact"),
19 *   label_collection = @Translation("CRM Contacts"),
20 *   label_singular = @Translation("crm contact"),
21 *   label_plural = @Translation("contacts"),
22 *   label_count = @PluralTranslation(
23 *     singular = "@count crm contacts",
24 *     plural = "@count crm contacts",
25 *   ),
26 *   bundle_label = @Translation("Contact type"),
27 *   handlers = {
28 *     "list_builder" = "Drupal\crm\ContactListBuilder",
29 *     "views_data" = "Drupal\views\EntityViewsData",
30 *     "access" = "Drupal\crm\ContactAccessControlHandler",
31 *     "form" = {
32 *       "add" = "Drupal\crm\Form\ContactForm",
33 *       "edit" = "Drupal\crm\Form\ContactForm",
34 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
35 *     },
36 *     "route_provider" = {
37 *       "html" = "Drupal\Core\Entity\Routing\AdminHtmlRouteProvider",
38 *     }
39 *   },
40 *   base_table = "crm_contact",
41 *   revision_table = "crm_contact_revision",
42 *   show_revision_ui = TRUE,
43 *   admin_permission = "administer crm contact types",
44 *   entity_keys = {
45 *     "id" = "id",
46 *     "revision" = "revision_id",
47 *     "bundle" = "bundle",
48 *     "label" = "name",
49 *     "uuid" = "uuid",
50 *     "status" = "status",
51 *     "published" = "status",
52 *     "owner" = "uid",
53 *     "uid" = "uid",
54 *   },
55 *   revision_metadata_keys = {
56 *    "revision_user" = "revision_uid",
57 *     "revision_created" = "revision_timestamp",
58 *     "revision_log_message" = "revision_log",
59 *   },
60 *   links = {
61 *     "collection" = "/admin/content/crm/contact",
62 *     "add-form" = "/crm/contact/add/{crm_contact_type}",
63 *     "add-page" = "/crm/contact/add",
64 *     "canonical" = "/crm/contact/{crm_contact}",
65 *     "edit-form" = "/crm/contact/{crm_contact}/edit",
66 *     "delete-form" = "/crm/contact/{crm_contact}/delete",
67 *   },
68 *   bundle_entity_type = "crm_contact_type",
69 *   field_ui_base_route = "entity.crm_contact_type.edit_form",
70 * )
71 */
72class Contact extends EditorialContentEntityBase implements CrmContactInterface {
73
74  use EntityOwnerTrait;
75
76  /**
77   * {@inheritdoc}
78   */
79  public function preSave(EntityStorageInterface $storage) {
80    parent::preSave($storage);
81    $this->setNewRevision();
82
83    // If type is person, set the label field to the name field.
84    if ($this->bundle() == 'person') {
85      $formatted_name = NULL;
86      $name_array = $this->get('full_name')->getValue();
87      if ($name_array != NULL) {
88        $name_formatter = \Drupal::service('name.formatter');
89        $formatted_name = $name_formatter->formatList($name_array);
90        $this->set('name', $formatted_name);
91      }
92    }
93
94  }
95
96  /**
97   * {@inheritdoc}
98   */
99  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
100    $fields = parent::baseFieldDefinitions($entity_type);
101    $fields += static::ownerBaseFieldDefinitions($entity_type);
102
103    $fields['primary'] = BaseFieldDefinition::create('entity_reference')
104      ->setLabel('Primary Contact')
105      ->setSetting('target_type', 'crm_contact')
106      ->setDisplayConfigurable('form', TRUE)
107      ->setDisplayOptions('form', [
108        'type' => 'entity_reference_autocomplete',
109        'weight' => 1,
110        'settings' => [
111          'match_operator' => 'CONTAINS',
112          'size' => '60',
113          'placeholder' => '',
114        ],
115      ])
116      ->setDisplayConfigurable('view', TRUE)
117      ->setDisplayOptions('view', [
118        'label' => 'before',
119        'type' => 'entity_reference_label',
120        'weight' => 0,
121      ]);
122
123    $fields['name'] = BaseFieldDefinition::create('string')
124      ->setRevisionable(TRUE)
125      ->setLabel(t('Name'))
126      ->setRequired(TRUE)
127      ->setSetting('max_length', 255)
128      ->setDisplayOptions('form', [
129        'type' => 'string_textfield',
130        'weight' => -5,
131      ])
132      ->setDisplayConfigurable('form', TRUE)
133      ->setDisplayOptions('view', [
134        'label' => 'hidden',
135        'type' => 'string',
136        'weight' => -5,
137      ])
138      ->setDisplayConfigurable('view', TRUE);
139
140    $fields['emails'] = BaseFieldDefinition::create('entity_reference')
141      ->setLabel(t('Emails'))
142      ->setRevisionable(TRUE)
143      ->setCardinality(-1)
144      ->setSetting('target_type', 'crm_contact_detail')
145      ->setSetting('handler_settings', ['target_bundles' => ['email']])
146      ->setDisplayConfigurable('form', TRUE)
147      ->setDisplayOptions('form', [
148        'type' => 'inline_entity_form_complex',
149        'weight' => 8,
150        'settings' => [
151          'allow_new' => TRUE,
152          'allow_existing' => FALSE,
153          'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE,
154          'match_operator' => 'CONTAINS',
155          'allow_duplicate' => FALSE,
156          'form_mode' => 'default',
157          'override_labels' => TRUE,
158          'label_singular' => t('email'),
159          'label_plural' => t('emails'),
160          'collapsible' => FALSE,
161          'collapsed' => FALSE,
162          'revision' => TRUE,
163        ],
164      ])
165      ->setDisplayConfigurable('view', TRUE)
166      ->setDisplayOptions('view', [
167        'label' => 'hidden',
168        'type' => 'entity_reference_entity_view',
169        'weight' => -5,
170        'settings' => [
171          'view_mode' => 'default',
172          'link' => FALSE,
173        ],
174      ]);
175
176    $fields['telephones'] = BaseFieldDefinition::create('entity_reference')
177      ->setLabel(t('Telephones'))
178      ->setRevisionable(TRUE)
179      ->setCardinality(-1)
180      ->setSetting('target_type', 'crm_contact_detail')
181      ->setSetting('handler_settings', ['target_bundles' => ['telephone']])
182      ->setDisplayConfigurable('form', TRUE)
183      ->setDisplayOptions('form', [
184        'type' => 'inline_entity_form_complex',
185        'weight' => 9,
186        'settings' => [
187          'allow_new' => TRUE,
188          'allow_existing' => FALSE,
189          'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE,
190          'match_operator' => 'CONTAINS',
191          'allow_duplicate' => FALSE,
192          'form_mode' => 'default',
193          'override_labels' => TRUE,
194          'label_singular' => t('telephone'),
195          'label_plural' => t('telephones'),
196          'collapsible' => FALSE,
197          'collapsed' => FALSE,
198          'revision' => TRUE,
199        ],
200      ])
201      ->setDisplayConfigurable('view', TRUE)
202      ->setDisplayOptions('view', [
203        'label' => 'hidden',
204        'type' => 'entity_reference_entity_view',
205        'weight' => -5,
206        'settings' => [
207          'view_mode' => 'default',
208          'link' => FALSE,
209        ],
210      ]);
211
212    $fields['addresses'] = BaseFieldDefinition::create('entity_reference')
213      ->setLabel('Addresses')
214      ->setRevisionable(TRUE)
215      ->setCardinality(-1)
216      ->setSetting('target_type', 'crm_contact_detail')
217      ->setSetting('handler_settings', ['target_bundles' => ['address']])
218      ->setDisplayConfigurable('form', TRUE)
219      ->setDisplayOptions('form', [
220        'type' => 'inline_entity_form_complex',
221        'weight' => 10,
222        'settings' => [
223          'allow_new' => TRUE,
224          'allow_existing' => FALSE,
225          'removed_reference' => InlineEntityFormComplex::REMOVED_DELETE,
226          'match_operator' => 'CONTAINS',
227          'allow_duplicate' => FALSE,
228          'form_mode' => 'default',
229          'override_labels' => TRUE,
230          'label_singular' => t('address'),
231          'label_plural' => t('addresses'),
232          'collapsible' => FALSE,
233          'collapsed' => FALSE,
234          'revision' => TRUE,
235        ],
236      ])
237      ->setDisplayConfigurable('view', TRUE)
238      ->setDisplayOptions('view', [
239        'label' => 'hidden',
240        'type' => 'entity_reference_entity_view',
241        'weight' => 0,
242        'settings' => [
243          'view_mode' => 'default',
244          'link' => FALSE,
245        ],
246      ]);
247
248    $fields['uid']
249      ->setLabel(t('Owned by'))
250      ->setDescription(t('The username of the content author.'))
251      ->setRevisionable(TRUE)
252      ->setDisplayOptions('view', [
253        'label' => 'hidden',
254        'type' => 'author',
255        'weight' => 0,
256      ])
257      ->setDisplayOptions('form', [
258        'type' => 'entity_reference_autocomplete',
259        'weight' => 5,
260        'settings' => [
261          'match_operator' => 'CONTAINS',
262          'size' => '60',
263          'placeholder' => '',
264        ],
265      ])
266      ->setDisplayConfigurable('form', TRUE);
267
268    $fields['status']
269      ->setDisplayOptions('form', [
270        'type' => 'boolean_checkbox',
271        'settings' => [
272          'display_label' => TRUE,
273        ],
274        'weight' => 120,
275      ])
276      ->setLabel(t('Status'))
277      ->setDefaultValue(TRUE)
278      ->setSetting('on_label', 'Status')
279      ->setDisplayConfigurable('form', TRUE);
280
281    $fields['created'] = BaseFieldDefinition::create('created')
282      ->setLabel(t('Created on'))
283      ->setDescription(t('The time that the contact was created.'))
284      ->setDisplayOptions('view', [
285        'label' => 'above',
286        'type' => 'timestamp',
287        'weight' => 20,
288      ])
289      ->setDisplayConfigurable('form', TRUE)
290      ->setDisplayOptions('form', [
291        'type' => 'datetime_timestamp',
292        'weight' => 20,
293      ])
294      ->setDisplayConfigurable('view', TRUE);
295
296    $fields['changed'] = BaseFieldDefinition::create('changed')
297      ->setLabel(t('Changed'))
298      ->setDescription(t('The time that the contact was last edited.'));
299
300    return $fields;
301  }
302
303}