-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabletop.module
executable file
·267 lines (245 loc) · 7.9 KB
/
tabletop.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
267
<?php
/**
* @file
*
*/
/**
* Implements hook_library().
*/
function tabletop_library() {
return array(
'tabletop' => array(
'title' => 'Tabletop',
'website' => 'https://github.com/jsoma/tabletop',
'version' => 0,
'js' => array(
'sites/all/libraries/tabletop/src/tabletop.js' => array(),
),
),
'handlebars.js' => array(
'title' => 'Handlebars',
'website' => 'http://handlebarsjs.com', // @see 'http://github.com/wycats/handlebars.js'
'version' => 'v1.0.0-rc3',
'js' => array(
'sites/all/libraries/handlebars.js' => array(),
),
),
);
}
/**
* Implements hook_filter_info().
*/
function tabletop_filter_info() {
return array(
'no_triple_stash' => array(
'title' => 'Tabletop/Handlebars: No triple stash',
'description' => 'Prevent users from bypassing HTML-escaping in Handlebars templates by using {{{triple-stash}}} expressions.',
'tips callback' => 'tabletop_filter_no_triple_stash_tips',
'process callback' => 'tabletop_filter_no_triple_stash_process',
),
);
}
/**
* Implements hook_filter_FILTER_tips().
*/
function tabletop_filter_no_triple_stash_tips($filter, $format, $long) {
if ($long) {
return t('"Triple-stash" Handlebars {{{expressions}}} will be transformed to normal, HTML-escaped {{expressions}}.');
}
else {
return t('Handlebars expressions must use {{two}} braces, not {{{three}}}.');
}
}
/**
* Implements hook_filter_FILTER_process().
*/
function tabletop_filter_no_triple_stash_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$text = preg_replace('/\{\{\{(.+?)\}\}\}/', '{{$1}}', $text);
return $text;
}
/**
* @see field_example.module
*
* Providing a field requires:
* - Defining a field:
* - hook_field_info()
* - hook_field_schema()
* - hook_field_validate()
* - hook_field_is_empty()
*
* - Defining a formatter for the field (the portion that outputs the field for
* display):
* - hook_field_formatter_info()
* - hook_field_formatter_view()
*
* - Defining a widget for the edit form:
* - hook_field_widget_info()
* - hook_field_widget_form()
*/
/**
* Implements hook_field_info().
*/
function tabletop_field_info() {
return array(
'tabletop' => array(
'label' => t('Tabletop'),
'description' => t('Retrieves and templates data from a Google Spreadsheet.'),
'default_widget' => 'tabletop',
'default_formatter' => 'tabletop',
),
);
}
/**
* Implements hook_field_validate().
*/
function tabletop_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {}
}
/**
* Implements hook_field_is_empty().
*/
function tabletop_field_is_empty($item, $field) {
return empty($item['key']) && empty($item['sheet']) && empty($item['template']);
}
/**
* Implements hook_field_widget_info().
*/
function tabletop_field_widget_info() {
return array(
'tabletop' => array(
'label' => t('Tabletop'),
'field types' => array('tabletop'),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function tabletop_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($instance['widget']['type'] == 'tabletop') {
$value = isset($items[$delta]) ? $items[$delta] : array('key' => NULL, 'sheet' => NULL, 'template' => NULL);
$element += array(
'#type' => 'fieldset',
'#element_validate' => array('tabletop_tabletop_widget_validate'),
'#delta' => $delta,
'#attributes' => array('class' => array('edit-tabletop')),
'#attached' => array(
'library' => array(
array('tabletop', 'tabletop'),
array('tabletop', 'handlebars.js'),
),
'js' => array(
drupal_get_path('module', 'tabletop') . '/tabletop-edit.js',
drupal_get_path('module', 'tabletop') . '/tabletop-display.js',
),
),
);
$element['key'] = array(
'#type' => 'textfield',
'#title' => t('Key'),
'#description' => t('A Google Spreadsheet key or URL.'),
'#default_value' => $value['key'],
'#attributes' => array('class' => array('tabletop-key')),
'#required' => $instance['required'],
);
$element['sheet'] = array(
'#type' => 'textfield',
'#title' => t('Sheet name'),
'#description' => t('A sheet name from your Google Spreadsheet. Your sheet must have at least a header row and one data row.'),
'#default_value' => $value['sheet'],
'#attributes' => array('class' => array('tabletop-sheet', 'js-hide')),
'#required' => $instance['required'],
);
$element['template'] = array(
'#type' => 'text_format',
'#title' => t('Template'),
'#description' => t('A !handlebars template for your data.', array(
'!handlebars' => l('Handlebars', 'http://handlebarsjs.com/'),
)),
'#default_value' => $value['template'],
'#format' => $value['format'],
'#attributes' => array('class' => array('tabletop-template')),
'#required' => $instance['required'],
'#prefix' => '<div class="tabletop-model-help"><div>'
. t('Display your data in a template using !handlebars syntax; to get started, try !each.', array(
'!handlebars' => l('Handlebars', 'http://handlebarsjs.com/'),
'!each' => l('<code>{{#each data}}</code>', 'http://handlebarsjs.com/#iteration', array('html' => TRUE)),
))
. '</div><div class="tabletop-model-columns"></div></div>',
);
}
return $element;
}
/**
* Field validation.
*/
function tabletop_tabletop_widget_validate($element, &$form_state) {
$delta = $element['#delta'];
$field_name = $form_state['field'][$element['#field_name']][$element['#language']]['field']['field_name'];
$value = isset($form_state['values'][$field_name][$element['#language']][$delta]) ? $form_state['values'][$field_name][$element['#language']][$delta] : NULL;
// Rearrange the template fields from the widget.
$value['format'] = $value['template']['format'];
$value['template'] = $value['template']['value'];
form_set_value($element, $value, $form_state);
}
/**
* Implements hook_field_formatter_info().
*/
function tabletop_field_formatter_info() {
return array(
'tabletop' => array(
'label' => t('Tabletop'),
'field types' => array('tabletop'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function tabletop_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
if ($display['type'] == 'tabletop' && !empty($items)) {
$element = array(
'#attached' => array(
'library' => array(
array('tabletop', 'tabletop'),
array('tabletop', 'handlebars.js'),
),
'js' => array(
drupal_get_path('module', 'tabletop') . '/tabletop-display.js',
),
),
);
$js_data = array(
'tables' => array(),
'templates' => array(),
);
foreach ($items as $delta => $item) {
// Generate an HTML id for the template.
$id = drupal_html_id("tabletop-{$entity_type}");
// Output the template in a script tag.
$element[$delta] = array(
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => array(
'id' => $id,
'class' => array('tabletop-handlebars-template'),
'type' => 'text/x-handlebars-template',
),
'#value' => check_markup($item['template'], $item['format'], $langcode),
);
// Aggregate the tables by spreadsheet key, and associate spreadsheet
// keys with template ids.
$js_data['tables'][$item['key']][] = $item['sheet'];
$js_data['templates'][$id] = array(
'key' => $item['key'],
'sheet' => $item['sheet'],
);
}
$element['#attached']['js'][] = array(
'data' => array('tabletop' => $js_data),
'type' => 'setting',
);
}
return $element;
}