Posted by jjwang on December 20, 2012 at 2:01am
Hi All,
I am creating a simple custom content type programmatically with Form API and able to save and update the content. However, for some reasons, my content is showing up under the general sidebar menus, not the "Create content" menu where it supposes to be. Here are my codes. Can someone help me what I am missing here?
I am using Drupal 6.
Thanks,
John
<?php
function secondmodule_menu() {
$items['secondmodule'] = array(
'title' => 'Secondmodule',
'page callback' => 'drupal_get_form',
'page arguments' => array('secondmodule_form'),
'access callback' => TRUE,
);
return $items;
}
function secondmodule_form() {
global $user;
$userID=$user->uid;
$type='secondmodule';
$node = node_load(array('uid' => $userID, 'type' => $type));
$first='';
$last='';
$isNewNode=true;
if($node != false) {
$result = db_query("SELECT * FROM {secondmodule} WHERE vid=%d", $node->vid);
while ( $obj = db_fetch_object ($result) ) {
$first=$obj->field_first;
$last=$obj->field_last;
}
$isNewNode=false;
}
$form['isNewNode'] = array(
'#type' => 'hidden',
'#value' => $isNewNode,
);
$form['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
);
$form['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#description' => "Please enter your first name.",
'#default_value' => $first,
);
$form['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#description' => "Please enter your last name.",
'#default_value' => $last,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
'#submit' => array('secondmodule_form_submit'),
);
return $form;
}
function secondmodule_form_submit($form, &$form_state) {
global $user;
$isNewNode= $form_state['values']['isNewNode'];
$nid= $form_state['values']['nid'];
$fname= $form_state['values']['first'];
$lname= $form_state['values']['last'];
$newNode = new stdClass();
if ($isNewNode!=true) {
$newNode = node_load($nid);
} else {
$newNode->title = 'Second Module';
$newNode->body = '';
$newNode->type = 'secondmodule';
$newNode->language = 'en';
$newNode->uid = $user->uid;
$newNode->created = time();
$newNode->changed = $newNode->created;
$newNode->promote = 0;
$newNode->sticky = 0;
$newNode->status = 1;
$newNode->language = 'en';
}
node_save($newNode);
if ($isNewNode!=true) {
db_query(
"UPDATE {secondmodule} SET field_first= '%s', field_last= '%s' "
."WHERE vid = %d",
$form_state['values']['first'],
$form_state['values']['last'],
$newNode->vid
);
} else {
db_query(
'INSERT INTO {secondmodule} (vid, nid, field_first, field_last) '
."VALUES (%d, %d, '%s', '%s')",
$newNode->vid,
$newNode->nid,
$form_state['values']['first'],
$form_state['values']['last']
);
}
drupal_set_message(t('The form has been submitted. '));
}
?>
Comments
You need to use the
You need to use the appropriate hooks to add content types grammatically.
If you are trying to create a
If you are trying to create a new node type, see http://api.drupal.org/api/examples/node_example%21node_example.module/7 and other API docs there and around.
It's not your job to build a whole edit form, or do your own 'create content' menu item if you can let the API system do it for you.
Specifically, you are at least missing the vital hook_node_info() function to start with
.dan. is the New Zealand Drupal Developer working on Government Web Standards
I did try to put hook_node_info() there...
However, Once I have hook_node_info() implemented, the content built-in buttons "Save/Preview" and the "View/Edit" tab layout show up. In my requirement, 1) I don't want the "Preview" button and 2) I have a few custom form buttons I need to add. meanwhile, instead of the "View/Edit" tab layout, 3) I just need a "Edit" page. I thought it would be a good approach to code from the scratch for what I want. With all my requirements above, what is a good approach you think?
Thanks a lot,
John
It sounds like you may not
It sounds like you may not want to use a content type.
A good example of custom
A good example of custom content type.
See http://www.andypangus.com/drupal-7-simple-content-type.
I am using Drupal 6
Unfortunately, that won't help me too much. maybe as the other author mentioned, it is not a good idea to use content type for my business requirement. Thanks.