Rich Text Editor Widget in Symfony 1.1
I was pretty amazed to discover that there doesn't seem to be a rich text editor widget for the new Symfony 1.1 forms framework. The forms part of the new version is fairly controversial, and it's not intended to be anything like backward compatible with 1.0 forms/validators, but I was genuinely suprised to see there wasn't a rich text editor out-of-the-box. Here's how to get one, fairly easily:
Install TinyMCE the same way you would in Symfony 1.0. This involves downloading TinyMCE, and making it web accessible. Then you have to uncomment and edit the rich_text_js_dir directive in your app's settings.yml.
To check you have TinyMCE installed correctly, before continuing, create a template with:
<?php use_helper('Form') ?>
<?php echo textarea_tag('some', 'content',
array('rich' => true)) ?>
Then view the template. You should see the TinyMCE editor. Now we just need to get it working with the new forms system. Introducing myWidgetFormRichTextarea:
/**
* myWidgetFormRichTextarea represents a rich text editor.
*
* @author Dominic Scheirlinck <[email protected]>
*/
class myWidgetFormRichTextarea extends sfWidgetFormTextarea
{
/**
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetForm
*/
protected function configure($options = array(), $attributes = array())
{
$this->addOption('editor', 'tinymce');
$this->addOption('tinymce_options', '');
$this->addOption('tinymce_gzip', false);
$this->addOption('css', false);
parent::configure($options, $attributes);
}
/**
* @param string $name The element name
* @param string $value The value displayed in this widget
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$editorClass = 'sfRichTextEditor' . $this->toCanonicalCase($this->getOption('editor'));
if (!class_exists($editorClass)) {
throw new sfConfigurationException(sprintf('The rich text editor "%s" does not exist.', $editorClass));
}
$editor = new $editorClass();
if (!in_array('sfRichTextEditor', class_parents($editor))) {
throw new sfConfigurationException(sprintf('The editor "%s" must extend sfRichTextEditor.', $editor));
}
$attributes = array_merge($attributes, $this->getOptions());
$editor->initialize($name, $value, $attributes);
return $editor->toHTML();
}
/**
* Converts a lower-case editor name to its canonical case
*
* @param string $editor
* @return string
*/
private function toCanonicalCase($editor)
{
switch ($editor) {
case 'tinymce':
return 'TinyMCE';
case 'fck':
return 'FCK';
}
}
}
If you're just using it to replace a textarea, you don't need to do anything much to use the rich widget – it'll use your existing rows and cols html attributes, and make the rich editor the right size automatically.
This might not work with future versions of Symfony, because it relies on the sfRichTextEditorTinyMCE class, which I think is only used to provide backward compatibility with 1.0.