Commit 1a1b4958 authored by Henri MEDOT's avatar Henri MEDOT
Browse files

First official version.

parent 3e16ce17
// $Id$
CKEDITOR LINK - A PLUGIN TO EASILY CREATE LINKS TO DRUPAL INTERNAL PATHS
http://drupal.org/project/ckeditor_link
INSTALLATION
Please read instructions at: http://drupal.org/project/ckeditor_link
CONTACT
Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
http://www.absyx.fr
; $Id$
name = CKEditor Link
description = CKEditor Link.
description = Easily create links to Drupal internal paths through CKEditor.
core = 6.x
\ No newline at end of file
......@@ -7,6 +7,16 @@
* http://www.absyx.fr
*/
/**
* Implementation of hook_init().
*/
function ckeditor_link_init() {
if (!variable_get('clean_url', 0)) {
module_disable(array('ckeditor_link'));
drupal_set_message(t('<em>CKEditor Link</em> has been disabled. <a href="!url">Clean URLs</a> need to be enabled for this module to work properly.', array('!url' => url('admin/settings/clean-urls'))), 'warning');
}
}
/**
* Implementation of hook_perm().
*/
......@@ -30,10 +40,10 @@ function ckeditor_link_autocomplete($string = '') {
$matches = array();
if ($string !== '') {
$sql = db_rewrite_sql("SELECT n.nid, n.title AS node_title, n.type AS node_type FROM {node} n WHERE n.title LIKE '%%%s%%' ORDER BY n.title, n.type");
$sql = db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.title LIKE '%%%s%%' ORDER BY n.title, n.type");
$result = db_query_range($sql, array($string), 0, 10);
while ($node = db_fetch_object($result)) {
$matches[$node->node_title .' ('. base_path() .'node/'. $node->nid. ')'] = '<div class="reference-autocomplete">'. check_plain($node->node_title) .'</div>';
$matches[$node->title .' (node/'. $node->nid. ')'] = '<div class="reference-autocomplete">'. check_plain($node->title) .'</div>';
}
}
......@@ -54,9 +64,11 @@ function ckeditor_link_process_form(&$form, &$form_state) {
if (!$added && ($js = drupal_add_js()) && isset($js['setting'])) {
$setting = call_user_func_array('array_merge_recursive', $js['setting']);
if (isset($setting['ckeditor']) || isset($setting['wysiwyg']['configs']['ckeditor'])) {
drupal_add_js('misc/autocomplete.js');
drupal_add_js(array('ckeditor_link' => array(
'module_path' => base_path().drupal_get_path('module', 'ckeditor_link'),
'autocomplete_path' => url('ckeditor_link/autocomplete'),
'msg_invalid_path' => t('Link must be a valid Drupal path.'),
)), 'setting');
$added = TRUE;
}
......@@ -69,10 +81,50 @@ function ckeditor_link_process_form(&$form, &$form_state) {
*/
function ckeditor_link_wysiwyg_plugin($editor, $version) {
if (($editor == 'ckeditor') && user_access('access ckeditor link')) {
return array('drupal_link' => array(
return array('drupal_path' => array(
'path' => drupal_get_path('module', 'ckeditor_link').'/plugins/link/',
'load' => TRUE,
'extensions' => array('Link' => t('CKEditor Link')),
));
}
}
/**
* Implementation of hook_filter().
*/
function ckeditor_link_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
case 'list':
return array(0 => t('CKEditor Link Filter'));
case 'description':
return t('Converts links added through <em>CKEditor Link</em> into aliased and language prefixed URLs.');
case 'no cache':
return FALSE;
case 'prepare':
return $text;
case 'process':
return preg_replace_callback('`\bhref="'. preg_quote(base_path(), '`') .'node/(\d+)(?=[?#"])`', '_ckeditor_link_filter_process', $text);
default:
return $text;
}
}
function _ckeditor_link_filter_process($matches) {
$nid = $matches[1];
$options = array();
if ($lang = db_result(db_query('SELECT language FROM {node} WHERE nid = %d', $nid))) {
$languages = language_list('enabled');
$languages = $languages[1];
if (isset($languages[$lang])) {
$options['language'] = $languages[$lang];
}
}
return 'href="'. url("node/$nid", $options);
}
......@@ -29,26 +29,32 @@
new Drupal.jsAC(input, new Drupal.ACDB(uri));
};
CKEDITOR.plugins.add('drupal_link', {
var extractPath = function(value) {
value = CKEDITOR.tools.trim(value);
var match;
match = /\((.*?)\)$/i.exec(value);
if (match && match[1]) {
value = match[1];
}
var basePath = Drupal.settings.basePath;
if (value.indexOf(basePath) == 0) {
value = value.substr(basePath.length);
}
if (/^node\/\d+$/.test(value)) {
return value;
}
return false;
};
CKEDITOR.plugins.add('drupal_path', {
init: function(editor, pluginPath) {
CKEDITOR.on('dialogDefinition', function(e) {
if ((e.editor != editor) || (e.data.name != 'link')) return;
// Overrides definition.
// Overrides linkType definition.
var definition = e.data.definition;
var infoTab = definition.getContents('info');
/*
definition.onShow = CKEDITOR.tools.override(definition.onShow, function(original) {
return function() {
original.call(this);
};
});
definition.onOk = function() {
};
*/
// Overrides linkType definition.
var content = getById(infoTab.elements, 'linkType');
content.items.unshift(['Drupal', 'drupal']);
content['default'] = 'drupal';
......@@ -57,21 +63,32 @@
id: 'drupalOptions',
children: [{
type: 'text',
id: 'drupal_link',
id: 'drupal_path',
label: editor.lang.link.title,
required: true,
onLoad: function() {
this.getInputElement().addClass('form-autocomplete');
initAutocomplete(this.getInputElement().$, Drupal.settings.ckeditor_link.autocomplete_path);
},
setup: function(data) {
this.setValue(data.drupal_path || '');
},
validate: function() {
var dialog = this.getDialog();
if (dialog.getValueOf('info', 'linkType') != 'drupal') {
return true;
}
var func = CKEDITOR.dialog.validate.notEmpty(editor.lang.link.noUrl);
return func.apply(this);
}
if (!func.apply(this)) {
return false;
}
if (!extractPath(this.getValue())) {
alert(Drupal.settings.ckeditor_link.msg_invalid_path);
this.focus();
return false;
}
return true;
},
}]
});
content.onChange = CKEDITOR.tools.override(content.onChange, function(original) {
......@@ -98,6 +115,14 @@
if (!data.type || (data.type == 'url') && !data.url) {
data.type = 'drupal';
}
else if (data.url && !data.url.protocol && data.url.url) {
var path = extractPath(data.url.url);
if (path) {
data.type = 'drupal';
data.drupal_path = path;
delete data.url;
}
}
this.setValue(data.type);
};
content.commit = function(data) {
......@@ -105,9 +130,8 @@
if (data.type == 'drupal') {
data.type = 'url';
var dialog = this.getDialog();
dialog.getContentElement('info', 'protocol').setValue('');
var match = /\((.*?)\)\s*$/i.exec(dialog.getContentElement('info', 'drupal_link').getValue());
dialog.getContentElement('info', 'url').setValue(match && match[1]);
dialog.setValueOf('info', 'protocol', '');
dialog.setValueOf('info', 'url', Drupal.settings.basePath + extractPath(dialog.getValueOf('info', 'drupal_path')));
}
};
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment