forked from holt83/ting_search_context
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ting_search_context.theme.inc
104 lines (91 loc) · 3.03 KB
/
ting_search_context.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
<?php
/**
* @file
* Theme functions used with the ting search context.
*/
/**
* Implements default preprocess function for the search context.
*/
function template_preprocess_ting_search_context(&$vars, $hook) {
// Add Javascript settings.
drupal_add_js(array('ting_search_context_id' => $vars['context_id']), 'setting');
drupal_add_js(array('ting_search_context_position' => $vars['position']), 'setting');
// Add JavaScript and CSS using wrapper functions.
_ting_search_context_add_javascript();
_ting_search_context_add_css();
}
/**
* Implements default preprocess function for the search context content.
*/
function template_preprocess_ting_search_context_content(&$vars, $hook) {
$node = $vars['node'];
$wrapper = entity_metadata_wrapper('node', $node);
// Attempt to find an image for the rated node.
// First priority is to use an image from a field on the node.
$image_uri = FALSE;
if ($field_name = ting_search_context_get_image_field_name($node)) {
if ($image = $wrapper->$field_name->value()) {
$image_uri = $image['uri'];
}
}
// If we didn't have a field name for the node type or if there was no image
// set on the field, use a random fallback image if any.
if (!$image) {
$fallback_images = variable_get('ting_search_context_fallback_images', array());
if (!empty($fallback_images)) {
$key = array_rand($fallback_images);
if ($image = file_load($fallback_images[$key])) {
$image_uri = $image->uri;
}
}
}
if ($image_uri) {
$vars['image'] = array(
'#theme' => 'image_style',
'#path' => $image_uri,
'#style_name' => 'ting_search_context',
);
}
// URL.
if ($node->type == 'ding_search_context_link') {
$url = $wrapper->field_search_context_link->url->value();
// One of the main usecases for ding_search_context_links is to link to
// search queries. Search queries are tricky because they are a mix of
// escaped and not escaped characters. This code handles it in a primitive
// fashion by only escaping the quotes.
//
// Quotes inside search queries need to be escaped.
$url = str_replace('"', '%22', $url);
// Only add Webtrends tracking params to internal links.
if (_ting_search_context_url_is_internal($url)) {
if (strpos($url, '?') === FALSE) {
$url .= '?WT.ac=searchcontext';
}
else {
$url .= '&WT.ac=searchcontext';
}
}
}
else {
$url = url('node/' . $node->nid, array('absolute' => TRUE, 'query' => array('WT.ac' => 'searchcontext')));
}
$vars['url'] = $url;
// Title.
$vars['title'] = $wrapper->title->value(array('sanitize' => TRUE));
}
/**
* Helper function to determine if a link is internal.
*
* Accounting for absolute internal links.
*/
function _ting_search_context_url_is_internal($url) {
$result = TRUE;
if (url_is_external($url)) {
// Still might be an internal absolute URL.
$parsed_url = parse_url($url);
if ($parsed_url['host'] !== $_SERVER['HTTP_HOST']) {
$result = FALSE;
}
}
return $result;
}