自定义 Drupal 7 字段仅保存第一个字符
我本质上是尝试使用新的 Drupal 7 Field API 创建我的第一个字段类型模块。 我已经设法让它在“编辑”视图中正确显示。
但是,当我尝试保存一些数据时,它只保存第一个字符。
这是模块:
<?php
function youtubefield_field_info() {
return array(
'youtubefield_video' => array(
'label' => t('Youtube video'),
'description' => t('This field stores a youtube video ID and displays the video associated with it'),
'settings' => array(
'max_length' => 11,
),
'instance_settings' => array(
'text_processing' => false,
),
'default_widget' => 'youtubefield_video_widget',
'default_formatter' => 'youtubefield_video_formatter',
),
);
}
function youtubefield_field_widget_info() {
return array(
'youtubefield_video_widget' => array(
'label' => t('Default'),
'field types' => array('youtubefield_video'),
),
);
}
function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element['#type'] = 'textfield';
$element['#default_value'] = isset($items[$delta]) ? $items[$delta] : '';
return $element;
}
function youtubefield_field_is_empty($item, $field) {
return !$item;
}
function youtubefield_field_formatter_info() {
return array(
'youtubefield_video_formatter' => array(
'label' => t('Youtube video'),
'field types' => array('youtubefield_video'),
),
);
}
这是安装程序:
<?php
function youtubefield_field_schema($field) {
return array(
'columns' => array(
'value' => array(
'type' => 'varchar',
'length' => 11,
'not null' => true,
),
),
'indexes' => array(),
);
}
编辑:问题似乎与小部件有关,因为它以编程方式获取数据时工作正常。
I'm essentially trying to create my first field type module using the new Drupal 7 Field API.
I've managed to get it to display correctly in the "edit" view.
However, when I try to save some data it only saves the first character.
Here's the module:
<?php
function youtubefield_field_info() {
return array(
'youtubefield_video' => array(
'label' => t('Youtube video'),
'description' => t('This field stores a youtube video ID and displays the video associated with it'),
'settings' => array(
'max_length' => 11,
),
'instance_settings' => array(
'text_processing' => false,
),
'default_widget' => 'youtubefield_video_widget',
'default_formatter' => 'youtubefield_video_formatter',
),
);
}
function youtubefield_field_widget_info() {
return array(
'youtubefield_video_widget' => array(
'label' => t('Default'),
'field types' => array('youtubefield_video'),
),
);
}
function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element['#type'] = 'textfield';
$element['#default_value'] = isset($items[$delta]) ? $items[$delta] : '';
return $element;
}
function youtubefield_field_is_empty($item, $field) {
return !$item;
}
function youtubefield_field_formatter_info() {
return array(
'youtubefield_video_formatter' => array(
'label' => t('Youtube video'),
'field types' => array('youtubefield_video'),
),
);
}
And here's the installer:
<?php
function youtubefield_field_schema($field) {
return array(
'columns' => array(
'value' => array(
'type' => 'varchar',
'length' => 11,
'not null' => true,
),
),
'indexes' => array(),
);
}
EDIT: The problem seems to be related to the widget, as it works fine when it gets the data programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个小部件形式对我有用:
This widget form worked for me:
我遇到了同样的问题,发现 hook_field_widget_form() 需要返回:“$element['value']”
I had the same problem and discoverd hook_field_widget_form() needs to return: "$element['value']"
将您的小部件表单更改为:
Change your widget form to: