Customizing a block - Adding a Rich Text Editor

Permalink
I'm trying to tweak a block to convert a text field to a rich text area.

The original block has a description field, but it's a regular text field. I would like to convert it to a rich text area.

I've added:
Loader::element('editor_controls');

to the top of my block.

And I've replaced the original text field code:
<input type="text" value="<?php  echo $imgInfo['Description']; ?>" name="Description[]" id="Description" style="width:100%;">


with:
<?php  Loader::element('editor_controls'); ?>
<textarea id="Description" name="Description[]" class="ccm-advanced-editor"><?php  echo $imgInfo['Description']; ?>


This gives me the rich text editor when I edit the block. But when I make changes to the content in the editor field, it does not update the database.

Does anyone know what I might be missing?

Thanks.

 
ssrgspdkt replied on at Permalink Reply
ssrgspdkt
Hi, you need to add as follows:
<?php 
Loader::element('editor_config');
Loader::element('editor_controls'); 
?>
<textarea name="Description" id="Description" class="ccm-advanced-editor"><?php echo $imgInfo['Description'] ?></textarea>
Metaglyphics replied on at Permalink Reply
Thanks for the reply. I've added
Loader::element('editor_config');


but I get the same issue. The editor shows the existing content in the field, but if I edit it, it ignores my changes.
ssrgspdkt replied on at Permalink Reply
ssrgspdkt
you need to add the following function in your controller.
function translateFromEditMode($text) {
      // now we add in support for the links
      $text = preg_replace(
         '/{CCM:CID_([0-9]+)}/i',
         BASE_URL . DIR_REL . '/' . DISPATCHER_FILENAME . '?cID=\\1',
         $text);
      // now we add in support for the files
      $text = preg_replace_callback(
         '/{CCM:FID_([0-9]+)}/i',
         array('[BLOckcontrollerClassName]', 'replaceFileIDInEditMode'),
         $text);
      $text = preg_replace_callback(
         '/{CCM:FID_DL_([0-9]+)}/i',
         array('[BLOckcontrollerClassName]', 'replaceDownloadFileIDInEditMode'),
         $text);


you need to replace your controller name instead of "[BLOckcontrollerClassName]" in my code.

you also need to add the following code in view function
$imginfo['Description']=$this->translateFrom($this->Description);

you also need to add the following code in save function
$args['Description'] = $this->translateTo($args['Description']);

you also need to add the following code in edit function
$args['Description'] = $this->translateFromEditMode($this->Description);

sometimes you may need to change property name:Description based on your block.
Metaglyphics replied on at Permalink Reply
Thanks again. Definitely getting there...

I'm now getting an error for each of the $text references. Example:

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, '[MyBlockController]::replaceCollectionID', to be a valid callback in (path to controller file)


Any idea what might cause that?
ssrgspdkt replied on at Permalink Reply
ssrgspdkt
Please remove square bracket.
Metaglyphics replied on at Permalink Reply
Thanks. Silly late-night mistake!

So I think the problem now is simply that the textarea contents aren't saving. The block I'm modifying doesn't actually have a public function save() command. It does have:

function save($data) { 
$db = Loader::db();
$args['thumbWidth'] = isset($data['thumbWidth']) ? intval($data['thumbWidth']) : 150;
$args['thumbHeight'] = isset($data['thumbHeight']) ? intval($data['thumbHeight']) : 100;
if( count($data['imgFIDs']) ){
$args['fsID'] = 0;
//delete existing images
$db->query("DELETE FROM tableName WHERE bID=".intval($this->bID));
//loop through and add the images
$pos=0;
foreach($data['imgFIDs'] as $imgFID){          
if(intval($imgFID)==0 || $data['fileNames'][$pos]=='tempFilename') continue;
$vals = array(
intval($this->bID),
intval($imgFID),


I have a feeling some variation of the
$args['Description'] = $this->translateTo($args['Description']);


line needs to go in here somewhere, but I'm not hitting on something that's working.