-
Notifications
You must be signed in to change notification settings - Fork 4
/
ad_entity.theme.inc
144 lines (128 loc) · 4.83 KB
/
ad_entity.theme.inc
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
<?php
/**
* @file
* Advertising Entity theme implementations.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Crypt;
/**
* Preprocess implementation for viewing an Advertising entity.
*
* @param array &$variables
* An array of variables.
*/
function template_preprocess_ad_entity(array &$variables) {
/** @var \Drupal\ad_entity\Entity\AdEntityInterface $ad_entity */
$ad_entity = $variables['ad_entity'];
$view_handler = $ad_entity->getViewPlugin();
if (empty($view_handler)) {
// Without a view handler, there's no output definition.
return;
}
$definition = $view_handler->getPluginDefinition();
// Pass through information about which container to use.
$variables['container'] = $definition['container'];
// Let the assigned view handler build the content.
$variables['content'] = $view_handler->build($ad_entity);
// It only makes sense to generate attributes and assets
// when the ad container is being rendered as Html.
if ($variables['container'] !== 'html') {
return;
}
// Pass through global frontend settings.
$variables['settings'] = _ad_entity_get_js_settings();
// Generate attributes.
$id = 'ad-entity-' . Crypt::randomBytesBase64(8);
$attributes = new Attribute(['id' => $id]);
$attributes->addClass('ad-entity-container');
if ($ad_entity->get('disable_initialization')) {
$attributes->addClass('initialization-disabled');
}
$attributes->addClass('not-initialized');
$attributes->setAttribute('data-ad-entity', $ad_entity->id());
$attributes->setAttribute('data-ad-entity-type', $ad_entity->get('type_plugin_id'));
$attributes->setAttribute('data-ad-entity-view', $ad_entity->get('view_plugin_id'));
// Let attributes defined by other components
// add or override the generated ones.
if (!empty($ad_entity->_attributes)) {
foreach ($ad_entity->_attributes as $name => $value) {
if ($name == 'class') {
$attributes->addClass($value);
}
else {
$attributes->setAttribute($name, $value);
}
}
}
$variables['attributes'] = $attributes;
// Insert targeting from backend context data.
$targeting_collection = $ad_entity->getTargetingFromContextData();
if (!$targeting_collection->isEmpty()) {
// Filter the targeting information before being displayed.
$targeting_collection->filter();
$variables['targeting'] = $targeting_collection->toJson();
}
else {
$variables['targeting'] = '{}';
}
// Attach the initial JS for viewing Advertising entities.
$variables['#attached']['library'][] = 'ad_entity/view';
// Attach the JS implementation for the view handler.
if (!empty($definition['library'])) {
$variables['#attached']['library'][] = $definition['library'];
// If it makes sense, also attach the viewready.js library.
if (isset($definition['requiresDomready']) && ($definition['requiresDomready'] === FALSE)) {
$variables['#attached']['library'][] = 'ad_entity/viewready';
}
}
}
/**
* Preprocess implementation for delivering Advertising context.
*
* @param array &$variables
* An array of variables.
*/
function template_preprocess_ad_entity_context(array &$variables) {
$item = $variables['item'];
$definition = $variables['definition'];
$id = $definition['id'];
$context_settings = $item->get('context_settings')->getValue();
$apply_on = $item->get('apply_on')->getValue();
$variables['context_id'] = $id;
// Build the context object.
$variables['context'] = ['context_id' => $id];
if (!empty($apply_on)) {
$variables['context']['apply_on'] = $apply_on;
}
if (!empty($context_settings[$id])) {
$variables['context']['settings'] = $context_settings[$id];
}
// Attach the fundamental JS implementation for applying Advertising context.
$variables['#attached']['library'][] = 'ad_entity/context';
// Attach the JS implementation for the context plugin.
$variables['#attached']['library'][] = $definition['library'];
}
/**
* Preprocess implementation for viewing a display config as iFrame.
*
* @param array &$variables
* An array of variables.
*/
function template_preprocess_ad_display_iframe(array &$variables) {
/** @var \Drupal\ad_entity\Entity\AdDisplayInterface $ad_display */
$ad_display = $variables['ad_display'];
$attributes = [
'width' => $variables['width'],
'height' => $variables['height'],
'class' => ['ad-display-iframe'],
];
if ($ad_display->isNew()) {
\Drupal::logger('ad_entity')->error(t('Tried to view a new, unsaved Advertising display config via iFrame. Only saved configs can be viewed via iFrame.'));
$variables['#cache']['max-age'] = 0;
$attributes['srcdoc'] = t('The display config can only be viewed when it is saved.');
}
else {
$attributes['src'] = $ad_display->toUrl('canonical')->setAbsolute(TRUE)->toString();
}
$variables['attributes'] = new Attribute($attributes);
}