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');
 
});

PHP Script to list and display all images in a directory

<?php
 
$path = "./";
$dir_handle = @opendir($path) or die("Unable to open folder");
 
while (false !== ($file = readdir($dir_handle))) {
 
	if (strpos($file,'.jpg') ||strpos($file,'.gif')||strpos($file,'.png')) {
		echo "<img src='$file' alt='$file'><br />";
	}
}
closedir($dir_handle);
 
?>

Demo

Drupal – Pass Data To Template File

Probably most useful for passing large blocks of data that can be iterated over from within the template file. Using this method allows the developer to keep the application logic separate from the presentation layer. The data being passed in would most typically be an array.

1) Create the function that will generate the data block i.e.

function template_preprocess_my_form(&$variables) {
     $data = array("foo" => "bar", "bar" => "foo");
     $variables['template_data'] = $data;
}

The important part is to name your function appropriately so that it will hook the correct form. In this instance the form hook is my_form.

2) Modify hook_theme to accomodate the variable

function my_module_theme() {
    $theme = array(
    'my_form' => array(
            'arguments' => array('form' => NULL,
                                 'template_data' => NULL),
            'template' => 'my_form_template',
        ),
    );
    return $theme;

Here it is important to ensure the array element key matches the variable that was added to the form’s variable array – in this case it is template_data

3) Access the variable you have created in the template file. The variable will available by adding the $ to the variable created in the steps above, in this case $template_data, and it can be used as a normal php variable, i.e.

<?php
foreach ($template_data as $i => $values) :
     echo $template_data[$i]
endforeach;
?>

Php long running script to dynamically update html

This code is a work in progress and should never be used in a production environment without extensive testing!

First, the page that is loaded into the browser, it uses jQuery to receive status updates from ‘getstatus.php’ script

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Update Inventory</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
 
     var flag = false
     var stop = false;
 
$(function() {
    startProcess();
    getStatus();
 
 
});
 
function startProcess() {
 
    $("#done").load('updatestock.php', function() {
 
        flag = true;
 
    });
 
}
 
function getStatus() {
 
    if ( stop ) {return false;}
 
    $("#status").load('getstatus.php');
    setTimeout("getStatus()",2000);
 
	if ( flag ) {stop = true;}
 
}
 
</script>
 
<style type="text/css">
div {padding:0 0 0 50px;}
</style>
 
</head>
<body>
	<h1>Long Running Script</h1>
	<div id="done"></div>
	<div id="status"></div>
</body>
</html>

This is the long running script simulator

 
<?php
 
$fh = fopen('status.log', 'w'); 
//simulate work for 30 seconds
    while($i < 30) {
	fwrite($fh, time()."<br />\n");
      sleep(1);
      $i++;
    }
 
fclose($fh);
 
?>

And getstatus.php, the script that returns data to be appended to div

<?php
/*
this script is accessed by the html version of the updater, and spools the output to the screen 
so the user can see updated information as the the script runs.
*/
 
$page = file_get_contents('status.log');
echo $page;
 
?>

Try it here (buggy)