drupal 7 自定义字段中的所见即所得
我正在尝试在 drupal 7 中构建一个自定义字段。除了所见即所得字段之外,一切都工作得很好。 我正在使用下一个代码来构建一个所见即所得元素:
$element['my_body'] = array(
'#title' => t('Editor'),
'#type' => 'text_format',
'#tree' => true,
'#rows' => 20,
'#format' => 'filtered_html',
);
当它不是所见即所得(常规文本区域)时,所有保存都很好,但是在我将其更改为text_format之后,drupal将字段的值作为带有2个键(值和格式)的数组获取,这会在 drupal 保存字段值时出错。 据我了解,drupal 期望获得的是两个不同的值(body_filter 和 format),而不是具有 2 个键(值和格式)的 body_filter 数组。
任何人都可以给我提示如何解决这个问题(在 google 和 drupal.org 中找不到任何相关内容)?
谢谢。
I am trying to build a custom field in drupal 7. evrything work just fine, except from the wysiwyg field.
I am using the next code to build an WYSIWYG element:
$element['my_body'] = array(
'#title' => t('Editor'),
'#type' => 'text_format',
'#tree' => true,
'#rows' => 20,
'#format' => 'filtered_html',
);
When its not wysiwyg (regular textarea) all save go fine, but after i change it to text_format, drupal get the value of the field as array with 2 keys (value and format), and that's make an error while drupal save the values of the field.
As much as i understnad it, what drupal expect to get is two deferent values (of body_filter and format) and not an body_filter array with 2 keys (value and format).
Anyone can give me a hint how to solve this issue (can't find anything relevant in google and drupal.org)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,并找到了解决方案,感谢 Berdir 的提示。
正如您提到的“text_format”返回一个包含两个值的数组,例如:
使用 hook_field_presave() 我能够准备这些值以将其保存到我的数据库中。
因为我不想保存格式值,
我只是从 ['MY_WYSIWYG_FIELD'] 数组中提取文本值,并将该数组替换为提取的值:
我的钩子如下所示:
希望这有帮助!
I bumped into the same problem, and found a solution, thanks to Berdir's hint.
As you mentioned 'text_format' returns an array with two values like:
Using hook_field_presave() I was able to prepare the values to save it into my db.
As I don't want to save the format value,
I simply extract the text value from the ['MY_WYSIWYG_FIELD'] array and replace the array with the extracted value:
my hook looks like this:
Hope this helps!
text_format 类型字段的值为
$form_state['values']['my_body']['value']
。The value of text_format type fields come as
$form_state['values']['my_body']['value']
.是的,text_format 字段的值是一个数组,这就是它应该的样子。
Drupal 不会自动保存某些内容,您在哪里以及如何保存它?您只需修复该代码即可使用数组。
Yes, the value of a text_format field is an array, that's how it is supposed to be.
Drupal does not save something automatically, where and how are you saving it? You simply need to fix that code to work with an array.