By webdevel on
I've spent the last couple days browsing the forum and I can't seem to find an answer to my question. What I am trying to do is add a new field to node/add/story, store that into a new table in the database, and then use those values elsewhere on my site. Currently I figured out how to add the field using hook_form_alter.
function ftb_rtstocks_form_alter($form_id, &$form) {
switch ($form_id)
{
case 'story_node_form':
$options = array(0 => t('Unrated'), 1, 2, 3, 4, 5);
$form['ftb_rtstocks'] = array(
'#type' => 'fieldset',
'#title' => t('Stocks')
);
$form['ftb_rtstocks']['symbols'] = array(
'#type' => 'textfield',
'#title' => t('Symbols'),
'#description' => t('Enter a stock symbol.')
);
$form['ftb_rtstocks']['#weight'] = -1;
}
}
and I figured out how to use the info in my database using hook_nodeapi()
function ftb_rtstocks_nodeapi(&$node, $op, $teaser, $page) {
switch ($op)
{
case 'update':
case 'insert':
ftb_rtstocks_node_save($node->nid, $node->ftb_rtstocks);
break;
case 'insert':
ftb_rtstocks_node_save($node->nid, $node->ftb_rtstocks);
break;
case 'view':
$types_to_ftb_rtstocks = variable_get('ftb_rtstocks_nodetypes', array('story'));
if (!in_array($node->type, $types_to_ftb_rtstocks))
{
break;
}
// Get previously saved symbols.
$result = db_query("SELECT symbols FROM {ftb_rtstocks} WHERE nid = %d", $node->nid);
$node->ftb_rtstocks = db_result($result);
// Add stock ticker as a content item.
$node->content['ftb_rtstocks_ticker'] = array('#value' => theme('nodeapi_ftb_rtstocks', $node->ftb_rtstocks), '#weight' => 10, );
}
}
However I can't figure out how to write my "symbols" field into the database. Right now I have:
function ftb_rtstocks_node_save($nid, $form_values) {
$symbols = $form_values['symbols'];
db_query("DELETE FROM {ftb_rtstocks} WHERE nid = %d", $nid);
db_query("INSERT INTO {ftb_rtstocks} (nid, symbols) VALUES (%d, '%s')", $nid, $symbols);
drupal_set_message(var_dump($form_values));
}
....but that only writes the nid and not the "symbols" field from my form that I am inserting into node/add/story.
Thanks for your ideas to point me in the right direction!
Comments
Are you aware that you can use CCK for this?
The content construction kit (CCK) module already provides this abiliity without the need to write code.
Yeah, I'm just trying to
Yeah, I'm just trying to learn how to do it without using CCK so I can develop more advanced custom modules later on. I'm so close to getting it but I can't seem to figure out that one little part :-)
There is no $node->ftb_rtstocks
When you place fields inside a fieldset, the array element used to group the items (in your case ftb_rtstocks) is only used as part of the resulting data if '#TREE' is TRUE. So by default you use the field name (in your cases 'symbols'), so
should be
A handy "trick" when things like this are not working is to add to the nodeapi hook a call like
which will show you all the nodes fields in the theme's message area.