Custom Drupal 7 Field only saves the first character(自定义 Drupal 7 Field 只保存第一个字符)
本文介绍了自定义 Drupal 7 Field 只保存第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实际上是在尝试使用新的 Drupal 7 Field API 创建我的第一个字段类型模块.我设法让它在编辑"视图中正确显示.
但是,当我尝试保存一些数据时,它只保存了第一个字符.
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.
这是模块:
<?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(),
);
}
问题似乎与小部件有关,因为它在以编程方式获取数据时工作正常.
The problem seems to be related to the widget, as it works fine when it gets the data programmatically.
推荐答案
这个小部件表单对我有用:
This widget form worked for me:
function youtubefield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta,
$element) {
$main_widget = array();
switch ($instance['widget']['type']) {
case 'youtubefield_video_widget':
$main_widget = $element + array(
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
);
break;
}
$element['value'] = $main_widget;
return $element;
}
这篇关于自定义 Drupal 7 Field 只保存第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:自定义 Drupal 7 Field 只保存第一个字符
基础教程推荐
猜你喜欢
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
