Hi,

I've been following the mini-tutorial on creating custom content types at http://drupal.org/node/132845. My implementation of hook_form() looks like the following:

function audit_report_form(&$node) {
  $type = node_get_types('type', $node);
  
    // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
  );
 
  // Form field for the OPINION
    $form['selectbox'] = array(
    '#type' => 'select',
    '#title' => t('Opinion'),
    '#default_value' => $node->selectbox,
    '#options' => array(
      1 => 'Pending',
      2 => 'Qualified',
      3 => 'Unqualified',
      4 => 'Adverse',
      5 => 'Disclaimer',
    ),
    '#description' => t('Please choose an opinion.'),
  );
  
    // Form field for the EXPLANATION OF OPINION
  $form['body_filter']['explaination_of_opinion'] = array(
    '#type' => 'textfield',
    '#title' => check_plain('Explanation of opinion'),
    '#default_value' => $node->explaination_of_opinion,
    '#required' => FALSE,
  );
    
  $form['body_filter']['filter'] = filter_form($node->format);

  // NOTE in node_example there is some addition code here not needed for this simple node-type

  return $form;
}

When I submit my content and then view it, I can see the page title fine, but the other fields (understandably) don't turn up. I've playing around with the hook_view() and hook_nodeapi() functions without success. I haven't found any clear documentation either.

The question is, how do I go about ensuring that nodes of type 'audit_report' display a page with the title and all the custom fields in it (perhaps allowing me to inject custom HTML around it).

TIA,

Xynner

Comments

nevets’s picture

Have you implemented hook_insert, hook_update and hook_delete?

xyn’s picture

Oops-my code was infested with a spelling mistake - ignore this request!

Thanks, no - I didn't. I can get hook_insert() inserting into my custom table, but am now having issues grabbing the form data for the new node fields. It's inserting the nid and vid, but the other fields are coming up as null when I try and insert them (accessed via $node->opinion). Can anyone hold my hand here? :-)

Here's my hook_insert():

function audit_report_insert($node) {
	db_query("INSERT INTO {node_audit_report} (vid, nid, opinion, explanation) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->opinion, $node->explanation_of_opinion);
}

And here's my hook_form():

function audit_report_form(&$node) {
  $type = node_get_types('type', $node);
  
    // We need to define form elements for the node's title and body.
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
  );
 
  // Form field for the OPINION
    $form['opinion'] = array(
    '#type' => 'select',
    '#title' => t('Opinion'),
    '#default_value' => $node->selectbox,
    '#options' => array(
      1 => 'Pending',
      2 => 'Qualified',
      3 => 'Unqualified',
      4 => 'Adverse',
      5 => 'Disclaimer',
    ),
    '#description' => t('Please choose an opinion.'),
  );
  
    // Form field for the EXPLANATION OF OPINION
  	$form['explaination_of_opinion'] = array(
    	'#type' => 'textfield',
    	'#title' => check_plain('Explanation of opinion'),
    	'#default_value' => $node->explaination_of_opinion,
    	'#required' => FALSE,
 	 );
    
  $form['body_filter']['filter'] = filter_form($node->format);

  return $form;
}
ff1’s picture

The one thing I have noticed is that your insert query contains the values in the wrong order. Instead of:
db_query("INSERT INTO {node_audit_report} (vid, nid, opinion, explanation) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->opinion, $node->explanation_of_opinion);
Try:
db_query("INSERT INTO {node_audit_report} (vid, nid, opinion, explanation) VALUES (%d, %d, %d, '%s')", $node->vid, $node->nid, $node->opinion, $node->explanation_of_opinion);

'%s' - used for text strings
%d - used for numbers

Hope that helps.
ian
Fantasy Formula 1