Hi! I'm creating a module for faqs. I was thinking that it would be easy to store the data in the node table by making the body look like:

<question>....</question><answer>...</answer>

I would then have two separate entry blocks, and when the data is saved to the database, I would modify the body to properly format the data. I've written most of the stuff, but how do I tell Drupal what $node->body should be? hook_insert/_update don't have a modifiable $node passed to them (and the body field may already have been written?).

Thanks,
Ricky

Comments

Steve Dondley’s picture

Try a search. I definitely remember someone writing about this. Don't know if it's publicly available.

--
Get better help from Drupal's forums and read this.

pukku’s picture

Hi! I did try a search before I posted — I looked for variations on $node->body, body, change node body, etc., but didn't seem to turn up anything relevant. Perhaps I missed something?

Michael M’s picture

Find out more at the nodeapi handbook page(http://drupaldocs.org/api/head/file/contributions/docs/developer/example...) , but here is a sample:

function condo_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'view':
      if ($node->type == 'mytype') {
        $node->body = mymodule_output($node, $teaser) .'or whatever data you want';
      }
      break;
  }
}

---
http://LandCondos.com

pukku’s picture

Hi! I've looked at hook_nodeapi — my concern is whether it gets called before the node is written to the node table. Your example above is for displaying the data, which I do know how to do. My concern is how to save the data to the database in the first place by altering $node->body before the save takes place...

nevets’s picture

Since you are writing a module I would use two fields, one for the question, one for the answer. You can use body (from node) for one and would need an additional table for the other. This way the data remains seperated for editing.

Then in the view hook you can modify the body as needed. I would suggest using a theme function, that way someone using phptemplate could modify the output if desired.

rjung’s picture

Another vote for the custom node type -- if you're already creating a custom module anyway, you might as well make it a custom node module. All you'd need is to implement hook_insert(), hook_update(), hook_form(), and hook_delete() to update your own data in addition to the regular Drupal node "body" field. See the Drupal example.module for details.

I'm not sure why you'd need to actually modify the body, though -- it's probably enough to just wrap your "questions" and "answers" inside DIV blocks and then use a theme function to format them as desired. Separation of content from presentation gives you greater flexibility in the long run.

--R.J.
http://www.electric-escape.net/

--R.J.

pukku’s picture

Hi! I've already written this module using a separate table. However, I'm thinking that the extra table is unnecessary, and it leaves the body basically unused. My hope was that I could just use the node table.

What I'm trying to do is to basically do the wrapping in a div — however, I want to have two separate entry textareas in the editing view. I could just as easily change it to split on <div class="question">, etc., but how do I set the body field to have the divs?

rjung’s picture

Having two textareas implies that your node has two text fields, one per text area. Since the standard Drupal node has one text field (body), what is the point of having a second text area if you don't have a second text field to go with it?

I suppose you could go through all the trouble of parsing $node->body for your question and answer DIVs, displaying them in separate text areas, and then re-concatenating them before performing a node update, but why? Having a second text field to support the second text area (and writing a little theming code to display the Qs and As) would be a lot easier by comparison.

--R.J.
http://www.electric-escape.net/

--R.J.

pukku’s picture

I've been reading the source to node.module, trying to figure this out. It looks like hook_nodeapi is called with an op of 'fields' before anything is done. So if I implemented hook_nodeapi and modified the body of the node when called with the op of 'fields', it should work? Does this make sense? Can anyone think of a reason that this would be a problem?

Thanks,
Ricky

nevets’s picture

I would argue trying to stuff two data inputs in one field just to "save" using another table is not the best approach. One, besides combining the two fields for storage, you will need to split the stored data into two parts for editing. So you save a minor amount of space at the exspense of more complicated code. You also mix program logic with presentation since the form of presentation is embedded in the stored body. Using the view function combined with a theme function seperates the program logic from the presentation while allowing others to override your choice for presentation.