如何通过Forms API将节点内容提交到节点中

时间:2012-12-30 23:38:44

标签: forms drupal

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

  

用户警告:键'PRIMARY'查询重复输入'0-0':   service_form_submit / * admin:service_form_submit * / INSERT INTO   服务(nid,vid,uid,fallbackservice_ids,类别,biblebasic,   blocked,reason_blocked,created,begin,stop)VALUES(0,0,0,   '','','',0,'',0,0,0)in   第261行/var/www/drupal/modules/service/service.module。

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

我通过$节点的方式是否正确?

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

1 个答案:

答案 0 :(得分:1)

使用自定义模块是否有任何特殊原因?为什么不创建一个新的内容类型?

如果你真的想用你自己的模块做这件事你有没看过examples module? 您真正想要查看的代码是here