Monday, June 1, 2009

drupal nodereference 2.0 - tagging with reviews

Cascade delete referencing nodes



One of the most frequently requested feature about drupal nodereference is "on delete cascade" of referred nodes when you remove the referenced one. This is a pretty easy task to accomplish if you are using the nodereferrer module:


function mymodule_nodeapi(&$node, $op, $params = NULL, $page = NULL) {
switch ($op) {
case 'delete':
if ($node->type == 'my_content_type_name') {
$referrers = array_keys(nodereferrer_referrers($node->nid));
foreach ($referrers as $referrer) {
node_delete($referrer);
}
}
break;
}
}


you can obtain the same effect without nodereferrer module: you have to query the database to obtain the referencing nodes nids like this:


function mymodule_nodeapi(&$node, $op, $params = NULL, $page = NULL) {
switch ($op) {
case 'delete':
if ($node->type == 'my_referenced_content_type_name') {
$result = db_query("SELECT nid FROM {content_type_referrer_name} r WHERE
r.field_content_reference_nid = %d", $node->nid);
while ($referrer = db_fetch_object($result)) {
node_delete($referrer->nid);
}
}
break;
}
}


Ok i guess you already knew those ones :]

Notifications when adding referencing nodes (ie. reviews)



Now the pulp: another task I had to implement for a website was "notify users about new nodes added referencing a content type". In other words: notify users when a review is written about an article. "Review" is a content type referencing another content type "article". I use the notifications module and afaik there's nothing implemented there that take care of this issue.

I added this snippet in my custom module:


function _oninsert_notification(&$review, $op) {
if(module_exists('notifications')) {
$article = node_load($review->field_article_ref[0]['nid']);
$event = array(
'module' => 'node',
'uid' => $article->uid,
'oid' => $article->nid,
'type' => 'node',
'action' => $op,
'node' => $article,
'params' => array('nid' => $article->nid),
);
notifications_event($event);
}
}


and hooked this one into the nodeapi (review) insert operation.

This adds an event in the queue and will send an email to users that have subscribed an article once a review is written about it.

Tagging with referencing nodes



Last but not least: tagging. What if you have to tag an article writing a review ? You have to add the tags to the referenced node (the article) but they normally are assigned to the review only. To get this behaviour I added this code to my module:


function mymodule_nodeapi(&$node, $op, $params = NULL, $page = NULL) {
switch ($op) {
case 'insert':
if (isset($node->field_article_ref[0]['nid'])) {
$article = node_load($node->field_article_ref[0]['nid']);
$tags = array_keys(taxonomy_node_get_terms($node));
foreach ($tags as $tag_tid) {
db_query('INSERT IGNORE INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)',
$article->nid, $article->vid, $tag_tid);
}
break;
}
}


This works if you have a tag vocabulary assigned to both content types (this will save at least some joins :)

So now you have got tags assigned to both reviews and articles. Does it worth ? Sure !
You can now count how many times an article is tagged with a specified tag and create suggested tags and tag clouds:


/*
* nid is the article node nid
*/
function _most_used_tags($nid = NULL) {
$terms = array();
if (is_numeric($nid)) {
$referrers = array_keys(nodereferrer_referrers($nid));
if ($referrers) {
$result = db_query_range("SELECT t.*, COUNT(t.name) AS rweight FROM {term_data} t
INNER JOIN {term_node} n ON t.tid = n.tid WHERE t.vid = %d
AND n.vid IN (". db_placeholders($referrers) .")
GROUP BY t.tid ORDER BY rweight DESC",
array_merge(array(VID_TAGS) /* vocabulary vid */, $referrers),
0 /* starting offset */, 10 /* number of tags returned */);
while ($term = db_fetch_object($result)) {
$terms[$term->tid] = $term;
}
}
}
return $terms;
}


this will return the most used tags about the article. Those tags are added when inserting reviews.

Can the community tags module do this ? Absolutely it can't. It does not let you associate tags inline with content editing/writing. This is the reason i didn't use it in my project.

my 3¢

2 comments:

Anonymous said...

would you have some time to help me implement what you discuss above?

Notifications when adding referencing nodes (ie. reviews)

my email is admin at traderstell dot com

Thanks!

berk said...

https://saglamproxy.com
metin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
YEUA1N

Post a Comment