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