-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtipser_client.module
143 lines (126 loc) · 3.94 KB
/
tipser_client.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
<?php
/**
* @file
* Contains tipser_client.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Cache\Cache;
use Drupal\tipser_client\TipserClient;
/**
* Implements hook_help().
*/
function tipser_client_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.tipser_client':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Tipser Client') . '</p>';
return $output;
default:
return '';
}
}
/**
* Implements hook_theme().
*/
function tipser_client_theme() {
return [
'tipser_product_page' => [
'variables' => [
'productId' => null,
'article_url' => null,
],
],
];
}
/**
* Implements hook_preprocess_region() for header_large.
*/
function tipser_client_preprocess_region__header_large(&$variables) {
$variables['include_tipser_icon'] = TipserClient::isActivated() && TipserClient::iconIsActivated();
}
/**
* Implements hook_library_info_alter().
*/
function tipser_client_library_info_alter(&$libraries, $extension) {
// Set version for tipser frontend library from sdk.js timestamp.
if (isset($libraries['frontend'])
&& isset($libraries['frontend']['js']['js/tipser.js'])
&& file_exists('public://tipser/tipser.sdk.js')) {
$date = filemtime('public://tipser/tipser.sdk.js');
$libraries['frontend']['version'] = $date;
}
}
/**
* Override title metatag for /tipser-products/ page
*
* Implements hook_metatags_alter().
*/
function tipser_client_metatags_alter(array &$metatags, array &$context) {
if ('tipser_client.open_from_amp_page' === \Drupal::routeMatch()->getRouteName()) {
$metatags['title'] = '[current-page:title]';
}
}
function tipser_client_preprocess_html(&$variables) {
if ('tipser_client.open_from_amp_page' === \Drupal::routeMatch()->getRouteName()) {
foreach ($variables['page']['#attached']['html_head'] as $i => &$htmlHead) {
if (isset($htmlHead[1]) && $htmlHead[1] === 'robots') {
$htmlHead[0]['#attributes']['content'] = 'noindex, nofollow, noarchive, nosnippet, noodp, noydir, noimageindex, notranslate';
}
}
}
if (TipserClient::isActivated()) {
$variables['#attached']['drupalSettings']['tipser'] = [
'userid' => \Drupal::config('tipser_client.config')->get('tipser_pos'),
'env' => \Drupal::config('tipser_client.config')->get('tipser_env'),
];
}
}
/**
* Implements hook_page_attachments_alter().
* @param array $page
*/
function tipser_client_page_attachments_alter(array &$page) {
$config = \Drupal::config('tipser_client.config');
if ($config->get('tipser_activated') && false === \Drupal::service('router.admin_context')->isAdminRoute()) {
$page['#attached']['library'][] = 'tipser_client/frontend';
}
}
/**
* Taken from infinite_odoscope_update_library_source
*
* @param string $source
* the URL of the tipser library. If source starts with http://
* a http call is made and the response body is written to target
* otherwise the source will be written to target as is.
* @param string $target
* the URI of the file where to store the source code e.g.
* public://tipser/tipser.sdk.js
*
* @return bool
* true on success otherwise false or exception
* @throws Exception
*/
function tipser_update_library_source($source, $target){
if ('' === $source) {
throw new \Exception('Tipser library source not set.');
}
if(preg_match('/^https?:\/\//', $source)) {
$client = Drupal::httpClient();
$response = $client->get($source);
if($response->getStatusCode() != 200) {
return FALSE;
}
$response_body = $response->getBody()->getContents();
}
else {
$response_body = $source;
}
$parts = explode('/', $target);
array_pop($parts);
$path = implode('/', $parts);
if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
Cache::invalidateTags(['library_info']);
return (file_put_contents($target, $response_body) !== FALSE);
}
}