insert.module 14 KB

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