Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.00% covered (danger)
35.00%
7 / 20
0.00% covered (danger)
0.00%
0 / 4
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_crm_contact_access
53.85% covered (warning)
53.85%
7 / 13
0.00% covered (danger)
0.00%
0 / 1
7.46
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\Core\Access\AccessResult;
11use Drupal\Core\Entity\EntityInterface;
12use Drupal\Core\Session\AccountInterface;
13
14/**
15 * Prepares variables for contact templates.
16 *
17 * Default template: crm-contact.html.twig.
18 *
19 * @param array $variables
20 *   An associative array containing:
21 *   - elements: An associative array containing the contact information and any
22 *     fields attached to the entity.
23 *   - attributes: HTML attributes for the containing element.
24 */
25function template_preprocess_crm_contact(array &$variables) {
26  $variables['view_mode'] = $variables['elements']['#view_mode'];
27  foreach (Element::children($variables['elements']) as $key) {
28    $variables['content'][$key] = $variables['elements'][$key];
29  }
30}
31
32/**
33 * Prepares variables for crm relationship templates.
34 *
35 * Default template: crm-relationship.html.twig.
36 *
37 * @param array $variables
38 *   An associative array containing:
39 *   - elements: An associative array containing the relationship and any
40 *     fields attached to the entity.
41 *   - attributes: HTML attributes for the containing element.
42 */
43function template_preprocess_crm_relationship(array &$variables) {
44  $variables['view_mode'] = $variables['elements']['#view_mode'];
45  foreach (Element::children($variables['elements']) as $key) {
46    $variables['content'][$key] = $variables['elements'][$key];
47  }
48}
49
50/**
51 * Implements hook_form_FORM_ID_alter().
52 */
53function crm_form_block_form_alter(&$form, FormStateInterface $form_state) {
54  $form['#attached']['library'][] = 'crm/crm.block';
55}
56
57/**
58 * Implements hook_crm_contact_access().
59 */
60function crm_crm_contact_access(EntityInterface $entity, $operation, AccountInterface $account) {
61  if (!in_array($operation, ['view', 'update'])) {
62    return AccessResult::neutral();
63  }
64  $query = \Drupal::entityQuery('crm_user_contact');
65  $query->condition('crm_contact', $entity->id());
66  $query->condition('user', $account->id());
67  $result = $query->accessCheck(FALSE)->execute();
68  if (!$result) {
69    return AccessResult::neutral();
70  }
71
72  if ($operation == 'view') {
73    return AccessResult::allowedIfHasPermission($account, 'view mapped crm_contact');
74  }
75  elseif ($operation == 'update') {
76    return AccessResult::allowedIfHasPermission($account, 'edit mapped crm_contact');
77  }
78
79  return AccessResult::neutral();
80}