-
Notifications
You must be signed in to change notification settings - Fork 5
/
ad_integration.module
233 lines (216 loc) · 6.48 KB
/
ad_integration.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* @file
* Contains module hooks.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_theme().
*/
function ad_integration_theme() {
return array(
'ad_slot_inline' => array(
'template' => 'ad-slot-inline',
'variables' => array(
'html_id' => NULL,
'ad_tag' => NULL,
),
),
'ad_slot_iframe' => array(
'template' => 'ad-slot-iframe',
'variables' => array(
'html_id' => NULL,
'ad_tag' => NULL,
),
),
'ad_slot_fia' => array(
'template' => 'ad-slot-fia',
'variables' => array(
'referrer' => NULL,
'ad_tag' => NULL,
'data' => NULL,
'adengine' => NULL,
),
),
);
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*/
function ad_integration_form_node_form_alter(&$form, FormStateInterface $form_state) {
/* Is there a better way to get the node? */
$entity = $form_state->getBuildInfo()['callback_object']->getEntity();
/*
* Set group of all ad_integration_settings elements
*/
$has_settings = FALSE;
foreach ($entity->getFieldDefinitions() as $fieldDefinition) {
if ($fieldDefinition->getType() === 'ad_integration_settings') {
$has_settings = TRUE;
$form[$fieldDefinition->getName()]['#group'] = 'ad_integration_settings_override';
}
}
if ($has_settings) {
if (!isset($form['advanced'])) {
$form['advanced'] = array(
'#type' => 'vertical_tabs',
'#weight' => 99,
);
}
$form['ad_integration_settings_override'] = array(
'#type' => 'details',
'#title' => t('Advertising settings'),
'#open' => FALSE,
'#group' => 'advanced',
'#optional' => FALSE,
);
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for taxonomy_term_form().
*/
function ad_integration_form_taxonomy_term_form_alter(&$form, FormStateInterface $form_state) {
ad_integration_form_node_form_alter($form, $form_state);
}
/**
* Implements template_preprocess_html().
*/
function ad_integration_page_attachments(array &$page) {
/* @var Drupal\ad_integration\AdIntegration $advertisingService */
$advertisingService = \Drupal::service('ad_integration');
$ad_provider = $advertisingService->getAdProvider();
$page['#cache']['tags'] = Cache::mergeTags(isset($page['#cache']['tags']) ? $page['#cache']['tags'] : [], $advertisingService->getCacheTags());
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'src' => '/' . drupal_get_path('module', 'ad_integration') . '/js/adHelper.js',
],
],
'ad_helper',
];
switch ($ad_provider) {
case 'fag':
_attach_fag($page, $advertisingService);
break;
case 'orbyd':
_attach_orbyd($page, $advertisingService);
break;
}
}
/**
* Adds the ad template for Facebook Instant Articles
*
* Implements hook_preprocess_views_view_row_fia().
*/
function ad_integration_preprocess_views_view_row_fia(&$variables) {
/** @var \Drupal\ad_integration\AdIntegrationInterface $advertisingService */
$advertisingService = \Drupal::service('ad_integration');
$ad_provider = $advertisingService->getAdProvider();
$config = Drupal::config('ad_integration.settings');
$ad_tag = $config->get('adsc_fia_tag');
$node = $variables['row']['#node'];
$data = array('entity' => $node);
if ($ad_provider == 'orbyd' && !empty($ad_tag)) {
$adRender = array(
'#theme' => 'ad_slot_fia',
'#referrer' => $GLOBALS['base_url'],
'#ad_tag' => $ad_tag,
'#data' => array(
'adunit1' => $advertisingService->getAdUnit1($data),
'adunit2' => $advertisingService->getAdUnit2($data),
'adunit3' => $advertisingService->getAdUnit3($data),
'keyword' => $advertisingService->getKeyword($data),
'admode' => $advertisingService->getAdMode($data),
),
);
$adengine = $advertisingService->getAdEngine();
if ($adengine) {
$adRender += array(
'#adengine' => $adengine,
);
}
$adHtml = render($adRender);
$variables['options']['automatic_ad'] = $adHtml;
}
}
/**
* @param array $page
* @param $advertisingService
* @return array
*/
function _attach_orbyd(array &$page, $advertisingService) {
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'text/javascript',
],
'#value' => "var adunit1 = '" . $advertisingService->getAdUnit1() . "';
var adunit2 = '" . $advertisingService->getAdUnit2() . "';
var adunit3 = '" . $advertisingService->getAdUnit3() . "';
var admode = '" . $advertisingService->getAdMode() . "';
var adkeyword = '" . $advertisingService->getKeyword() . "';
var admobile = deviceIsMobile().toString();
var addevice = getDeviceType();",
],
'advertising_config',
];
$adengine = $advertisingService->getAdEngine();
if ($adengine) {
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'src' => $adengine,
'type' => 'text/javascript',
],
],
'ad_orbyd_head',
];
}
}
/**
* @param array $page
* @param $advertisingService
* @return array
*/
function _attach_fag(array &$page, $advertisingService) {
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'text/javascript',
],
'#value' => "var adsc_adunit1 = '" . $advertisingService->getAdUnit1() . "';
var adsc_adunit2 = '" . $advertisingService->getAdUnit2() . "';
var adsc_adunit3 = '" . $advertisingService->getAdUnit3() . "';
var adsc_mode = '" . $advertisingService->getAdMode() . "';
var adsc_keyword = '" . $advertisingService->getKeyword() . "';
var adsc_mobile = deviceIsMobile().toString();
var adsc_device = getDeviceType();",
],
'advertising_config',
];
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'src' => $advertisingService->getAdContainerTag(),
],
],
'container_tag',
];
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'src' => $advertisingService->getAdEngine(),
],
],
'ad_engine',
];
$page['#attached']['library'][] = 'ad_integration/id_integration';
return $page;
}