By Bagz on
I have a form with a text area set to use CKEditor. I also have a Javascript attached to the form which will modify some of the CKEditor behaviour:
$form['content_html'] = array(
'#title' => t('Text area for editing HTML content'),
'#type'=>'text_format',
'#cols' => 100,
'#rows' => 10,
'#default_value' => $content_html,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit changes',
);
// attach the JS to the form
$form['#attached'] = array(
'js' => array(
'type' => 'file',
'data' => $somepath . '/ckeditor_mods.js',
),
);
Problem: When Drupal renders the form it loads my ckeditor_mods.js before it loads the CKEditor scripts, with the result that my script does not run due to a "CKEDITOR is not defined" error.
My question is, how can I load a script that will execute after Drupal has loaded the CKeditor?
Is there a hook that I can use (please tell me there is!)?
Thanks in advance..
Comments
Solved
I have given myself a pat on the back for figuring it out.
The trick is to use
drupal_add_js($somepath . '/ckeditor_mods.js', array('defer' => true));This sets the defer attribute on the script tag; which specifies that the execution of a script should
be deferred (delayed) until after the page has been loaded.
See http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/7
and
http://www.w3schools.com/tags/att_script_defer.asp for more info.
For those who are interested, this allows you to set CKEditor to read only mode in Drupal forms using the new setReadOnly() function in CKEditor 3.6.1.