简体   繁体   English

为drupal中的自定义textarea表单字段启用WYSIWYG

[英]Enable WYSIWYG for a custom textarea form field in drupal

i have created a custom form in my custom drupal6 module and the form has a textarea. 我在自定义drupal6模块中创建了一个自定义表单,表单有一个textarea。 I am outputting the form as html embedded inside the php code, like the following: 我输出的表单是嵌入在php代码中的html,如下所示:

function custom_my_form() 
{
$f = '<form name="add_user" action="" method="post">
<label>  Name </label>
<p><input type="text" name="name" value="" /></p>
<label>About Yourself</label>
<p><textarea name="desc"></textarea></p>
<input type="submit" name="submit" value="Add">
</form>';

return $f;
}

I have installed and enabled the WYSIWYG module. 我已经安装并启用了WYSIWYG模块。 My default input format is "Filtered HTML" and i have chosen the FCK editor for this input format. 我的默认输入格式是“Filtered HTML”,我为此输入格式选择了FCK编辑器。

Now i want to enable the WYSIWYG for the textarea field in this form. 现在我想在此表单中为textarea字段启用WYSIWYG。 How can i go ahead with this ? 我怎么能继续这个呢?

For D7, its even simple. 对于D7来说,它甚至简单。 We got new type called text_format. 我们得到了一个名为text_format的新类型。

$form['comment'] = array('#type' => 'text_format', 
                         '#format' => 'full_html', 
                         '#title' => t('Description'), 
                         '#required' => TRUE);

Well, how ever you create forms is up to you, it just depends if it has ever come back to bite you in the butt... 好吧,你怎么创造表格取决于你,这取决于它是否曾经回来咬你的屁股......

Look at the comment module. 看一下评论模块。 I had noticed that it was possible to choose input format for comments, and when Full/filtered HTML was selected, the WYSIWYG editor kicked in. Here is the related code: 我注意到可以为注释选择输入格式,当选择完整/过滤HTML时,WYSIWYG编辑器就会启动。以下是相关代码:

$form['comment_filter']['comment'] = array(
  '#type' => 'textarea',
  '#title' => t('Comment'),
  '#rows' => 15,
  '#default_value' => $default,
  '#required' => TRUE,    

);
if (!isset($edit['format'])) {
  $edit['format'] = FILTER_FORMAT_DEFAULT;
}
$form['comment_filter']['format'] = filter_form($edit['format']);

So, you define an array with two elements, one of which is the textarea itself, and the other one the format chooser generated by filter_form, and that's all. 因此,您定义了一个包含两个元素的数组,其中一个是textarea本身,另一个是filter_form生成的格式选择器,这就是全部。

This was from http://groups.drupal.org/node/104604 这是来自http://groups.drupal.org/node/104604

Yes, you can do this. 是的,你可以这样做。 Just add the following javascript to your page. 只需将以下javascript添加到您的页面即可。

<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
        tinymce.init({selector:'textarea'});
</script>

This script automatically converts the text areas in your html code to rich text editors 此脚本自动将html代码中的文本区域转换为富文本编辑器

More info at this link . 更多信息,请点此链接

First, you should not create forms like that. 首先,您不应该创建这样的表单。 Instead, use Drupal's built-in Forms API. 相反,使用Drupal的内置Forms API。 Take a look at the Form API Quickstart Guide for more information. 有关详细信息,请参阅Form API快速入门指南

This page on Drupal's site will help you add WYSIWYG to your custom forms: http://drupal.org/node/358316 Drupal网站上的这个页面将帮助您将WYSIWYG添加到您的自定义表单: http//drupal.org/node/358316

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM