Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.39% covered (danger)
17.39%
8 / 46
0.00% covered (danger)
0.00%
0 / 7
CRAP
n/a
0 / 0
template_preprocess_crm_contact
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
template_preprocess_crm_relationship
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
crm_form_block_form_alter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
crm_form_crm_contact_type_edit_form_alter
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
6
crm_form_crm_contact_type_edit_form_submit
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
crm_crm_contact_access
53.85% covered (warning)
53.85%
7 / 13
0.00% covered (danger)
0.00%
0 / 1
7.46
crm_modules_installed
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
1<?php
2
3/**
4 * @file
5 * Provides a contact entity type.
6 */
7
8use Drupal\Core\Render\Element;
9use Drupal\Core\Form\FormStateInterface;
10use Drupal\name\Entity\NameFormat;
11use Drupal\Core\Access\AccessResult;
12use Drupal\Core\Entity\EntityInterface;
13use Drupal\Core\Session\AccountInterface;
14
15/**
16 * Prepares variables for contact templates.
17 *
18 * Default template: crm-contact.html.twig.
19 *
20 * @param array $variables
21 *   An associative array containing:
22 *   - elements: An associative array containing the contact information and any
23 *     fields attached to the entity.
24 *   - attributes: HTML attributes for the containing element.
25 */
26function template_preprocess_crm_contact(array &$variables) {
27  $variables['view_mode'] = $variables['elements']['#view_mode'];
28  foreach (Element::children($variables['elements']) as $key) {
29    $variables['content'][$key] = $variables['elements'][$key];
30  }
31}
32
33/**
34 * Prepares variables for crm relationship templates.
35 *
36 * Default template: crm-relationship.html.twig.
37 *
38 * @param array $variables
39 *   An associative array containing:
40 *   - elements: An associative array containing the relationship and any
41 *     fields attached to the entity.
42 *   - attributes: HTML attributes for the containing element.
43 */
44function template_preprocess_crm_relationship(array &$variables) {
45  $variables['view_mode'] = $variables['elements']['#view_mode'];
46  foreach (Element::children($variables['elements']) as $key) {
47    $variables['content'][$key] = $variables['elements'][$key];
48  }
49}
50
51/**
52 * Implements hook_form_FORM_ID_alter().
53 */
54function crm_form_block_form_alter(&$form, FormStateInterface $form_state) {
55  $form['#attached']['library'][] = 'crm/crm.block';
56}
57
58/**
59 * Implements hook_form_FORM_ID_alter().
60 */
61function crm_form_crm_contact_type_edit_form_alter(&$form, FormStateInterface $form_state) {
62  $entity = $form_state->getFormObject()->getEntity();
63  if ($entity->id() != 'person') {
64    return;
65  }
66
67  $format_options = NameFormat::loadMultiple();
68  $format_options = array_map(function ($format) {
69    return $format->label();
70  }, $format_options);
71
72  $form['name'] = [
73    '#type' => 'details',
74    '#title' => t('Name'),
75    '#group' => 'additional_settings',
76  ];
77
78  $form['name']['name_format'] = [
79    '#type' => 'select',
80    '#title' => t('Name Format'),
81    '#description' => t('Select the format for displaying names for this contact type.'),
82    '#options' => $format_options,
83    '#default_value' => $entity->getThirdPartySetting('crm', 'name_format', 'default'),
84  ];
85  $form['actions']['submit']['#submit'][] = 'crm_form_crm_contact_type_edit_form_submit';
86}
87
88/**
89 * Submit handler for the contact type edit form.
90 */
91function crm_form_crm_contact_type_edit_form_submit(array &$form, FormStateInterface $form_state) {
92  $name_format = $form_state->getValue('name_format');
93  // Third party settings.
94  $entity = $form_state->getFormObject()->getEntity();
95  $entity->setThirdPartySetting('crm', 'name_format', $name_format);
96  $entity->save();
97}
98
99/**
100 * Implements hook_crm_contact_access().
101 */
102function crm_crm_contact_access(EntityInterface $entity, $operation, AccountInterface $account) {
103  if (!in_array($operation, ['view', 'update'])) {
104    return AccessResult::neutral();
105  }
106  $query = \Drupal::entityQuery('crm_user_contact');
107  $query->condition('crm_contact', $entity->id());
108  $query->condition('user', $account->id());
109  $result = $query->accessCheck(FALSE)->execute();
110  if (!$result) {
111    return AccessResult::neutral();
112  }
113
114  if ($operation == 'view') {
115    return AccessResult::allowedIfHasPermission($account, 'view mapped crm_contact');
116  }
117  elseif ($operation == 'update') {
118    return AccessResult::allowedIfHasPermission($account, 'edit mapped crm_contact');
119  }
120
121  return AccessResult::neutral();
122}
123
124/**
125 * Implements hook_modules_installed().
126 */
127function crm_modules_installed(array $modules) {
128  if (in_array('crm_field', $modules)) {
129    \Drupal::service('module_installer')->install(['crm_field']);
130  }
131}