通过上一篇《在symfony项目中应用TinyMCE》文章,我们知道了,如何在symfony配置TinyMCE,如何在模板中应用TinyMCE,
可是,如果我们想在apps/backend中应用应该如何办呢?
通过在google的搜索,有两个方法。
1、配置genertor.yml文件,和config下面的app.yml、setting.yml文件,在网上一搜可以搜索到很多,可是,昨天试了一上午
也没有测试出来,这里就不讲了,主要说一下第二种方法。
2、定义form
我们在lib下面新建一个sfWidgetFormTextareaTinyMCE.class.php文件
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfWidgetFormTextareaTinyMCE represents a Tiny MCE widget.
*
* You must include the Tiny MCE JavaScript file by yourself.
*
* @package symfony
* @subpackage widget
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfWidgetFormTextareaTinyMCE.class.php 11894
2008-10-01 16:36:53Z fabien $
*/
class sfWidgetFormTextareaTinyMCE extends sfWidgetFormTextarea
{
/**
* Constructor.
*
* Available options:
*
* * theme: The Tiny MCE theme
* * width: Width
* * height: Height
* * config: The javascript configuration
*
* @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('theme', 'advanced');
$this->addOption('width');
$this->addOption('height');
$this->addOption('config', '');
}
/**
* @param string $name The element name
* @param string $value The value selected 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())
{
$textarea = parent::render($name, $value, $attributes, $errors);
$js = sprintf(<<<EOF
<script type="text/javascript">
tinyMCE.init({
mode: "exact",
elements: "%s",
theme: "%s",
%s
%s
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_statusbar_location: "bottom",
theme_advanced_resizing: true
%s
});
</script>
EOF
,
$this->generateId($name),
$this->getOption('theme'),
$this->getOption('width') ? sprintf('width:
"%spx",', $this->getOption('width')) : '',
$this->getOption('height') ? sprintf('height:
"%spx",', $this->getOption('height')) : '',
$this->getOption('config') ?
",\n".$this->getOption('config') : ''
);
return $textarea.$js;
}
}
接下来我们需要重新定义lib/form下我们需要的form(e.g. 我们定义的description字段)
public function configure()
{
$this->widgetSchema['description'] = new sfWidgetFormTextareaTinyMCE(array(
'width' => 450,
'height' => 350,
'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
));
}
最后大家别忘记在layout.php 中定义js的路径
javascripts: [js/tiny_mce.js]