-
Notifications
You must be signed in to change notification settings - Fork 5
/
insert.module
458 lines (405 loc) · 15 KB
/
insert.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* @file
* Allows insertion of files, images, and other media directly into the body
* field by using an "Insert" button next to the uploaded file.
*/
// Load default implementations of insert hooks for core modules.
require_once dirname(__FILE__) . '/includes/file.inc';
require_once dirname(__FILE__) . '/includes/image.inc';
require_once dirname(__FILE__) . '/includes/insert.inc';
/**
* Implements hook_menu().
*/
function insert_menu() {
// A page in the Reports section to show usage of insert in all fields
if (module_exists('field_ui')) {
$items['admin/reports/fields/list'] = array(
'title' => 'List fields',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/reports/fields/insert'] = array(
'title' => 'Insert presets',
'description' => 'Insert presets enabled.',
'page callback' => 'insert_fields_list',
'access arguments' => array('administer content types'),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
'file' => 'insert.admin.inc',
);
}
return $items;
}
/**
* Implements hook_config_info().
*/
function insert_config_info() {
$prefixes['insert.settings'] = array(
'label' => t('Settings'),
'group' => t('Insert'),
);
return $prefixes;
}
/**
* Implements hook_element_info().
*/
function insert_element_info() {
$extra = array('#after_build' => array('insert_element_process'));
$elements = array();
foreach (insert_widgets() as $widget_type => $widget) {
$element_type = isset($widget['element_type']) ? $widget['element_type'] : $widget_type;
$elements[$element_type] = $extra;
}
return $elements;
}
/**
* Implements hook_theme().
*/
function insert_theme() {
return array(
'insert_widget' => array(
'render element' => 'element',
'template' => 'templates/insert-widget',
),
'insert_field_widget_settings_styles' => array(
'render element' => 'element',
),
// Theme functions in includes/insert.inc.
'insert_image' => array(
'variables' => array('item' => NULL, 'widget' => NULL),
'template' => 'templates/insert-image',
'file' => 'includes/insert.inc',
),
'insert_link' => array(
'variables' => array('item' => NULL, 'widget' => NULL),
'template' => 'templates/insert-link',
'file' => 'includes/insert.inc',
),
'insert_icon_link' => array(
'variables' => array('item' => NULL, 'widget' => NULL),
'template' => 'templates/insert-icon-link',
'file' => 'includes/insert.inc',
),
// Theme functions in includes/image.inc.
'image_insert_image' => array(
'variables' => array('item' => NULL, 'widget' => NULL, 'style_name' => NULL),
'template' => 'templates/image-insert-image',
'pattern' => 'image_insert_image__[a-z0-9_]+',
'file' => 'includes/image.inc',
),
);
}
/**
* Get a list of all supported image styles.
*/
function insert_styles($reset = FALSE) {
static $styles;
if (!isset($styles) || $reset) {
$styles = array();
foreach (module_implements('insert_styles') as $module) {
$module_styles = module_invoke($module, 'insert_styles');
foreach ($module_styles as $name => $style) {
$module_styles[$name]['name'] = $name;
$module_styles[$name]['module'] = $module;
}
$styles = array_merge($styles, $module_styles);
}
backdrop_alter('insert_styles', $styles);
uasort($styles, '_insert_style_sort');
}
return $styles;
}
/**
* Sort the styles.
*/
function _insert_style_sort($a, $b) {
$a = (array)$a + array('weight' => 0, 'label' => '');
$b = (array)$b + array('weight' => 0, 'label' => '');
return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : strnatcasecmp($a['label'], $b['label']));
}
/**
* Load an individual insert style.
*/
function insert_style_load($style_name) {
$styles = insert_styles();
return isset($styles[$style_name]) ? $styles[$style_name] : FALSE;
}
/**
* Get a list of styles suitable for an #options array.
*/
function insert_styles_list() {
$list = array();
foreach (insert_styles() as $name => $style) {
$list[$name] = $style['label'];
}
return $list;
}
/**
* Get a list of all supported field widgets.
*/
function insert_widgets($reset = FALSE) {
static $widgets;
if (!isset($widgets) || $reset) {
$widgets = array();
foreach (module_implements('insert_widgets') as $module) {
$module_widgets = module_invoke($module, 'insert_widgets');
foreach ($module_widgets as $type => $widget) {
$module_widgets[$type]['type'] = $type;
$module_widgets[$type]['module'] = $module;
}
$widgets = array_merge($widgets, $module_widgets);
}
backdrop_alter('insert_widgets', $widgets);
}
return $widgets;
}
/**
* Load a single insert field widget info.
*/
function insert_widget_load($widget_type) {
$widgets = insert_widgets();
return isset($widgets[$widget_type]) ? $widgets[$widget_type] : FALSE;
}
/**
* Given an item and an insert style, return the output.
*/
function insert_content($item, $style, $widget) {
return module_invoke($style['module'], 'insert_content', $item, $style, $widget);
}
/**
* Process function for insert-enabled fields.
*/
function insert_element_process($element) {
static $js_added;
// Bail out early if the needed properties aren't available. This happens
// most frequently when editing a field configuration.
if (!isset($element['#entity_type'])) {
return $element;
}
$item = $element['#value'];
$field = field_info_field($element['#field_name']);
$instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
$widget_settings = $instance['widget']['settings'];
$widget_type = $instance['widget']['type'];
// Bail out of Insert is not enabled on this field.
if (empty($widget_settings['insert'])) {
return $element;
}
// Add base settings only once.
if (!isset($js_added)) {
$js_added = array();
$settings = array('fileDirectoryPath' => file_default_scheme());
backdrop_add_js(array('insert' => $settings), 'setting');
backdrop_add_js(backdrop_get_path('module', 'insert') . '/insert.js');
}
// Add settings for this widget only once.
if (!isset($js_added[$widget_type])) {
$js_added[$widget_type] = TRUE;
$insert_widget = insert_widget_load($widget_type);
$insert_settings = array(
'maxWidth' => $widget_settings['insert_width'],
'wrapper' => $insert_widget['wrapper'],
'fields' => $insert_widget['fields'],
);
backdrop_add_js(array('insert' => array('widgets' => array($widget_type => $insert_settings))), 'setting');
}
// Load the file if it's not entirely loaded.
if ($element['fid']['#value'] && !isset($item['filename'])) {
if ($loaded_file = file_load($element['fid']['#value'])) {
$item = array_merge((array) $loaded_file, $item);
}
}
if (isset($item['filename'])) {
$insert_styles = !empty($widget_settings['insert_styles']['<all>']) ? backdrop_map_assoc(array_keys(insert_styles())) : array_filter((array) $widget_settings['insert_styles']);
$default = !empty($instance['widget']['settings']['insert_default']) ? $widget_settings['insert_default'] : 'auto';
if (!isset($insert_styles[$default])) {
$insert_styles[$default] = $default;
}
foreach ($insert_styles as $style_name => $enabled) {
if ($enabled && ($style = insert_style_load($style_name))) {
$element['insert_templates'][$style_name] = array(
'#type' => 'hidden',
'#value' => insert_content($item, $style, $instance['widget']),
'#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
'#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
'#attributes' => array('class' => array('insert-template')),
);
$style_options[$style_name] = $style['label'];
}
// Always provide a file name property.
$element['insert_filename'] = array(
'#type' => 'hidden',
'#value' => $item['filename'],
'#id' => $element['#id'] . '-insert-filename',
'#name' => $element['#name'] . '[insert_filename]',
'#attributes' => array('class' => array('insert-filename')),
);
}
$element['insert'] = array(
'#theme' => 'insert_widget',
'#type' => 'markup',
'#options' => $style_options,
'#widget' => $instance['widget'],
'#weight' => -3,
'#default_value' => $default,
);
}
return $element;
}
/**
* Implements hook_form_alter().
*/
function insert_form_field_ui_field_edit_form_alter(&$form, $form_state) {
$instance = $form['#instance'];
if (array_key_exists($instance['widget']['type'], insert_widgets())) {
$field = $form['#field'];
if (empty($form['instance']['settings'])) {
$form['instance']['settings'] = array();
}
$form['instance']['settings'] += insert_field_widget_settings_form($field, $instance);
}
}
/**
* Implements hook_field_widget_info_alter().
*
* A list of settings needed by Insert module on widgets.
*/
function insert_field_widget_info_alter(&$info) {
$settings = array(
'insert' => 0,
'insert_absolute' => config_get('insert.settings', 'absolute_paths'),
'insert_styles' => array('auto'),
'insert_default' => array('auto'),
'insert_class' => '',
'insert_width' => '',
);
foreach (insert_widgets() as $widget_type => $widget) {
if (isset($info[$widget_type]['settings'])) {
$info[$widget_type]['settings'] += $settings;
}
}
}
/**
* Configuration form for editing insert settings for a field instance.
*/
function insert_field_widget_settings_form($field, $instance) {
$widget = $instance['widget'];
$settings = $widget['settings'];
$form['insert'] = array(
'#type' => 'fieldset',
'#title' => t('Insert'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('These options allow the user to easily insert an HTML tags into text areas or WYSIWYG editors after uploading a file or image. The "Automatic" style will insert a <img> tag for images and a <a> tag for other files. Other styles may insert tags that may not match the file type.'),
'#weight' => 20,
'#parents' => array('instance', 'widget', 'settings'),
);
$form['insert']['insert'] = array(
'#type' => 'checkbox',
'#title' => t('Enable insert button'),
'#default_value' => $settings['insert'],
'#description' => t('Enable the insert button and options for this widget.'),
'#weight' => -10,
);
$form['insert']['insert_absolute'] = array(
'#type' => 'checkbox',
'#title' => t('Use absolute paths'),
'#default_value' => isset($settings['insert_absolute']) ? $settings['insert_absolute'] : config_get('insert.settings', 'absolute_paths'),
'#description' => t('Includes the full URL prefix "@base_url" in all links and image tags.', array('@base_url' => $GLOBALS['base_url'])),
'#weight' => -9,
);
$form['insert']['insert_styles'] = array(
'#title' => t('Enabled insert styles'),
'#type' => 'checkboxes',
'#options' => insert_styles_list(),
'#default_value' => !empty($settings['insert_styles']['<all>']) ? array_keys(insert_styles_list()) : $settings['insert_styles'],
'#description' => t('Select which styles should be available when sending items to text areas. If no styles are selected, the option to use a style is not displayed. If all styles are selected, new styles will be enabled by default.'),
'#element_validate' => array('insert_field_widget_settings_styles_validate'),
'#theme' => 'insert_field_widget_settings_styles',
'#weight' => 0,
);
$form['insert']['insert_default'] = array(
'#title' => t('Default insert style'),
'#type' => 'select',
'#options' => insert_styles_list(),
'#default_value' => $settings['insert_default'],
'#description' => t('Select the default style which will be selected by default or used if no specific styles above are enabled.'),
'#weight' => 1,
);
$form['insert']['insert_class'] = array(
'#title' => t('Additional CSS classes'),
'#type' => 'textfield',
'#default_value' => $settings['insert_class'],
'#description' => t('Add any classes that should be added to the item on output.'),
'#weight' => 5,
);
$form['insert']['insert_width'] = array(
'#title' => t('Maximum image insert width'),
'#type' => 'textfield',
'#size' => 10,
'#field_suffix' => ' '. t('pixels'),
'#default_value' => $settings['insert_width'],
'#description' => t('When inserting images, the height and width of images may be scaled down to fit within the specified width. Note that this does not resize the image, it only affects the HTML output. To resize images it is recommended to install the <a href="http://drupal.org/project/image_resize_filter">Image Resize Filter</a> module.'),
'#weight' => 10,
);
return $form;
}
/**
* An #element_validate function for the styles list on the settings form.
*/
function insert_field_widget_settings_styles_validate($element, &$form_state) {
if (array_values($element['#value']) == array_keys($element['#options'])) {
form_set_value($element, array('<all>' => '<all>'), $form_state);
}
}
/**
* Theme the output of the styles list on the settings form.
*/
function theme_insert_field_widget_settings_styles($variables) {
$element = $variables['element'];
backdrop_add_js('core/misc/tableselect.js');
$header = array(
array('class' => array('select-all'), 'data' => ' ' . t('Select all')),
);
$rows = array();
foreach ($element['#options'] as $key => $label) {
$row = array();
$row[] = backdrop_render($element[$key]);
$rows[] = $row;
}
return theme('table', array('header' => $header, 'rows' => $rows));
}
/**
* Utility function to create a URL for Insert.
*
* This is modeled after file_create_url(), but with the modification that it
* will consistently use absolute or relative URLs, depending on the Insert
* setting.
*/
function insert_create_url($uri, $absolute = NULL, $clean_urls = TRUE) {
$absolute = isset($absolute) ? $absolute : config_get('insert.settings', 'absolute_paths');
// If not using clean URLs, the image derivative callback is only available
// with the query string. Always use the non-clean URL in the event that the
// image cache is flushed and needs to be regenerated. See image_style_url().
if (!$clean_urls && file_uri_scheme($uri) == 'public') {
$directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
$url = url($directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE));
}
else {
$url = file_create_url($uri);
}
if (!$absolute && strpos($url, $GLOBALS['base_url']) === 0) {
$url = base_path() . ltrim(str_replace($GLOBALS['base_url'], '', $url), '/');
}
return $url;
}
/**
* Preprocess variables for the insert-widget.tpl.php file.
*/
function template_preprocess_insert_widget(&$vars) {
$element = $vars['element'];
$vars['insert_styles'] = $element['#options'];
$vars['default_style'] = $element['#default_value'];
$vars['widget_type'] = $element['#widget']['type'];
}