Hi,
I'm working on a little module which opens a forum topic for each story you create. The story shows a link to the forum topic in place of the comments system and the forum topic shows the teaser of the story and a link back to this story.
The idea is to bring integration between articles and forums and having a specific forum which contains all the news discussion.
I'm trying to use Drupal's nodeapi so as to make this compatible with any type of node and node_save() so as to avoid having to deal with lower-level database calls.
Here are the two main problems I encountered during the development of this module. Any help would be welcome.
PROBLEM #1: using node_save() in a nodeapi 'insert' operation
The nodeapi hook performs actions when a node is submited, inserted, deleted, updated and viewed and node_save() takes a StdClass with the node's properties and creates the node.
I first tried with the submit operation of nodeapi and everything worked fine with the creation of the forum topic but I discovered that submit is also called when I modify a node so I tried with insert. Resulting in forum topics duplicating on each node modification.
The insert operation of nodeapi looks more appropriate for what I want to do. The problem is that insert appears to be called by node_save(). As a result, when I use the node_save() function to save my forum topic, I have an endless loop (the node_save calls nodapi insert operation and the insert operation calls node_save).
Is there a way to save a node without calling the insert operation? Or is there a way, when using the submit operation, to test if the node is created or if it's modified?
PROBLEM #2: Assigning taxonomy to the forum topic before calling node_save()
My second problem is the taxonomy. I would like to force my forum topic to be assigned to one specific forum which I will put as setting for the module. The vocabulary will be forced to "1" which is the vocabulary used by the forum module and the term will also be "1" which is my "News comments" forum. This will be hard-coded for the beginning but I'll later create a settings page where we can set which forum we want to use.
The problem is that whatever I tried to assign to the vid (vocabulary id) and tid (taxonomy id) properties for the forum topic before calling the node_save function, it doesn't seem to take it at all. What's the clean official procedure to assign a vocabulary and a term to a forum?
All of this can of course be done with database calls but the idea is to have clean code using the API as much as possible.
Thanks
Comments
Hello.. Sounds like a good
Hello..
Sounds like a good project, I'd find it useful.
I haven't worked with Taxonomy yet so I can't help you with that, but as for problem #1.
I have a module that also has to save info on insert (new node). I just set a $GLOBAL variable which gets set and used to determine if the procedure has been run. For example:
<?phpcase 'insert':
//Append nodeinfo information to brand new posts that use the inline filter
if (!$GLOBALS['inline_nodeinfo_insert']) {
$new_node = node_load($node->nid);
clear_nodeinfo_tags($new_node);
set_nodeinfo_tags($new_node);
node_save($new_node);
}
// Mark that the node has been updates so we don't loop endlessly
$GLOBALS['inline_nodeinfo_insert'] = true;
return;
?>
I think doing something similar would work for you..
--
Codelica.com - Open Source System Design
Willing to share?
neural, I'm working on a project with the exact same goal as you. I'm wondering if you would be willing to share your code (whatever state it's in), and I'd be happy to share any enhancements back with you.
Excellent Project
Hi, I'm about to embark on exactly the same project!
I was going to use "submit" not "insert" but then I'm new to Drupal. Having found your post and now eagerly await news of progresss and, like dasil003, I'd love to see the code so far - It's all a steep learning curve for me so far!!
Also - while investigating _nodeapi I found that $node->is_new was set to 1 during "insert"
Here's my attemp so far which works ok for my purposes
I have disabled new comments on the page node until I work out how to add a link to the forum
<?php
// $Id$
/*
* @file
* Creates a new forum topic when user has created a new book page.
*
* Also updates the topic with amendments to original page
*
*/
/*
* Implementation of hook_nodeapi().
*/
function page2topic_nodeapi(&$node, $op, $teaser, $page) {
if ($node->type=="forum" && $op=="insert"){
drupal_set_message(t("This <em/>Topic</em/> has been added to the <em/>Forum</em/>"),$status="status");
}
if ($node->type=="book") {
switch ($op) {
case "submit": // prevent comments being added to book pages
$node->comment = 0;
break;
case "insert":
$forum_node = new StdClass();
foreach ($node as $key => $value) {
$forum_node->$key = $value;
}
$forum_node->nid ='';
$forum_node->vid ='';
$forum_node->type = forum;
$forum_node->comment = 2;
$forum_node->tid = 5;
node_save($forum_node);
break;
case "update":
$conditions = array ("type" => forum, "created" => $node->created);
$forum_node = node_load($conditions);
$forum_node->title = $node->title;
$forum_node->taxonomy = $node->taxonomy;
$forum_node->body = $node->body;
node_save($forum_node);
drupal_set_message(t("This <em/>Forum Topic</em/> has been updated"),$status="status");
break;
}
}
}
Trying to get values for pre-inserted node from nodeapi
okay so say I have the following:
function module_nodeapi(&$node, $op, $teaser, $page) {
if ($node->type=="forum" && $op=="insert"){
drupal_set_message(t("This <em/>Topic</em/> has been added to the <em/>Forum</em/>"),$status="status");
}
if ($node->type=="story") {
switch ($op) {
case "submit": // prevent comments being added
$node->comment = 0;
break;
case "insert":
$forum_node = new StdClass();
foreach ($node as $key => $value) {
$forum_node->$key = $value;
}
$forum_node->nid ='';
$forum_node->vid ='';
$forum_node->type = forum;
$forum_node->comment = 2;
//need access cck field info (right now returning NULL)
//how do I access cck field info before it's inserted
$forum_node->body = $node->field_content['value'];
//I have create mirror image of terms in from one vocabulary in one of my forum containers with the same names
//right now $node->tid also returning NULL
$ftid = db_result(db_query('SELECT tid FROM {term_data} where name like (select name from term_data where tid = %d) and tid != %d',$node->tid,$node->tid));
$forum_node->tid = $ftid;
node_save($forum_node);
drupal_set_message(t("This <em/>Forum Topic</em/> has been created to the tid = ".$ftid."with content: ".$node->field_content['value']),$status="status");
break;
case "update":
$conditions = array ("type" => forum, "created" => $node->created);
$forum_node = node_load($conditions);
$forum_node->title = $node->title;
//$forum_node->taxonomy = $node->taxonomy;
$forum_node->body = $node->field_content['value']; //node content
//node_save($forum_node);
drupal_set_message(t("This <em/>Forum Topic</em/> has been updated to the tid = ".$ftid),$status="status");
break;
//case "delete":
//break;
}
}
}
The problem is right now getting the $node->tid and $node->field_content information to plug into the forum node. How do I refer to the form values, I think this is what I've seen done before but not sure if it was from nodeapi.
~fayola
--------
http://www.fayola.net
~fayola
--------
http://www.fayola.net
Opps.... okay I know why I
Opps.... okay I know why I couldn't get my values, because $node->tid does not exist. It's actually $node->taxonomy which returns an array and I forgot that my field also returns an array (in case it's repeating). Sorry for the posting before doing my own troubleshooting.
~fayola
--------
http://www.fayola.net
~fayola
--------
http://www.fayola.net