我嘗試使用drupal 6.26在drupal數據庫中存儲自定義內容類型 - 節點 。 我已經創建的架構表已經, 但是當我米執行提交表單 的按鈕,我得到了跟隨着錯誤:如何通過Forms API將節點內容提交到節點
user warning: Duplicate entry '0-0' for key 'PRIMARY' query: service_form_submit /* admin : service_form_submit */ INSERT INTO service (nid, vid,uid, fallbackservice_ids , category , biblebasic , blocked , reason_blocked , created , begin , stop) VALUES (0, 0, 0, '', '', '', 0, '', 0, 0, 0) in /var/www/drupal/modules/service/service.module on line 261.
和至少沒有條目存儲在數據庫中,無論是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);
}
?>
我看了一下這個例子,但是它沒有使用提交鉤子。我希望用戶可以通過表單和提交按鈕提交內容。我不知道我怎麼能有一個簡單的形式存儲在自定義內容類型。我同意,通常我應該通過cck創建我的自定義內容類型,但每次閱讀有關提交按鈕的表單時,建議使用Forms API,以便進一步處理數據。但是,如何使用表單API沒有自定義模塊。我有大約10個不同的桌子連在一起,我認爲它更乾淨? –
如果您不需要表單提交的實際節點,那麼您可能不需要節點模塊,只需創建一個提供表單並使用表單api的模塊。在這種情況下,您不會使用hook_node_info,而是使用hook_menu爲您的表單提供頁面並使用您自己的提交功能。我的猜測是這可能是你應該這樣做的方式。 如果你確實需要一個節點,那麼你可以按照節點示例模塊的方式來完成,也可以在保存節點時使用hook_nodeapi來完成你的工作。 另外,也許webform模塊可以做你需要的一切。 – 2pha
我認爲我需要防禦性的節點,因爲對於我的內容類型服務,我想添加fifestar評分和地理位置信息。我認爲這是唯一可能的,如果我有一個節點,是嗎?如果我像這個例子那樣做,我不知道如何放置提交按鈕來讓它工作。對於webform模塊,我已經在某處讀到,如果我想在節點中有數據進行進一步處理,這不是一個好的解決方案,這就是我不這樣做的原因。 –