【问题标题】:How to submit node content via Forms API into node如何通过 Forms API 将节点内容提交到节点
【发布时间】:2012-12-30 23:38:44
【问题描述】:

我正在尝试存储自定义内容类型 - 节点 在使用drupal 6.26 的drupal 数据库中。 我已经创建了模式表, 但是当我执行表单的提交按钮时 我收到以下错误:

用户警告:键“PRIMARY”查询的重复条目“0-0”: service_form_submit /* 管理员 : service_form_submit */ INSERT INTO 服务(nid、vid、uid、fallbackservice_ids、类别、biblebasic、 阻塞,reason_blocked,创建,开始,停止)值(0、0、0, '', '', '', 0, '', 0, 0, 0 ) 在 /var/www/drupal/modules/service/service.module 在第 261 行。

并且至少没有条目存储在db中,我输入的nid、vid、uid和其他值似乎都不可用。

我如何传递 $node 是否正确?

function service_form(&$node) {
function service_form_submit($node) {

这里是我如何使用 $node 的完整代码?

<?php
/**
* Implementation of hook_node_info().
*/
function service_node_info() {
  // We return an array since a module can define multiple node types.
  return array(
    'service' => array(
      'name' => t('Service'), // Required.
      'module' => 'service',  // Required. Prefix of the callback functions to look for: service_validate(), service_insert(), service_delete()
      'description' => t('Offer your service to the church.'), // Required.
      'has_title' => TRUE,
      'title_label' => t('Please enter your Service Name'),
      'has_body' => TRUE,
      'body_label' => t('Service Description'),
      'min_word_count' => 2,
      'locked' => TRUE
    )
    // todo here we add more node types later
  );
}


function service_form(&$node) {

// Get metadata for this node type
  // (we use it for labeling title and body fields).
  // We defined this in service_node_info().
  $type = node_get_types('type', $node);

  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Service Information')
  );
  // service name
  $form ['general']['title'] = array(
    '#type' => 'textfield',
    '#title' =>  check_plain($type->title_label), // 'Service Name',  //
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
    '#maxlength' => 255,
  );
  // service description
  $form ['general']['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' =>  check_plain($type->body_label),  // 'Service Description', //
    '#default_value' => $node->body,
    '#rows' => 6,
    '#required' => TRUE
  );
//  Filter options
// $form['body_filter']['filter'] = filter_form($node->format);

// todo replace with taxonomy  - ask forum
  $form ['general'] ['category'] = array(
    '#multiple' => '0',
    '#required' => '1',
    '#key_type_toggled' => '0',
    '#description' => t('Category of the service  '),
    '#default_value' => isset($node->category) ? $node->category : '',
    '#weight' => '3',
    '#type' => 'select',
    '#options' => array(
      '1' => t('Apostelservice'),
      '2' => t('Social Service'),
      '3' => t('Praying Service'),
   ),
  '#multiple_toggle' => '1',
  '#title' => t('Category'),
   );

     $form['availabilty'] = array(
    '#type' => 'fieldset',
    '#title' => t('Service Availabilty') ,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    );


   $form ['availabilty'] ['service_start'] = array(
   '#type' => 'date_popup',
   '#title' => t('Start service on'),
   '#size' => 20,
   '#maxlength' => 20,
   '#default_value' => isset($node->service_start) ? $node->service_start : '',
   );

   $form['availabilty']['service_stop'] = array(
   '#type' => 'date_popup',
   '#title' => t('Terminate Service at'),
   '#size' => 20,
   '#maxlength' => 20,
   '#default_value' => isset($node->service_stop) ? $node->service_stop : '',
   );

   // fallback service
   $form ['availabilty']['fallback_service'] = array(
  '#multiple' => '1',
  '#required' => '0',
  '#key_type_toggled' => '0',
  '#description' => t('Choose a fallback services to avoid bottlnecks'),
  '#default_value' => array(
   '0' => '1',
  ),
  '#type' => 'select',
  '#options' => array(
    '1' => t(' random / no fallback service'),
    '2' => t(' Fallback Service 2'),
    '3' => t(' Fallback Service 3'),
  ),
  '#multiple_toggle' => '1',
  '#title' => t('Fallback Services'),
  );


   $form ['availabilty']['blocked'] = array(
  '#default_value' => array(
  ),
  '#required' => '0',
  '#key_type_toggled' => '0',
  '#description' => t('The service can be disabled - blocked due to different reasons e.g. misuse, clarification needed'),
  '#weight' => '1',
  '#type' => 'checkboxes',
  '#options' => array(
    'one' => t('Block Service'),
   ),
  '#title' => t('Block Service'),
   );
   $form ['availabilty']['reason_blocked'] = array(
   '#required' => '0',
   '#input_format' => '1',
   '#description' => t('Please enter a reason why the Service is disabled, if this is the case.'),
   '#weight' => '2',
   '#type' => 'textarea',
   '#title' => t('Reason of blocked Service'),
   );

   $form['location'] = array(
   '#type' => 'fieldset',
   '#title' => t('Service location information')
   );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Register your service'),
  );

  return $form;
}
**
* Handle submission of the service form and saving
* of the data to the database.
*/
function service_form_submit($node) {

db_query("INSERT INTO {service} (nid, vid,uid,  fallbackservice_ids , category , biblebasic ,
blocked , reason_blocked , created , begin , stop) VALUES (%d, %d, %d, '%s', '%s', '%s',  %d, '%s', %d, %d, %d )",
$node->nid, $node->vid,$node->uid,  $node->fallbackservice_ids, $node->category,
  $node->biblebasic, $node->blocked, $node->reason_blocked,  $node->created, $node->begin, $node->stop);
  }
?>

【问题讨论】:

    标签: forms drupal


    【解决方案1】:

    您使用自定义模块执行此操作有什么特别的原因吗?为什么不直接创建一个新的内容类型?

    如果你真的想用你自己的模块来做,你看过examples module吗? 你真正想看的代码是here

    【讨论】:

    • 我查看了示例,但它没有使用提交挂钩。我希望用户可以通过表单和提交按钮提交内容。我不知道如何将简单的表单存储在自定义内容类型中。我同意,通常我应该通过 cck 创建我的自定义内容类型,但是每次我阅读带有提交按钮的表单时,建议是使用 Forms API,以进一步处理数据。但是我如何使用表单 API没有自定义模块。我有大约 10 个不同的表,它们链接在一起,我认为它更干净?
    • 如果您不需要实际的节点来提交表单,那么您可能不需要节点模块,只需创建一个提供表单的模块并使用表单 api。在这种情况下,您不会使用 hook_node_info 而是使用 hook_menu 为页面提供表单并使用您自己的提交函数。我的猜测是这可能是你应该这样做的方式。如果您也想要一个节点,那么您可以按照节点示例模块的方式进行操作,也可以在保存节点时使用 hook_nodeapi 来完成您的工作。另外,也许 webform 模块可以做你需要的一切。
    • 我认为我需要一个防御节点,因为对于我的内容类型服务,我想添加 fifestar 评级和地理位置信息。我认为这只有在我有一个节点的情况下才有可能,不是吗?如果我像示例那样做,我不知道如何放置提交按钮以使其正常工作。对于 webform 模块,我在某处读到,如果我想在节点中保存数据以供进一步处理,这不是一个好的解决方案,这就是我不走这条路的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多