-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm_fields_extra.module
138 lines (117 loc) · 3.9 KB
/
civicrm_fields_extra.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* @file
* Module 'civicrm_fields_extra'.
*/
/**
* Implements hook_entity_extra_field_info().
*/
function civicrm_fields_extra_entity_extra_field_info() {
// Fetch each CT containing civicrm fields.
$list = _entity_fields();
// Extra civicrm fields to be added.
$fields = _civicrm_fields();
foreach ($list as $civicrm_entity => $drupal_entity) {
// Check if fields need to be added for civicrm_entity.
if (!empty($fields[$civicrm_entity])) {
foreach ($drupal_entity as $entity => $entity_type) {
foreach ($entity_type as $type => $field) {
foreach ($fields[$civicrm_entity] as $key => $value) {
$code = "cf_" . $civicrm_entity . "_" . $entity . "_" . $type . "_" . $field . "_" . $key;
$extra[$entity][$type]['display'][$code] = [
'label' => '[extra] ' . $value,
'description' => $value,
'weight' => 0,
];
}
}
}
}
}
return $extra;
}
/**
* Implements hook_entity_view().
*/
function civicrm_fields_extra_entity_view(array &$build, $entity, $display, $view_mode) {
// Fetch each CT containing civicrm fields.
$list = _entity_fields();
// Extra civicrm fields to be added.
$fields = _civicrm_fields();
foreach ($list as $civicrm_entity => $drupal_entity) {
// Check if fields need to be added for civicrm_entity.
if (!empty($fields[$civicrm_entity])) {
foreach ($drupal_entity as $entity_key => $entity_type) {
// Check for this entity type.
if ($entity_key == $entity->getEntityTypeId()) {
foreach ($entity_type as $type => $field) {
// Check for this bundle.
if ($type == $entity->bundle()) {
// Fetch civicrm id from civicrm_field.
$id = $entity->get($field)->getString();
// Check if value is given.
if (!empty($id)) {
// @todo make 'return' params generic!!!
$civicrm_service = \Drupal::service('civicrm.service');
$params = [
'return' => ['event_type', 'title', 'start_date', 'end_date'],
'id' => $id,
];
// @todo check if api call is succesfull!!!
$data = $civicrm_service->api($civicrm_entity, 'GetSingle', $params);
foreach ($fields[$civicrm_entity] as $key => $value) {
$code = "cf_" . $civicrm_entity . "_" . $entity_key . "_" . $type . "_" . $field . "_" . $key;
$build[$code] = [
'#type' => 'markup',
'#markup' => $data[$key],
];
}
}
}
}
}
}
}
}
}
/**
* Define fields per CiviCRM entity.
*/
function _civicrm_fields() {
return [
'event' => [
'event_title' => 'Title',
],
];
}
/**
* Return all Drupal entities containing civicrm_fields.
*/
function _entity_fields() {
$list = [];
$entity_manager = \Drupal::service('entity_field.manager');
$field_map = $entity_manager->getFieldMap();
foreach ($field_map as $entity_type => $entity_fields) {
foreach ($entity_fields as $field_key => $field) {
// Find civicrm_field_contact.
if ($field['type'] == 'civicrm_field_contact') {
foreach ($field['bundles'] as $bundle) {
$list['contact'][$entity_type][$bundle] = $field_key;
}
}
// Find civicrm_field_event.
if ($field['type'] == 'civicrm_field_event') {
foreach ($field['bundles'] as $bundle) {
$list['event'][$entity_type][$bundle] = $field_key;
}
}
// Find civicrm_field_contribution_page.
if ($field['type'] == 'civicrm_field_contribution_page') {
foreach ($field['bundles'] as $bundle) {
$list['contribution_page'][$entity_type][$bundle] = $field_key;
}
}
}
}
return $list;
}