Drupal & CKeditor Load Configuration Settings On Dynamic Page

In Drupal the ckeditor hooks into form so that it populates all of the profile configuration information on page load. This works pretty well unless you use an ajax call to dynamically load a textarea. This code will allow you to pull that info for a given profile and place a ckeditor on the textarea. It only utilizes a few of the settings available. You should be able to easily modify to suit your purposes.

Just to note, it was a D6 site, ckeditor module using 3.6.4

function get_cke_settings($profile){
 
    module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
 
    $full_profile = ckeditor_profile_load($profile); 
    $profile_settings = $full_profile->settings;
 
    //send this piece of the array to ckeditor function that populates paths to/for plugins
    $plugin = $profile_settings[loadPlugins];
    $plugin_paths = ckeditor_plugins_render($plugin);
 
    //replace this part of the array with part returned from function
    $profile_settings[loadPlugins] =  $plugin_paths;
   //if you have encoding type issues try using php's encoder
  //  print json_encode($profile_settings);
  drupal_json($profile_settings);
}

You’ll need to add a menu path for the function. Here’s the js that makes an ajax request to this function. I just added this to the success function of the request that loads the dynamic textarea. The id was ‘agenda-text’ – dont forget to change this to match whatever you have. I also set it up so that the last parameter passed to the function is the name of the profile.

$.getJSON('path/to/function/MINItoolbar', function(data) {
 
var result = data;
toolbarSetting =   result.toolbar;
skinSetting    =   result.skin;
plugins        =  result.loadPlugins;
js_conf        =  result.js_conf;
stylesPath     =   "drupal:" + result.styles_path;
 
   jQuery.extend(Drupal.settings, {
        "ckeditor": {
               "settings": 
                      {
                       "agenda-text": {                  
                           "toolbar": toolbarSetting,
                            "skin": skinSetting,
                            "loadPlugins": plugins,
                            "stylesCombo_stylesSet": stylesPath,        
                                }
                            }
                        }
                  });
 
Drupal.ckeditorOn('agenda-text');
 
});

One thought on “Drupal & CKeditor Load Configuration Settings On Dynamic Page”

Leave a Reply

Your email address will not be published.