Search code examples
javascriptdrupaldrupal-7ckeditor

Add attributes to link in CKEditor 4 in Drupal


I want to add rel="nofollow" to all links in the CKEditor. I have read a lot of questions about it and also tried my best with the documentation. However I cannot get it working.

I am using Drupal 7 (Not the wysiwyg module, just the ckeditor module with cdn version 4).

Code that I have tried:

var editor = new CKEDITOR.editor();
CKEDITOR.on('instanceReady', function( ev ) {
  editor.dataProcessor.htmlFilter.addRules(
  {
    elements :
    {
      a : function( element )
      {
        console.log(element.attributes);
          if ( !element.attributes.rel )
              element.attributes.rel = 'nofollow';
      }
    }
  });
});

This code was what I found in other questions. In the documentation I can't find the addRules function, and if I put a breakpoint inside of the function, I see that it never gets called.

I would really appreciate some input!


Solution

  • Do you need to get it done through CKeditor's config? Because This can be configurated within the Drupal interface:

    1. Configuration > Text formats > Choose the input format ex: Filtered HTML
    2. check Limit allowed HTML tags
    3. scroll down to the vertical tab Limit allowed HTML tags
    4. check Add rel="nofollow" to all links