php - Custom Drupal 7 Field 只保存第一个字符

标签 php drupal drupal-7 drupal-fields drupal-field-api

我实际上是在尝试使用新的 Drupal 7 Field API 创建我的第一个字段类型模块。 我设法让它在“编辑” View 中正确显示。
但是,当我尝试保存一些数据时,它只保存了第一个字符。

这是模块:

<?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(),
  );
}

编辑:问题似乎与小部件有关,因为它以编程方式获取数据时工作正常。

最佳答案

这个小部件形式对我有用:

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;
}

关于php - Custom Drupal 7 Field 只保存第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426362/

相关文章:

使用 MySQL 显示表状态的 php 日期格式

php - 如何打印数组的相似值?

image - 在节点字段中嵌入外部图像链接,而不将它们下载到自己的站点?

php - 从看门狗将值插入到表中

带有 MySQL 集群的 PHP/PDO

PHP复制功能和Apache组

php - Laravel 5,子域路由,带可选参数

php - 从服务器调用中获取奇怪的 IP 地址(PHP、Drupal)

css - 当 float 图像超过特定宽度时,如何使用 CSS 向下移动文本?

drupal - 我的新 drupal 主题没有显示。如何在安装或手动创建后显示 drupal 主题?