Posted by maen on November 3, 2012 at 4:36pm
Hello at all,
I need a bit advise to create my first module...
Goal: insert nodes with an additional formular, made by myself. It's a first step for me. What I did:
I use the example.module and crated with this source the following code:
function test_menu() {
$items = array();
$items['test/form'] = array(
'title' => t('My form'),
'page callback' => 'test_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function test_form() {
return drupal_get_form('test_my_form');
}
function test_my_form($form_state) {
// This is the first form element. It's a textfield with a label, "Name"
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
//// Adds a simple submit button that refreshes the form and clears its
//contents -- this is the default behavior for forms.
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function test_insert($node) {
db_query("INSERT INTO {node} (vid, nid) VALUES (%d, %d)", $node->vid, $node->nid);
}With firebug I see as post:
form_build_id form-f0e679c0f42012048763a185f2ca7987
form_id test_my_form
form_token 7228d9756aa80afaa41314333f1ae19c
op Submit
title egal
title=egal&op=Submit&form_build_id=form-f0e679c0f42012048763a185f2ca7987&form_token=7228d9756aa80afaa41314333f1ae19c&form_id=test_my_formBut no insert in DB. So can anybody please explain what I do wrong?
Thx in advance,
maen
Comments
First off, you are getting
First off, you are getting yourself in trouble, you should not be manipulating the node table directly. It is better to use node_save().
What is you actual goal here, the code mostly suggests a custom form (not a node) but saving to the node table suggests a custom content type. For the later using the UI is a simpler choice, and if using code there is an API that should be followed.
My goal is as follows:
I want a possibility to manipulate some fields from a node by clicking the field (I use custom php for these fields) in views. With this click, a popup shall open with a form that loads the node object from the source that was clicked. The form itself shows only two fields, the title (static value prefilled) and a cck field (prefilled but overwritable) which was clicked.
Now, although I' ve been using drupal since a long time, I want to write my first own module. So in my first step, I try to do some basics by writing a form to insert a node. Then when I understand how it works I want to learn how it is possible to load a node in a from and update the node. Then I want to use AHAH and jquery or anything like this to manipulate forms to transfer automatically transfer values from a CCK field 1 to a CCK field 2, update the node and close the pop-up form by clicking on save. After that I reached two goals: a basic understanding of drupal, jquery and ajax and a fundament to provide individual dashlets for authors and moderators, which could be useful in many perspectives.
I don't want to use editview or something like this because I also want to learn drupal.
Thx,
maen