drupal - 在 drupal 7 中将表单元素添加到节点以供用户输入

我在我的模块中制作了我自己的内容类型。它有文本和图像字段。

我现在想添加一个表单字段,如单选框和一个提交按钮,以便在呈现节点时用户可以提供他们的输入。我做不到。

1) 当我尝试在创建内容类型时添加单选框时,我的第一次尝试失败了。

simplequiz_radio11' => array(
'field_name' => 'simplequiz_radio11,
**'type'       => 'radio',**

除了文本和图像,我在类型字段中写什么。我想再次提一下,当节点未处于编辑模式时,单选框需要接受用户输入。

2) 我尝试使用 drupal_get_forms 并且我能够使用 drupal_get_forms 将表单及其元素附加到节点但是我的节点有很多图像(字段)并且我需要每个图像的表单元素所以它不实用这边走。下面是我的代码:

function simplequiz_node_view($node, $view_mode, $langcode)
{  
$f = drupal_get_form('simplequiz_form', $node);
$f['#weight'] = 100;
$node->content['data_collection_form'] = $f;
}

这工作正常,但我如何为 10 个图像字段执行此操作?似乎第一个选项更好,但我似乎无法获得正确的字段类型。

//我设法解决了它,因为我的逻辑是错误的我需要使用格式化程序而不是 drupal 中的字段。

function simplequiz_field_formatter_info() {
return array(

'simplequiz_example_field' => array(
  'label' => t('radio button on node'),
  'field types' => array('text'),
),
 'simplequiz_btn_field' => array(
  'label' => t('button on node'),
  'field types' => array('text'),
), 

);


}

/**
* Implements hook_field_formatter_view().
*/


function simplequiz_field_formatter_view($object_type, $object, $field, $instance,    
$langcode, $items, $display) {
switch ($display['type']) {
 case 'simplequiz_example_field':
  $element[0]['#type'] = 'markup';
  $element[0]['#markup'] = theme('simplequiz_example_field', array('value' => $items[0]   
['safe_value']));
  break;

case 'simplequiz_btn_field':
  $element[0]['#type'] = 'markup';
  $element[0]['#markup'] = theme('simplequiz_btn_field', array('value' => $items[0]  
['safe_value']));
  break;
 }
 return $element;
 }

/**
* Theme the example field.
 */
function theme_simplequiz_example_field($variables) {
return '<form><input type="radio" name="type" value="silver">Gold'
.$variables['value']. ' </br> <input type="radio" name="type" value="Gold"> Silver </br>   
</form>'

    ;
}

function theme_simplequiz_btn_field($variables) {
 return '<input type="submit" class="form-submit" value="Submit" name="op" id="edit-   
submit--2">'

    ;
}

干杯, 维沙尔

最佳答案

//我设法解决了它,因为我的逻辑是错误的我需要使用格式化程序而不是 drupal 中的字段。

function simplequiz_field_formatter_info() {
return array(

'simplequiz_example_field' => array(
  'label' => t('radio button on node'),
  'field types' => array('text'),
),
 'simplequiz_btn_field' => array(
  'label' => t('button on node'),
  'field types' => array('text'),
), 

);


}

/**
* Implements hook_field_formatter_view().
*/


function simplequiz_field_formatter_view($object_type, $object, $field, $instance,    
$langcode, $items, $display) {
switch ($display['type']) {
 case 'simplequiz_example_field':
  $element[0]['#type'] = 'markup';
  $element[0]['#markup'] = theme('simplequiz_example_field', array('value' => $items[0]   
['safe_value']));
  break;

case 'simplequiz_btn_field':
  $element[0]['#type'] = 'markup';
  $element[0]['#markup'] = theme('simplequiz_btn_field', array('value' => $items[0]  
['safe_value']));
  break;
 }
 return $element;
 }

/**
* Theme the example field.
 */
function theme_simplequiz_example_field($variables) {
return '<form><input type="radio" name="type" value="silver">Gold'
.$variables['value']. ' </br> <input type="radio" name="type" value="Gold"> Silver </br>   
</form>'

    ;
}

function theme_simplequiz_btn_field($variables) {
 return '<input type="submit" class="form-submit" value="Submit" name="op" id="edit-   
submit--2">'

    ;
}

干杯, 维沙尔

https://stackoverflow.com/questions/8271261/

相关文章:

ruby-on-rails - Rails - I18n - 如何本地化 f.collection_

iphone - 使用场景转换动画化 UIKit 项目

xml - HTML 到 XML,反之亦然,并在运行时从 UI - JAXB 使用模式进行验证?

asp.net - 使用分页在 RadGrid Telerik 中查找记录

Ruby Gem 安装失败 - 执行 gem 时出错 - 未初始化的常量 Syck::Syck

ruby-on-rails-3 - 无法修改关联,因为源反射类通过 :has_many + rail

continuous-integration - 自动构建 Access 2007 数据库

django - 如何更改方法 is_valid() 中的表单字段

macos - Mac 上的实时动态壁纸(桌面)

sql-server - 'Insert into linked table failed, RAI