Been searching around for a way to set default link target in the CKeditor link dialog box, but with no luck. Today the default setting is "not set", but the client want's it to be _blank.

It seems FCKeditor had a setting for this, but can't find anything similar for CKeditor. Have tried this based on the setting for FCK, but with no result:

  function mymodule_wysiwyg_editor_settings_alter(&$settings, $context) {
  if($context['profile']->editor == 'ckeditor') {
      $settings['DefaultLinkTarget'] = '_blank';
  }
}

Not possible?

CommentFileSizeAuthor
#2 ck-gone.png139.14 KBvegardjo
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TwoD’s picture

Status: Closed (fixed) » Fixed

I looked through http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html and the source of CKEditor's Link plugin,but can't find any option for that either.

However, I did find out how to emulate the behavior from FCKeditor. You'll need one custom Drupal module, and one custom CKEditor plugin. After that it'll be a piece of cake to reuse this on any site.

sites/all/modules/cdt/cdt.info:

name = CKEditor default target
description = Changes CKEditor's default link target and emulates FCKeditor's DefaultLinkTarget setting.
package = Custom
core = 7.x

sites/all/modules/cdt/cdt.module:

/**
 * @file
 * Drupal/Wysiwyg integration for the CKEditor default_target plugin.
 */

/**
 * Implements hook_wysiwyg_plugin().
 *
 * Informs CKEditor about our custom plugin in
 * sites/all/libraries/ckeditor_plugins/default_target/plugin.js;
 */
function cdt_wysiwyg_plugin($editor, $version) {
  if ($editor == 'ckeditor') {
    return array(
      'default_target' => array(
        'path' => wysiwyg_get_path('ckeditor_plugins') . '/default_target',
        'filename' => 'plugin.js',
        'extensions' => array('default_target' => t('Default link target')),
//      We could override our custom settings without touching the plugin 'library' here or via hook_wysiwyg_editor_settings_alter().
//         'options' => array(
//          'DefaultLinkTarget' => '_top',
//        ),
        'load' => TRUE,
        'internal' => FALSE,
        
      ),
    );
  }
}

sites/all/libraries/ckeditor_plugins/default_target/plugin.js:

/**
 * @file
 * A small plugin to emulate FCKeditor's DefaultLinkTarget setting.
 * 
 * @see http://docs.cksource.com/CKEditor_3.x/Tutorials/Timestamp_Plugin
 * @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Dialog_Customization
 * @see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.pluginDefinition.html
 */

/**
 * Register a placeholder plugin so CKEditor won't complain.
 */
CKEDITOR.plugins.add('default_target', {
  requires: ['link'],
  init: function(editor) {
    // The plugin itself doesn't actually do anything, but having it is a
    // convenient way to load the file. The only downside is that our event
    // handler will be registered even if the requirements aren't fulfilled,
    // but it won't cause problems. We could of course do all kinds of
    // things here of we wanted.
  }
});

/**
 * Hook into dialog creation to set our override.
 */
CKEDITOR.on('dialogDefinition', function(ev) {
  var dialogName = ev.data.name, dialogDefinition = ev.data.definition;
  if (dialogName == 'link') {
    // Simulate FCKConfig.DefaultLinkTarget.
    var targetTab = dialogDefinition.getContents('target');
    var targetField = targetTab.get('linkTargetType');
    // Fetch the actual value from the emulated FCKeditor setting, default value set at bottom of file.
    targetField['default'] = ev.editor.config.DefaultLinkTarget;
    var oldSetup = targetField.setup;
    targetField.setup = function(data) {
      targetField.onChange.call(this);
      if (oldSetup) {
        oldSetup.call(this, data);
      }
    }

    // New default value for the CSS Class field
    var secondTargetTab = dialogDefinition.getContents('advanced');
    var secondTargetField = secondTargetTab.get('advCSSClasses');
    secondTargetField['default'] = ev.editor.config.DefaultLinkCSSClass;
  }
});

// Sets the default config value to _blank.
// Individual instances can override it as with any other setting.
CKEDITOR.config.DefaultLinkTarget = '_blank';

// Sets the default config value to 'Colorbox'.
// Individual instances can override it as with any other setting.
CKEDITOR.config.DefaultLinkCSSClass = 'Colorbox';

Enable the module and check the "Default link target" box under "Buttons and Plugins" and you're all set! :)

References:

vegardjo’s picture

Status: Active » Fixed
FileSize
139.14 KB

Wow - best support answer ever, thanks a bundle! :)

Sadly I didn't quite get it to work. I enable the module, add the CK plugin and activate the plugin (and cleared cache), but the result of this is that the WYSIWYG won't load at all - the textfield is simply gone. If I deactivate the plugin again it's back. This on Drupal 7.8, admin theme Seven, WYSIWYG 2.1, CKEditor 3.6.1.7072

See screenshot. You will also see the visibility: hidden style there, if I remove this I see the actual textfield, but the WYSIWYG is still not loaded in it..

TwoD’s picture

Status: Fixed » Postponed (maintainer needs more info)

The original textarea is hidden by the editor as it uses an iframe for editing, but it looks like it did not load completely.

Can you check Firebug's Console tab for errors?
Also check the Net tab to make sure all files got downloaded properly (none are red).
The plugin file from sites/all/libraries/ckeditor_plugins/default_target/plugin.js should also show up in there.

vegardjo’s picture

Status: Postponed (maintainer needs more info) » Fixed

Aah, my bad!

I placed the file in sites/all/libraries/ckeditor/plugins/default_target/plugin.js while it should be in sites/all/libraries/ckeditor_plugins/default_target/plugin.js so I placed it in the plugin directory to ckeditor, you in a separate plugin directory. Which also is smarter :)

Thanks again for your help, it works like a charm now!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Mirroar’s picture

At least for the current version of ckEditor, you'll have to update the JavaScript a little. Otherwise, _blank will be selected in the link dialog box by default, but after clicking OK no target will be set anyway. The reason for this is the fact that ckEditor also checks the content of the linkTargetName field on submitting, and that value is usually set via the onChange-callback.

So here is an updated version of the JavaScript code which also calls the onChange-callback while setting up the dialog.

/**
 * @file
 * A small plugin to emulate FCKeditor's DefaultLinkTarget setting.
 * 
 * @see http://docs.cksource.com/CKEditor_3.x/Tutorials/Timestamp_Plugin
 * @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Dialog_Customizat... * @see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.pluginDefinition.... */

/**
 * Register a placeholder plugin so CKEditor won't complain.
 */
CKEDITOR.plugins.add('default_target', {
  requires: ['link'],
  init: function(editor) {
    // The plugin itself doesn't actually do anything, but having it is a
    // convenient way to load the file. The only downside is that our event
    // handler will be registered even if the requirements aren't fulfilled,
    // but it won't cause problems. We could of course do all kinds of
    // things here of we wanted.
  },
  onLoad: function() {
  }
});

/**
 * Hook into dialog creation to set our override.
 */
CKEDITOR.on('dialogDefinition', function(ev) {
  var dialogName = ev.data.name, dialogDefinition = ev.data.definition;
  if (dialogName == 'link') {
    // Simulate FCKConfig.DefaultLinkTarget.
    var targetTab = dialogDefinition.getContents('target');
    var targetField = targetTab.get('linkTargetType');
    // Fetch the actual value from the emulated FCKeditor setting, default value set at bottom of file.
    targetField['default'] = ev.editor.config.DefaultLinkTarget;

    var oldSetup = targetField.setup;
    targetField.setup = function(data) {
      targetField.onChange.call(this);
      if (oldSetup) {
        oldSetup.call(this, data);
      }
    }
  }
});

// Sets the default config value to _blank.
// Individual instances can override it as with any other setting.
CKEDITOR.config.DefaultLinkTarget = '_blank';
TwoD’s picture

Status: Fixed » Closed (fixed)

Updated the code in #1 with the change from #6 and added a new default CSS Class feature from #1915198: Set default class to Colorbox in LINK dialog .

sayansk’s picture

Issue summary: View changes

Enable the module and check the "Default link target" box under "Buttons and Plugins" and you're all set!
The third day I struggle with this problem. I'm sorry, but I can not find the "Buttons and Plugins" ... Can you please tell me where to find them? In the settings CKEditor is no such :(

hugovk’s picture

Thanks for this!

Enable the module and check the "Default link target" box under "Buttons and Plugins" and you're all set!

sayansk, find it somewhere like:

Modules -> Wysiwyg -> Configure -> CKEditor -> Edit -> BUTTONS AND PLUGINS -> Default link target