comparison sites/all/modules/insert/insert.module @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 <?php
2
3 /**
4 * @file
5 * Allows insertion of files, images, and other media directly into the body
6 * field by using an "Insert" button next to the uploaded file.
7 */
8
9 // Load default implementations of insert hooks for core modules.
10 require dirname(__FILE__) . '/includes/file.inc';
11 require dirname(__FILE__) . '/includes/image.inc';
12 require dirname(__FILE__) . '/includes/insert.inc';
13
14 /**
15 * Implements hook_element_info().
16 */
17 function insert_element_info() {
18 $extra = array('#after_build' => array('insert_element_process'));
19
20 $elements = array();
21 foreach (insert_widgets() as $widget_type => $widget) {
22 $element_type = isset($widget['element_type']) ? $widget['element_type'] : $widget_type;
23 $elements[$element_type] = $extra;
24 }
25
26 return $elements;
27 }
28
29 /**
30 * Implements hook_theme().
31 */
32 function insert_theme() {
33 return array(
34 'insert_widget' => array(
35 'render element' => 'element',
36 'template' => 'templates/insert-widget',
37 ),
38 'insert_field_widget_settings_styles' => array(
39 'render element' => 'element',
40 ),
41
42 // Theme functions in includes/insert.inc.
43 'insert_image' => array(
44 'variables' => array('item' => NULL, 'widget' => NULL),
45 'template' => 'templates/insert-image',
46 'file' => 'includes/insert.inc',
47 ),
48 'insert_link' => array(
49 'variables' => array('item' => NULL, 'widget' => NULL),
50 'template' => 'templates/insert-link',
51 'file' => 'includes/insert.inc',
52 ),
53 'insert_icon_link' => array(
54 'variables' => array('item' => NULL, 'widget' => NULL),
55 'template' => 'templates/insert-icon-link',
56 'file' => 'includes/insert.inc',
57 ),
58
59 // Theme functions in includes/image.inc.
60 'image_insert_image' => array(
61 'variables' => array('item' => NULL, 'widget' => NULL, 'style_name' => NULL),
62 'template' => 'templates/image-insert-image',
63 'pattern' => 'image_insert_image__[a-z0-9_]+',
64 'file' => 'includes/image.inc',
65 ),
66 );
67 }
68
69 /**
70 * Get a list of all supported image styles.
71 */
72 function insert_styles($reset = FALSE) {
73 static $styles;
74
75 if (!isset($styles) || $reset) {
76 $styles = array();
77 foreach (module_implements('insert_styles') as $module) {
78 $module_styles = module_invoke($module, 'insert_styles');
79 foreach ($module_styles as $name => $style) {
80 $module_styles[$name]['name'] = $name;
81 $module_styles[$name]['module'] = $module;
82 }
83 $styles = array_merge($styles, $module_styles);
84 }
85 drupal_alter('insert_styles', $styles);
86 uasort($styles, '_insert_style_sort');
87 }
88
89 return $styles;
90 }
91
92 /**
93 * Sort the styles.
94 */
95 function _insert_style_sort($a, $b) {
96 $a = (array)$a + array('weight' => 0, 'label' => '');
97 $b = (array)$b + array('weight' => 0, 'label' => '');
98 return $a['weight'] < $b['weight'] ? -1 : ($a['weight'] > $b['weight'] ? 1 : strnatcasecmp($a['label'], $b['label']));
99 }
100
101 /**
102 * Load an individual insert style.
103 */
104 function insert_style_load($style_name) {
105 $styles = insert_styles();
106 return isset($styles[$style_name]) ? $styles[$style_name] : FALSE;
107 }
108
109 /**
110 * Get a list of styles suitable for an #options array.
111 */
112 function insert_styles_list() {
113 $list = array();
114 foreach (insert_styles() as $name => $style) {
115 $list[$name] = $style['label'];
116 }
117 return $list;
118 }
119
120 /**
121 * Get a list of all supported field widgets.
122 */
123 function insert_widgets($reset = FALSE) {
124 static $widgets;
125
126 if (!isset($widgets) || $reset) {
127 $widgets = array();
128 foreach (module_implements('insert_widgets') as $module) {
129 $module_widgets = module_invoke($module, 'insert_widgets');
130 foreach ($module_widgets as $type => $widget) {
131 $module_widgets[$type]['type'] = $type;
132 $module_widgets[$type]['module'] = $module;
133 }
134 $widgets = array_merge($widgets, $module_widgets);
135 }
136 drupal_alter('insert_widgets', $widgets);
137 }
138
139 return $widgets;
140 }
141
142 /**
143 * Load a single insert field widget info.
144 */
145 function insert_widget_load($widget_type) {
146 $widgets = insert_widgets();
147 return isset($widgets[$widget_type]) ? $widgets[$widget_type] : FALSE;
148 }
149
150 /**
151 * Given an item and an insert style, return the output.
152 */
153 function insert_content($item, $style, $widget) {
154 return module_invoke($style['module'], 'insert_content', $item, $style, $widget);
155 }
156
157 /**
158 * Process function for insert-enabled fields.
159 */
160 function insert_element_process($element) {
161 static $js_added;
162
163 // Bail out early if the needed properties aren't available. This happens
164 // most frequently when editing a field configuration.
165 if (!isset($element['#entity_type'])) {
166 return $element;
167 }
168
169 $item = $element['#value'];
170 $field = field_info_field($element['#field_name']);
171 $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
172
173 $widget_settings = $instance['widget']['settings'];
174 $widget_type = $instance['widget']['type'];
175
176 // Bail out of Insert is not enabled on this field.
177 if (empty($widget_settings['insert'])) {
178 return $element;
179 }
180
181 // Add base settings only once.
182 if (!isset($js_added)) {
183 $js_added = array();
184 $settings = array('fileDirectoryPath' => file_default_scheme());
185 drupal_add_js(array('insert' => $settings), 'setting');
186 drupal_add_js(drupal_get_path('module', 'insert') . '/insert.js');
187 }
188
189 // Add settings for this widget only once.
190 if (!isset($js_added[$widget_type])) {
191 $js_added[$widget_type] = TRUE;
192 $insert_widget = insert_widget_load($widget_type);
193 $insert_settings = array(
194 'maxWidth' => $widget_settings['insert_width'],
195 'wrapper' => $insert_widget['wrapper'],
196 'fields' => $insert_widget['fields'],
197 );
198 drupal_add_js(array('insert' => array('widgets' => array($widget_type => $insert_settings))), 'setting');
199 }
200
201 // Load the file if it's not entirely loaded.
202 if ($element['fid']['#value'] && !isset($item['filename'])) {
203 if ($loaded_file = file_load($element['fid']['#value'])) {
204 $item = array_merge((array) $loaded_file, $item);
205 }
206 }
207
208 if (isset($item['filename'])) {
209 $insert_styles = !empty($widget_settings['insert_styles']['<all>']) ? drupal_map_assoc(array_keys(insert_styles())) : array_filter((array) $widget_settings['insert_styles']);
210 $default = !empty($instance['widget']['settings']['insert_default']) ? $widget_settings['insert_default'] : 'auto';
211 if (!isset($insert_styles[$default])) {
212 $insert_styles[$default] = $default;
213 }
214 foreach ($insert_styles as $style_name => $enabled) {
215 if ($enabled && ($style = insert_style_load($style_name))) {
216 $element['insert_templates'][$style_name] = array(
217 '#type' => 'hidden',
218 '#value' => insert_content($item, $style, $instance['widget']),
219 '#id' => $element['#id'] . '-insert-template-' . str_replace('_', '-', $style_name),
220 '#name' => $element['#name'] . '[insert_template][' . $style_name . ']',
221 '#attributes' => array('class' => array('insert-template')),
222 );
223 $style_options[$style_name] = $style['label'];
224 }
225
226 // Always provide a file name property.
227 $element['insert_filename'] = array(
228 '#type' => 'hidden',
229 '#value' => $item['filename'],
230 '#id' => $element['#id'] . '-insert-filename',
231 '#name' => $element['#name'] . '[insert_filename]',
232 '#attributes' => array('class' => array('insert-filename')),
233 );
234 }
235
236 $element['insert'] = array(
237 '#theme' => 'insert_widget',
238 '#type' => 'markup',
239 '#options' => $style_options,
240 '#widget' => $instance['widget'],
241 '#weight' => -3,
242 '#default_value' => $default,
243 );
244 }
245
246 return $element;
247 }
248
249 /**
250 * Implements hook_form_alter().
251 */
252 function insert_form_field_ui_field_edit_form_alter(&$form, $form_state) {
253 $instance = $form['#instance'];
254 if (array_key_exists($instance['widget']['type'], insert_widgets())) {
255 $field = $form['#field'];
256 if (empty($form['instance']['settings'])) {
257 $form['instance']['settings'] = array();
258 }
259 $form['instance']['settings'] += insert_field_widget_settings_form($field, $instance);
260 }
261 }
262
263 /**
264 * Implements hook_field_widget_info_alter().
265 *
266 * A list of settings needed by Insert module on widgets.
267 */
268 function insert_field_widget_info_alter(&$info) {
269 $settings = array(
270 'insert' => 0,
271 'insert_absolute' => variable_get('insert_absolute_paths', FALSE),
272 'insert_styles' => array('auto'),
273 'insert_default' => array('auto'),
274 'insert_class' => '',
275 'insert_width' => '',
276 );
277
278 foreach (insert_widgets() as $widget_type => $widget) {
279 if (isset($info[$widget_type]['settings'])) {
280 $info[$widget_type]['settings'] += $settings;
281 }
282 }
283 }
284
285 /**
286 * Configuration form for editing insert settings for a field instance.
287 */
288 function insert_field_widget_settings_form($field, $instance) {
289 $widget = $instance['widget'];
290 $settings = $widget['settings'];
291
292 $form['insert'] = array(
293 '#type' => 'fieldset',
294 '#title' => t('Insert'),
295 '#collapsible' => TRUE,
296 '#collapsed' => TRUE,
297 '#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 &lt;img&gt; tag for images and a &lt;a&gt; tag for other files. Other styles may insert tags that may not match the file type.'),
298 '#weight' => 20,
299 '#parents' => array('instance', 'widget', 'settings'),
300 );
301
302 $form['insert']['insert'] = array(
303 '#type' => 'checkbox',
304 '#title' => t('Enable insert button'),
305 '#default_value' => $settings['insert'],
306 '#description' => t('Enable the insert button and options for this widget.'),
307 '#weight' => -10,
308 );
309
310 $form['insert']['insert_absolute'] = array(
311 '#type' => 'checkbox',
312 '#title' => t('Use absolute paths'),
313 '#default_value' => isset($settings['insert_absolute']) ? $settings['insert_absolute'] : variable_get('insert_absolute_paths', FALSE),
314 '#description' => t('Includes the full URL prefix "@base_url" in all links and image tags.', array('@base_url' => $GLOBALS['base_url'])),
315 '#weight' => -9,
316 );
317
318 $form['insert']['insert_styles'] = array(
319 '#title' => t('Enabled insert styles'),
320 '#type' => 'checkboxes',
321 '#options' => insert_styles_list(),
322 '#default_value' => !empty($settings['insert_styles']['<all>']) ? array_keys(insert_styles_list()) : $settings['insert_styles'],
323 '#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.'),
324 '#element_validate' => array('insert_field_widget_settings_styles_validate'),
325 '#theme' => 'insert_field_widget_settings_styles',
326 '#weight' => 0,
327 );
328
329 $form['insert']['insert_default'] = array(
330 '#title' => t('Default insert style'),
331 '#type' => 'select',
332 '#options' => insert_styles_list(),
333 '#default_value' => $settings['insert_default'],
334 '#description' => t('Select the default style which will be selected by default or used if no specific styles above are enabled.'),
335 '#weight' => 1,
336 );
337
338 $form['insert']['insert_class'] = array(
339 '#title' => t('Additional CSS classes'),
340 '#type' => 'textfield',
341 '#default_value' => $settings['insert_class'],
342 '#description' => t('Add any classes that should be added to the item on output.'),
343 '#weight' => 5,
344 );
345
346 $form['insert']['insert_width'] = array(
347 '#title' => t('Maximum image insert width'),
348 '#type' => 'textfield',
349 '#size' => 10,
350 '#field_suffix' => ' '. t('pixels'),
351 '#default_value' => $settings['insert_width'],
352 '#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.'),
353 '#weight' => 10,
354 );
355
356 return $form;
357 }
358
359 /**
360 * An #element_validate function for the styles list on the settings form.
361 */
362 function insert_field_widget_settings_styles_validate($element, &$form_state) {
363 if (array_values($element['#value']) == array_keys($element['#options'])) {
364 form_set_value($element, array('<all>' => '<all>'), $form_state);
365 }
366 }
367
368 /**
369 * Theme the output of the styles list on the settings form.
370 */
371 function theme_insert_field_widget_settings_styles($variables) {
372 $element = $variables['element'];
373 drupal_add_js('misc/tableselect.js');
374
375 $header = array(
376 array('class' => array('select-all'), 'data' => ' ' . t('Select all')),
377 );
378
379 $rows = array();
380 foreach ($element['#options'] as $key => $label) {
381 $row = array();
382 $row[] = drupal_render($element[$key]);
383 $rows[] = $row;
384 }
385 return theme('table', array('header' => $header, 'rows' => $rows));
386 }
387
388 /**
389 * Utility function to create a URL for Insert.
390 *
391 * This is modeled after file_create_url(), but with the modification that it
392 * will consistently use absolute or relative URLs, depending on the Insert
393 * setting.
394 */
395 function insert_create_url($uri, $absolute = NULL, $clean_urls = TRUE) {
396 $absolute = isset($absolute) ? $absolute : variable_get('insert_absolute_paths', FALSE);
397
398 // If not using clean URLs, the image derivative callback is only available
399 // with the query string. Always use the non-clean URL in the event that the
400 // image cache is flushed and needs to be regenerated. See image_style_url().
401 if (!$clean_urls && file_uri_scheme($uri) == 'public') {
402 $directory_path = file_stream_wrapper_get_instance_by_uri($uri)->getDirectoryPath();
403 $url = url($directory_path . '/' . file_uri_target($uri), array('absolute' => TRUE));
404 }
405 else {
406 $url = file_create_url($uri);
407 }
408
409 if (!$absolute && strpos($url, $GLOBALS['base_url']) === 0) {
410 $url = base_path() . ltrim(str_replace($GLOBALS['base_url'], '', $url), '/');
411 }
412 return $url;
413 }
414
415 /**
416 * Preprocess variables for the insert-widget.tpl.php file.
417 */
418 function template_preprocess_insert_widget(&$vars) {
419 $element = $vars['element'];
420
421 $vars['insert_styles'] = $element['#options'];
422 $vars['default_style'] = $element['#default_value'];
423 $vars['widget_type'] = $element['#widget']['type'];
424 }