Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
crm_install
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @file
5 * Provides a contact entity type.
6 */
7
8declare(strict_types=1);
9
10use Drupal\Core\Datetime\DrupalDateTime;
11
12/**
13 * Implements hook_install().
14 */
15function crm_install() {
16  // Lock the person contact type.
17  \Drupal::state()->set('crm.contact_type.locked', [
18    'person' => 'crm',
19  ]);
20
21  $entity_type_manager = \Drupal::entityTypeManager();
22  $site = \Drupal::config('system.site');
23  $name = $site->get('name') ?? 'Default Organization';
24  $mail = $site->get('mail');
25  $storage = $entity_type_manager->getStorage('crm_contact');
26  $detail_storage = $entity_type_manager->getStorage('crm_contact_detail');
27
28  $org_email = $detail_storage->create([
29    'bundle' => 'email',
30    'email' => $mail,
31    'type' => 'main',
32    'crm_contact' => 1,
33  ]);
34  $org_email->save();
35  $org = $storage->create([
36    'name' => $name,
37    'bundle' => 'organization',
38    'emails' => [
39      'target_id' => $org_email->id(),
40    ],
41  ]);
42  $org->save();
43
44  $user_storage = $entity_type_manager->getStorage('user');
45  $user = $user_storage->load(1);
46
47  $user_email = $detail_storage->create([
48    'bundle' => 'email',
49    'email' => $user->get('mail')->value,
50    'type' => 'main',
51    'crm_contact' => 2,
52  ]);
53  $user_email->save();
54
55  $storage->create([
56    'full_name' => ['given' => $user->get('name')->value],
57    'bundle' => 'person',
58    'emails' => [
59      'target_id' => $user_email->id(),
60    ],
61  ])->save();
62
63  $crm_user_contact_storage = $entity_type_manager->getStorage('crm_user_contact');
64  $crm_user_contact_storage->create([
65    'user' => 1,
66    'crm_contact' => 2,
67  ])->save();
68
69  $crm_relationship_storage = $entity_type_manager->getStorage('crm_relationship');
70  $crm_relationship_storage->create([
71    'bundle' => 'employee',
72    'contacts' => [2, 1],
73    'start_date' => (new DrupalDateTime('now'))->format('Y-m-d'),
74  ])->save();
75
76}