-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisitor_tracking_inbound_code.module
More file actions
148 lines (116 loc) · 4.01 KB
/
visitor_tracking_inbound_code.module
File metadata and controls
148 lines (116 loc) · 4.01 KB
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
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @file
* Contains visitor_tracking_inbound_code.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
// @todo Support visitor_tracking module as a data plugin.
/**
* Implements hook_help().
*/
function visitor_tracking_inbound_code_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the visitor_tracking_inbound_code module.
case 'help.page.visitor_tracking_inbound_code':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Allows the persistent tracking of inbound URL parameters for visitors.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_entity_presave().
*/
function visitor_tracking_inbound_code_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
$fields = visitor_tracking_inbound_code_get_tracking_fields();
if ($entity instanceof \Drupal\Core\Entity\FieldableEntityInterface) {
$fill_fields = [];
if (array_key_exists($entity->getEntityTypeId(), $fields)) {
$fill_fields += $fields[$entity->getEntityTypeId()];
}
if (array_key_exists('*', $fields)) {
$fill_fields += $fields['*'];
}
visitor_tracking_inbound_code_fill_fields($entity, $fill_fields);
}
}
function visitor_tracking_inbound_code_fill_fields(\Drupal\Core\Entity\FieldableEntityInterface $entity, $fields) {
$tracking_codes = visitor_tracking_inbound_code_get_active();
if (!empty($tracking_codes)) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$value = [
'#theme' => 'visitor_tracking_inbound_code_tracking_codes',
'#tracking_codes' => $tracking_codes,
'#request_keys' => visitor_tracking_inbound_code_get_request_keys(),
];
/** @var \Drupal\Core\Render\Markup $markup */
$markup = $renderer->renderPlain($value);
$value = [0 => ['value' => $markup]];
foreach ($fields as $field_name) {
if ($entity->hasField($field_name) && $entity->get($field_name)->isEmpty()) {
$entity->set($field_name, $value);
}
}
}
}
/**
* Implements hook_theme().
*/
function visitor_tracking_inbound_code_theme($existing, $type, $theme, $path) {
return array(
'visitor_tracking_inbound_code_tracking_codes' => array(
'variables' => array(
'tracking_codes' => NULL,
'request_keys' => NULL,
),
),
);
}
/**
* Implements hook_entity_presave().
*/
function visitor_tracking_inbound_code_get_tracking_fields() {
$settings = \Drupal::config('visitor_tracking_inbound_code.settings');
$values = explode(',', $settings->get('tracking_fields'));
$fields = [];
foreach ($values as $value) {
$value = trim($value);
$entity_type = '*';
if (strpos($value, ':') !== FALSE) {
list($entity_type, $value) = explode(':', $value);
}
$fields[$entity_type][] = $value;
}
return $fields;
}
function visitor_tracking_inbound_code_get_request_keys() {
$settings = \Drupal::config('visitor_tracking_inbound_code.settings');
$values = explode(',', $settings->get('request_keys'));
$keys = [];
foreach ($values as $value) {
$key = trim($value);
$label = $key;
if (strpos($key, ':') !== FALSE) {
list($key, $label) = explode(':', $key);
}
$keys[$key] = t($label)->render();
}
return $keys;
}
function visitor_tracking_inbound_code_get_active() {
/** @var \Drupal\Core\Session\AccountProxyInterface $current_user */
$current_user = \Drupal::service('current_user');
$codes = [];
if ($current_user instanceof \Drupal\Core\Session\AccountProxyInterface && !is_null(\Drupal::requestStack()->getCurrentRequest()->getSession())) {
/** @var \Drupal\user\PrivateTempStoreFactory $private_tempstore_factory */
$private_tempstore_factory = \Drupal::service('user.private_tempstore');
$tempstore = $private_tempstore_factory->get('visitor_tracking_inbound_code');
$codes = $tempstore->get('tracking_codes');
if (empty($codes)) {
$codes = [];
}
}
return $codes;
}