function wysiwyg_load_editor in Wysiwyg 6.2
Same name and namespace in other branches
- 5.2 wysiwyg.module \wysiwyg_load_editor()
- 5 wysiwyg.module \wysiwyg_load_editor()
- 6 wysiwyg.module \wysiwyg_load_editor()
- 7.2 wysiwyg.module \wysiwyg_load_editor()
Load an editor library and initialize basic Wysiwyg settings.
Parameters
$profile: A wysiwyg editor profile.
Return value
TRUE if the editor has been loaded, FALSE if not.
See also
1 call to wysiwyg_load_editor()
- wysiwyg_get_profile in ./
wysiwyg.module - Determine the profile to use for a given input format id.
File
- ./
wysiwyg.module, line 317 - Integrates client-side editors with Drupal.
Code
function wysiwyg_load_editor($profile) {
static $settings_added;
static $loaded = array();
$path = drupal_get_path('module', 'wysiwyg');
$name = $profile->editor;
// Library files must be loaded only once.
if (!isset($loaded[$name])) {
// Load editor.
$editor = wysiwyg_get_editor($name);
if ($editor) {
// Determine library files to load.
// @todo Allow to configure the library/execMode to use.
if (isset($profile->preferences['library']) && isset($editor['libraries'][$profile->preferences['library']])) {
$library = $profile->preferences['library'];
$files = $editor['libraries'][$library]['files'];
}
else {
// Fallback to the first defined library by default (external libraries can change).
$library = key($editor['libraries']);
$files = array_shift($editor['libraries']);
$files = $files['files'];
}
// Check whether the editor requires an initialization script.
if (!empty($editor['init callback'])) {
$init = $editor['init callback']($editor, $library, $profile);
if (!empty($init)) {
// Build a file for each of the editors to hold the init scripts.
// @todo Aggregate all initialization scripts into one file.
$js_path = file_create_path('js');
file_check_directory($js_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$js_path .= '/';
$hash = base64_encode(hash('sha256', $init, TRUE));
// Modify the hash so it's safe to use in URLs.
$hash = strtr($hash, array(
'+' => '-',
'/' => '_',
'=' => '',
));
$filename = 'wysiwyg_' . $name . '_' . $hash . '.js';
$init_exists = file_exists($js_path . $filename);
// Attempt to create the file, or fall back to an inline script (which
// will not work in Ajax calls).
if (!$init_exists && !file_save_data($init, $js_path . $filename, FILE_EXISTS_REPLACE)) {
drupal_add_js($init, 'inline', 'header', FALSE, TRUE, TRUE);
}
else {
$path_prefix = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE ? 'system/files/js/' : $js_path;
drupal_add_js($path_prefix . $filename, 'core', 'header', FALSE, TRUE, TRUE);
}
}
}
foreach ($files as $file => $options) {
if (is_array($options)) {
$options += array(
'type' => 'module',
'scope' => 'header',
'defer' => FALSE,
'cache' => TRUE,
'preprocess' => TRUE,
);
drupal_add_js($editor['library path'] . '/' . $file, $options['type'], $options['scope'], $options['defer'], $options['cache'], $options['preprocess']);
}
else {
drupal_add_js($editor['library path'] . '/' . $options);
}
}
// If editor defines an additional load callback, invoke it.
// @todo Isn't the settings callback sufficient?
if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
$editor['load callback']($editor, $library);
}
// Load JavaScript integration files for this editor.
$files = array();
if (isset($editor['js files'])) {
$files = $editor['js files'];
}
foreach ($files as $file) {
drupal_add_js($editor['js path'] . '/' . $file, 'module', 'footer');
}
// Load CSS stylesheets for this editor.
$files = array();
if (isset($editor['css files'])) {
$files = $editor['css files'];
}
foreach ($files as $file) {
drupal_add_css($editor['css path'] . '/' . $file);
}
$loaded[$name] = TRUE;
}
else {
$loaded[$name] = FALSE;
}
}
// Add basic Wysiwyg settings if any editor has been added.
if (!isset($settings_added) && $loaded[$name]) {
drupal_add_js(array(
'wysiwyg' => array(
'configs' => array(),
'plugins' => array(),
'disable' => t('Disable rich-text'),
'enable' => t('Enable rich-text'),
),
), 'setting');
// Initialize our namespaces in the *header* to do not force editor
// integration scripts to check and define Drupal.wysiwyg on its own.
drupal_add_js($path . '/wysiwyg.init.js', 'core');
// The 'none' editor is a special editor implementation, allowing us to
// attach and detach regular Drupal behaviors just like any other editor.
drupal_add_js($path . '/editors/js/none.js');
// Add wysiwyg.js to the footer to ensure it's executed after the
// Drupal.settings array has been rendered and populated. Also, since editor
// library initialization functions must be loaded first by the browser,
// and Drupal.wysiwygInit() must be executed AFTER editors registered
// their callbacks and BEFORE Drupal.behaviors are applied, this must come
// last.
drupal_add_js($path . '/wysiwyg.js', 'module', 'footer');
$settings_added = TRUE;
}
return $loaded[$name];
}