-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhax.module
266 lines (232 loc) · 7.5 KB
/
hax.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* @file
* Headless Authoring eXperience, accept calls and make things haxable.
*/
use Drupal\hax\HaxService;
/**
* Implements hook_preprocess_hook().
*
* Add cms-hax tag and related markup as wrapper to body field.
*/
function hax_preprocess_field(&$vars) {
// Supporting attachments handled in hax_page_attachments().
// Allow for cache variability based on route name so this can be handled
// evaluated separately for the view and the hax mode tabs.
$vars['#cache']['contexts'][] = 'route.name';
if (!hax_is_current_route_supported()) {
return;
}
$entity_type = $vars['element']['#object']->getEntityTypeId();
$view_mode = $vars['element']['#view_mode'];
if ($vars['field_name'] == 'body' && $entity_type == 'node' && $view_mode == 'full'
&& \Drupal::config('hax.settings')->get('hax_autoload_element_node_view')) {
// Generate autoload list.
$elementstring = \Drupal::config('hax.settings')
->get('hax_autoload_element_list');
// Blow up based on space.
$elements = explode(' ', $elementstring);
$components = '';
foreach ($elements as $element) {
if (!empty($element)) {
$components .= '<' . $element . ' slot="autoloader">' . '</' . $element . '>';
}
}
$appStoreConnection = json_encode([
'url' => base_path() . 'hax-app-store/' . \Drupal::csrfToken()->get(),
]);
// Get the NID from current path.
$request = \Drupal::request();
$current_path = $request->getPathInfo();
$path_args = explode('/', $current_path);
$node_id = $path_args[2];
// TODO Not ideal, but we've fallen back to using the body text instead of
// prefix/suffix due to filtering. This will go through the node's selected
// text format for render.
// @todo: convert to string replacement function or template.
$vars['items'][0]['content']['#text'] = '<cms-hax open-default end-point="' . base_path() . 'hax-node-save/' . $node_id . '/' . \Drupal::csrfToken()
->get() . '" body-offset-left="' . \Drupal::config('hax.settings')
->get('hax_offset_left') . '" app-store-connection=' . "'" . $appStoreConnection . "'" . '>'
. $components . $vars['items'][0]['content']['#text'] . '</cms-hax>';
}
}
/**
* Implements hook_hax_app_store().
*/
function hax_hax_app_store() {
$config = \Drupal::config('hax.settings');
$hax = new HaxService();
$apikeys = [];
$baseApps = $hax->baseSupportedApps();
foreach ($baseApps as $key => $app) {
if ($config->get('hax_' . $key . '_key') != '') {
$apikeys[$key] = $config->get('hax_' . $key . '_key');
}
}
$json = $hax->loadBaseAppStore($apikeys);
// Pull in the core ones we supply.
if (\Drupal::moduleHandler()->moduleExists('file')) {
$tmp = json_decode(_hax_site_connection());
array_push($json, $tmp);
}
return $json;
}
/**
* Hax site connection.
*
* Connection details for this site. This is where all the really important
* stuff is that will make people freak out.
*/
function _hax_site_connection() {
global $base_url;
$parts = explode('://', $base_url);
// Built in support when file_entity and restws is in place.
$title = t('Internal files');
$json = '{
"details": {
"title": "' . $title . '",
"icon": "perm-media",
"color": "light-blue",
"author": "Drupal",
"description": "Drupal site integration for HAX",
"tags": ["media", "drupal"]
},
"connection": {
"protocol": "' . $parts[0] . '",
"url": "' . $parts[1] . '",
"operations": {
"browse": {
"method": "GET",
"endPoint": "file.json",
"pagination": {
"style": "link",
"props": {
"first": "page.first",
"next": "page.next",
"previous": "page.previous",
"last": "page.last"
}
},
"search": {
},
"data": {
},
"resultMap": {
"defaultGizmoType": "image",
"items": "list",
"preview": {
"title": "name",
"details": "mime",
"image": "url",
"id": "uuid"
},
"gizmo": {
"source": "url",
"id": "uuid",
"title": "name",
"type": "type"
}
}
},
"add": {
"method": "POST",
"endPoint": "hax-file-save/' . \Drupal::csrfToken()->get() . '",
"acceptsGizmoTypes": [
"image",
"video",
"audio",
"pdf",
"svg",
"document",
"csv"
],
"resultMap": {
"item": "data.file",
"defaultGizmoType": "image",
"gizmo": {
"source": "url",
"id": "uuid"
}
}
}
}
}
}';
return $json;
}
/**
* Implements hook_page_attachments().
*
* Load all attachments for this page.
*/
function hax_page_attachments(array &$attachments) {
// Need this to execute on variations of route.name to support view and
// hax mode tab variation.
// @todo: As written, this code has no impact. Need to impact $attachments.
// Unnecessary, it appears. Remnant from paste from hax_preprocess_field().
// Slated for removal, once completely confirmed.
//$vars['#cache']['contexts'][] = 'route.name';
// Slated for removal - unless we determine we're shotgunning too much with
// our attachments, in which case we'd need a wider
// hax_is_current_route_supported() determination that will allow us to have
// the attachments on both the hax mode and node view pages.
//if (!hax_is_current_route_supported()) {
// return;
//}
// Fake a component to get it into the head of the document, heavy weighting.
$component = new \stdClass();
$component->machine_name = 'cms-hax';
$component->file = 'libraries/webcomponents/polymer/bower_components/cms-hax/cms-hax.html';
// The #tag is the html tag.
// Set up an array of attributes inside the tag.
$element = [
'#tag' => 'link',
'#attributes' => [
'href' => base_path() . $component->file,
'rel' => 'import',
],
];
$attachments['#attached']['html_head'][] = [
$element,
'webcomponent-' . $component->machine_name,
];
// TODO do we need the autoload attachments here, too?
// Generate autoload list.
$element_string = \Drupal::config('hax.settings')
->get('hax_autoload_element_list');
// Blow up based on space.
$elements = explode(' ', $element_string);
foreach ($elements as $element) {
$component = new \stdClass();
$component->machine_name = $element;
// Pull in from webcomponents location.
$component->file = 'libraries/webcomponents/polymer/bower_components/' . $element . '/' . $element . '.html';
// The #tag is the html tag.
// Set up an array of attributes inside the tag.
$element = [
'#tag' => 'link',
'#attributes' => [
'href' => base_path() . $component->file,
'rel' => 'import',
],
];
// Add components to html_head via $vars.
$attachments['#attached']['html_head'][] = [
$element,
'webcomponent-' . $component->machine_name,
];
}
}
/**
* Identify whether the current route is supported for HAX deployment.
*
* @return bool
* TRUE if the current route is supported.
*/
function hax_is_current_route_supported() {
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name == 'hax.node_form') {
return TRUE;
}
return FALSE;
}