As part of a larger need, and just wanting to understand the forms API, I'm trying to write a simple module that will add a textarea to an existing form, save the data, and retrieve it when editing. I can do everything except populate the field with the database value on edit. Can someone help out.

Here is what I have so far.

<?php


function service_details_form_alter($form_id, &$form) {
 if ($form['type']['#value'] == 'eproject') {
  $form['servicedetails'] = array(
   '#type' => 'textarea',
   '#title' => t('Additional Service Information'),
   '#default_value' => '',
   '#prefix' => '<div id="servicedetails">',
   '#suffix' => '</div>',
   '#required' => FALSE,
   '#weight' => -10,
  );
 return $form;
 }
}

function service_details_nodeapi(&$node, $op, $page) {
 switch($node->type) {
  case "eproject":
   switch($op) {
    case ("insert"):
     $sql = db_query("replace into service_details (nid,tid,details) values (%d,1,'%s')",$node->nid,$node->servicedetails);
     break;
    case ("update"):
     $sql = db_query("replace into service_details (nid,tid,details) values (%d,1,'%s')",$node->nid,$node->servicedetails);
     break;
    case 'load':
     $details = db_result(db_query('SELECT details FROM {service_details} WHERE nid = %d and tid=1', $node->nid));
     $form['servicedetails'] = array(
      '#value' => $details,
     );
    break;
   }
  break;
 }
}
?>

Comments

epicflux’s picture

I really encourage you to use cck and views to handle your custom data. Those two modules will handle all the data storing and retrieval, and then you just deal with the data output. And when you need to move to drupal 6, its just an update, not re-writing your module completely.

But if you really don't want to do that then what you're doing wrong here is in the nodeapi function. In that function you're working with the node object, not adding values to the form array.

So you need to change your code to add your custom value to the node object.

Harley_Dog’s picture

thanks for the reply, but I'm not sure what exactly to change. Could you be more specific.

I tried the following without luck

<?php
case 'load':
     $details = db_result(db_query('SELECT details FROM {service_details} WHERE nid = %d and tid=1', $node->nid));
     $node->content['servicedetails'] = array(
      '#value' => $details,
      );
    break;
?>
aaronbauman’s picture

what epicflux means is "you're doing it wrong".
if you're just adding a single text area to the end of a node form, use cck instead of what you're doing.

Harley_Dog’s picture

As I stated, I'm trying to understand the Forms API and develop a module. Adding the textarea is only one component of what I want to do. I'm aware of the CCK module. I can appreciate the suggestions for alternate approaches, but I'm also interested in how to solve this one problem. I thought the API was so great, that new fields could be rendered on the fly with ease.

aaronbauman’s picture

ok, i see. well, the forms API is great, albeit with a somewhat steep learning curve.
i could also go on to gripe about forms api vs nodeapi and why aren't they better integrated, etc... but i digress.

first, if you haven't already, bookmark the FAPI quickstart and FAPI reference. You will use those oftentimes.

Next, are you sure the db fields are not populating?
For starters, as epicflux pointed out, your nodeapi load operation is populating $form instead of $node:

 $form['servicedetails'] = array(
      '#value' => $details,
     );

Check your db and odds are you'll see the fields in service_details are actually populated.

Also: is there a particular reason you're using REPLACE on insert? you should presumably be using INSERT on insert and UPDATE on update...
Otherwise, you may as well combine these two case statements.

Harley_Dog’s picture

Thank you...

I can confirm that the database fields are saving correctly. If I start with a new form, I can save data in the textarea added in form_alter. I have also added print "Service Details Information: " . $node->content['servicedetails']['#value']; to my .tpl.php file and can successfully display the data when viewing the form. Now, my only unresolved issues, is to populate the textarea on edit, so that users can modify as necessary.

You can see in my second post, that I did change the $form syntaex to

$node->content['servicedetails'] = array(
      '#value' => $details,
    );

but that did not help. What am I missing? Should I specify a default value in the form_alter function?

epicflux’s picture

Please, please, please use the cck module, pretty please.

Tell you what, you don't even have to use it, just install it for now and setup a field that will store the data you want. Watch it handle the field, save the data, and display it. Then uninstall the module.

Then just keep doing what you're doing.

Harley_Dog’s picture

Yes CCK, I have used this to create many content types. Now will you tell me how to display the saved data on the form for users to edit?

epicflux’s picture

CCK will handle that for you, after you've saved the node the field should have the data in it when you view the form.

rafinskipg’s picture

I'm also interested on doing that.

I had altered a form (advanced profile form), and inserted javascript fields with a new module, now I'd like to save the value of those fields in the database, and when I load the profile, populate those values into my javascript fields.

If someone knows how to do it, it will be very good.

And I know CCK, It's just CCK can't handle what I want to do. The fields I created are jscripts from a different page, external one.

Thanks for your help.

rafinskipg’s picture

I mean, can I alter the form_submit of the node in order to include that values?
Is that possible??