ckeditor_link.module

  1. cis7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  2. cle7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  3. ecd7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  4. elmsmedia7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  5. harmony7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  6. icor7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  7. meedjum_blog7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
  8. mooc7 sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module

Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr> http://www.absyx.fr

Functions

File

sites/all/modules/ulmus/ckeditor_link/ckeditor_link.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
  5. * http://www.absyx.fr
  6. */
  7. /**
  8. * Implementation of hook_permission().
  9. */
  10. function ckeditor_link_permission() {
  11. return array(
  12. 'access ckeditor link' => array('title' => t('Access <em>CKEditor Link</em>')),
  13. 'administer ckeditor link' => array('title' => t('Administer <em>CKEditor Link</em>')),
  14. );
  15. }
  16. /**
  17. * Implementation of hook_menu().
  18. */
  19. function ckeditor_link_menu() {
  20. $items['ckeditor_link/autocomplete'] = array(
  21. 'page callback' => 'ckeditor_link_autocomplete',
  22. 'access arguments' => array('access ckeditor link'),
  23. 'type' => MENU_CALLBACK,
  24. );
  25. $items['ckeditor_link/revert'] = array(
  26. 'page callback' => 'ckeditor_link_revert',
  27. 'access arguments' => array('access ckeditor link'),
  28. 'type' => MENU_CALLBACK,
  29. );
  30. $items['admin/config/content/ckeditor_link'] = array(
  31. 'title' => 'CKEditor Link',
  32. 'description' => 'Configure CKEditor Link.',
  33. 'page callback' => 'drupal_get_form',
  34. 'page arguments' => array('ckeditor_link_settings_form'),
  35. 'access arguments' => array('administer ckeditor link'),
  36. 'file' => 'ckeditor_link.admin.inc',
  37. );
  38. return $items;
  39. }
  40. function ckeditor_link_autocomplete($string = '') {
  41. $matches = array();
  42. if ($string !== '') {
  43. $types = ckeditor_link_get_types();
  44. $limit = variable_get('ckeditor_link_limit', 10);
  45. $results = array();
  46. foreach ($types as $type) {
  47. $func = $type['module'] .'_ckeditor_link_'. $type['type'] .'_autocomplete';
  48. if (function_exists($func)) {
  49. $results += $func($string, $limit);
  50. if (count($results) > $limit) {
  51. break;
  52. }
  53. }
  54. }
  55. drupal_alter('ckeditor_link_autocomplete', $results, $string);
  56. array_splice($results, $limit);
  57. foreach ($results as $path => $title) {
  58. $matches[$title .' ('. $path .')'] = '<div class="reference-autocomplete">'. check_plain($title) .'</div>';
  59. }
  60. }
  61. drupal_json_output($matches);
  62. }
  63. function ckeditor_link_revert() {
  64. $output = NULL;
  65. $args = func_get_args();
  66. $path = trim(implode('/', $args), '/');
  67. if ($path !== '') {
  68. $langcode = LANGUAGE_NONE;
  69. $path = ckeditor_link_path_strip_language($path, $langcode);
  70. $path = drupal_get_normal_path($path, $langcode);
  71. $types = ckeditor_link_get_types();
  72. foreach ($types as $type) {
  73. $func = $type['module'] .'_ckeditor_link_'. $type['type'] .'_revert';
  74. if (function_exists($func)) {
  75. $result = $func($path, $langcode);
  76. if ($result !== NULL) {
  77. $output = ($result !== FALSE) ? $result .' ('. ckeditor_link_path_prefix_language($path, $langcode) .')' : FALSE;
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. drupal_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  84. echo drupal_json_encode($output);
  85. }
  86. function ckeditor_link_get_types() {
  87. static $types;
  88. if (!isset($types)) {
  89. $types = array();
  90. $data = array();
  91. foreach (module_implements('ckeditor_link_types') as $module) {
  92. $func = $module .'_ckeditor_link_types';
  93. $data[$module] = $func();
  94. }
  95. foreach ($data as $module => $_types) {
  96. foreach ($_types as $type) {
  97. if (!is_array($type)) {
  98. $type = array('type' => $type);
  99. }
  100. $type['module'] = $module;
  101. $types[$module .'.'. $type['type']] = $type;
  102. }
  103. }
  104. drupal_alter('ckeditor_link_types', $types);
  105. $types = array_values($types);
  106. foreach ($types as $type) {
  107. if (isset($type['file'])) {
  108. require_once(drupal_get_path('module', $type['module']) .'/'. $type['file']);
  109. }
  110. }
  111. }
  112. return $types;
  113. }
  114. /**
  115. * Implementation of hook_ckeditor_link_types().
  116. */
  117. function ckeditor_link_ckeditor_link_types() {
  118. $types[] = array('type' => 'node', 'file' => 'includes/ckeditor_link.node.inc');
  119. if (module_exists('taxonomy')) {
  120. $types[] = array('type' => 'taxonomy', 'file' => 'includes/ckeditor_link.taxonomy.inc');
  121. if (module_exists('i18n_taxonomy')) {
  122. $types[] = array('type' => 'i18n_taxonomy', 'file' => 'includes/ckeditor_link.i18n_taxonomy.inc');
  123. }
  124. }
  125. $types[] = array('type' => 'menu', 'file' => 'includes/ckeditor_link.menu.inc');
  126. if (module_exists('i18n_menu')) {
  127. $types[] = array('type' => 'i18n_menu', 'file' => 'includes/ckeditor_link.i18n_menu.inc');
  128. }
  129. return $types;
  130. }
  131. /**
  132. * Implementation of hook_element_info_alter().
  133. */
  134. function ckeditor_link_element_info_alter(&$type) {
  135. if (user_access('access ckeditor link')) {
  136. $type['text_format']['#pre_render'][] = 'ckeditor_link_text_format_pre_render';
  137. $type['form']['#post_render'][] = 'ckeditor_link_form_post_render';
  138. }
  139. }
  140. function ckeditor_link_text_format_pre_render($element) {
  141. _ckeditor_link_has_text_format(TRUE);
  142. return $element;
  143. }
  144. function _ckeditor_link_has_text_format($set = FALSE) {
  145. static $has = FALSE;
  146. if (!$set) {
  147. return $has;
  148. }
  149. $has = TRUE;
  150. }
  151. function ckeditor_link_form_post_render($content, $element) {
  152. static $added;
  153. if (!isset($added) && _ckeditor_link_has_text_format() && ($js = drupal_add_js()) && isset($js['settings']['data'])) {
  154. $settings = call_user_func_array('array_merge_recursive', $js['settings']['data']);
  155. if (isset($settings['ckeditor']) || isset($settings['wysiwyg']['configs']['ckeditor'])) {
  156. $added = TRUE;
  157. drupal_add_css(drupal_get_path('module', 'ckeditor_link') .'/ckeditor_link.css');
  158. drupal_add_js('misc/autocomplete.js');
  159. drupal_add_js(array('ckeditor_link' => array(
  160. 'module_path' => base_path() . drupal_get_path('module', 'ckeditor_link'),
  161. 'autocomplete_path' => url('ckeditor_link/autocomplete'),
  162. 'revert_path' => url('ckeditor_link/revert'),
  163. 'msg_invalid_path' => t('Link must be a valid internal path.'),
  164. 'type_name' => ckeditor_link_get_link_type_name(),
  165. 'type_selected' => (bool) variable_get('ckeditor_link_type_selected', 1),
  166. )), 'setting');
  167. }
  168. }
  169. return $content;
  170. }
  171. /**
  172. * Implementation of hook_ckeditor_plugin().
  173. */
  174. function ckeditor_link_ckeditor_plugin() {
  175. return array('ckeditor_link' => array(
  176. 'name' => 'drupal_path',
  177. 'desc' => t('CKEditor Link - A plugin to easily create links to Drupal internal paths'),
  178. 'path' => drupal_get_path('module', 'ckeditor_link') .'/plugins/link/',
  179. ));
  180. }
  181. /**
  182. * Implementation of hook_wysiwyg_plugin().
  183. */
  184. function ckeditor_link_wysiwyg_plugin($editor, $version) {
  185. if ($editor == 'ckeditor') {
  186. return array('drupal_path' => array(
  187. 'path' => drupal_get_path('module', 'ckeditor_link') .'/plugins/link/',
  188. 'load' => TRUE,
  189. 'extensions' => array('Link' => t('CKEditor Link')),
  190. ));
  191. }
  192. }
  193. /**
  194. * Implementation of hook_filter_info().
  195. */
  196. function ckeditor_link_filter_info() {
  197. $filters['ckeditor_link_filter'] = array(
  198. 'title' => t('CKEditor Link Filter'),
  199. 'description' => t('Converts links added through <em>CKEditor Link</em> into aliased and language prefixed URLs.'),
  200. 'process callback' => 'ckeditor_link_filter_process',
  201. );
  202. return $filters;
  203. }
  204. function ckeditor_link_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  205. _ckeditor_link_filter_process(NULL, $langcode);
  206. return preg_replace_callback('`\bhref="'. preg_quote(base_path(), '`') .'([^?#"]+)`', '_ckeditor_link_filter_process', $text);
  207. }
  208. function _ckeditor_link_filter_process($matches, $langcode = NULL) {
  209. static $stored_langcode = LANGUAGE_NONE;
  210. if ($matches === NULL) {
  211. $stored_langcode = $langcode;
  212. return;
  213. }
  214. $path = urldecode($matches[1]);
  215. $langcode = $stored_langcode;
  216. $path = ckeditor_link_path_strip_language($path, $langcode);
  217. $types = ckeditor_link_get_types();
  218. foreach ($types as $type) {
  219. $func = $type['module'] .'_ckeditor_link_'. $type['type'] .'_url';
  220. if (function_exists($func)) {
  221. $url = $func($path, $langcode);
  222. if ($url) {
  223. return 'href="'. $url;
  224. }
  225. }
  226. }
  227. return 'href="'. base_path() . $matches[1];
  228. }
  229. function ckeditor_link_get_link_type_name() {
  230. return t(variable_get('ckeditor_link_type_name', 'Internal path'), array('!site_name' => variable_get('site_name', 'Drupal')));
  231. }
  232. function ckeditor_link_path_strip_language($path, &$langcode) {
  233. $languages = ckeditor_link_get_languages();
  234. if ($languages) {
  235. $args = explode('/', $path);
  236. $prefix = array_shift($args);
  237. foreach ($languages as $language) {
  238. if (!empty($language->prefix) && ($language->prefix == $prefix)) {
  239. $langcode = $language->language;
  240. $path = implode('/', $args);
  241. break;
  242. }
  243. }
  244. }
  245. return $path;
  246. }
  247. function ckeditor_link_path_prefix_language($path, $langcode) {
  248. if ($langcode != LANGUAGE_NONE) {
  249. $languages = ckeditor_link_get_languages();
  250. if ($languages && isset($languages[$langcode])) {
  251. $language = $languages[$langcode];
  252. if (!empty($language->prefix)) {
  253. $path = (empty($path)) ? $language->prefix : $language->prefix .'/'. $path;
  254. }
  255. }
  256. }
  257. return $path;
  258. }
  259. function ckeditor_link_get_languages() {
  260. static $languages;
  261. if (!isset($languages)) {
  262. $languages = FALSE;
  263. if (drupal_multilingual() && language_negotiation_get_any(LOCALE_LANGUAGE_NEGOTIATION_URL)) {
  264. $languages = language_list('enabled');
  265. $languages = $languages[1];
  266. }
  267. }
  268. return $languages;
  269. }
  270. function ckeditor_link_url($path = NULL, $langcode) {
  271. $options = array();
  272. if ($langcode != LANGUAGE_NONE) {
  273. $languages = ckeditor_link_get_languages();
  274. if ($languages && isset($languages[$langcode])) {
  275. $options['language'] = $languages[$langcode];
  276. }
  277. }
  278. if (!isset($options['language'])) {
  279. $options['language'] = language_default();
  280. }
  281. return url($path, $options);
  282. }
  283. function _ckeditor_link_check_path($path) {
  284. return preg_match('`^[a-z][\w\/\.-]*$`i', $path);
  285. }