By bigbman on
I'm trying to create a custom module that represents a note, or series of notes that can be attributed with a parent node, taxonomy and other common node features. I'm familliar with the annotate module, but it doesn't actually create drupal nodes. I'm also aware of CCK, but the interface for creation of what I'd call a 'note' is clunky. I want a list of notes available from a tab of any node, and a note creation form below that. Sounds pretty simple eh?
Well, I got as far as rendering the form and a static list (which will be populated later). I'm running into a mental snag of, how do I actually create or submit a "note" node from the parent node view?
Here's the code for my module:
<?php
/**
* Implementation of hook_node_info().
**/
function note_node_info()
{
return array(
'note' => array(
'name' => t('Node Note'),
'module' => 'note',
'description' => t("For notes of existing nodes."))
);
}
/**
* Implementation of hook_perm().
*/
function note_perm()
{
return array('create note', 'edit own note');
}
/**
* Implementation of hook_access().
*/
function note_access($op, $node)
{
global $user;
switch($op)
{
case 'create':
return user_access('create note');
break;
case 'update':
case 'delete':
if (user_access('edit own note') && ($user->uid == $node->uid))
{
return TRUE;
}
break;
}
}
/**
* Implementation of hook_menu().
*/
function note_menu($may_cache)
{
$items = array();
if ($may_cache)
{
/*$items[] = array(
'path' => 'node/add/note',
'title' => t('New Note'),
'access' => user_access('create note'));*/
}
else
{
// we now construct a tab for allowed nodes to show all notes
// if we have an id
if (is_numeric(arg(1)))
{
// we're viewing a node
if (arg(0) == 'node')
{
$nid = arg(1);
$items[] = array(
'path' => 'node/'. $nid .'/notes',
'title' => t('Notes'),
'callback' => 'note_node_ui',
'callback arguments' => array($nid),
'access' => true,
'type' => MENU_LOCAL_TASK,
);
}
}
}
return $items;
}
/**
* Implementation of hook_form().
*/
function note_form(&$node, &$param)
{
$type = node_get_types('type', $node);
watchdog("Note", t("Building form for node '%k'", array('%k' => $param->title)));
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => TRUE,
'#default_value' => t("Note for '%k'", array('%k' => $param->title)),
'#weight' => -5
);
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
//'#default_value' => $node->body,
'#required' => TRUE,
'#weight' => -4
);
$form['body_filter']['filter'] = filter_form($node->format);
$form['parent_nid'] = array(
'#type' => 'hidden',
'#value' => $param->nid,
);
//$form['#action'] = url('node/add/note/'. $node->nid);
//$form['submit'] = array('#type' => 'submit', '#value' => t('Multiple values'));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function theme_note_form ( $form)
{
$output = drupal_render( $form);
return $output;
}
/**
* Implementation of hook_validate()
*/
function note_validate(&$node)
{
}
/**
* Implementation of hook_insert()
*/
function note_insert($node)
{
db_query("INSERT INTO {note} (vid, nid, parent_nid) VALUES (%d, %d, %d)",
$node->vid, $node->nid, $node->parent_nid);
}
/**
* Implementation of hook_update
*/
function note_update($node)
{
/*if($node->revision)
{
note_insert($node);
}
else
{
db_query("UPDATE {note} SET http_link = '%s', http_anchor = '%s' WHERE vid = %d",
$node->http_link, $node->http_anchor, $node->vid);
}*/
}
/**
* Implementation of hook_nodeapi()
*/
function note_nodeapi(&$node, $op, $teaser, $page)
{
switch ($op)
{
case 'delete revision':
//db_query("DELETE FROM {janode} WHERE vid = %d", $node->vid);
return;
}
}
/**
* Implementation of hook_delete()
*/
function note_delete($node)
{
db_query('DELETE FROM {note} WHERE nid = %d', $node->nid);
}
/**
* Implementation of hook_load()
*/
function note_load($node)
{
$sql = "SELECT parent_id FROM {note} WHERE vid = %d";
return db_fetch_object(db_query($sql, $node->vid));
}
/**
* Implementation of hook_view()
*/
function note_view(&$node, $teaser = FALSE, $page = FALSE)
{
$node = node_prepare($node, $teaser);
return $node;
}
/**
*
* @param unknown_type $nid
* @return unknown
*/
function _note_list_by_node( $nid)
{
//$result = db_query( ( "SELECT uid, nid, visibility, note FROM {notes} WHERE nid = %d") , $nid);
$items= array('one','two','three');
/*if( $items)
{
while ($note = db_fetch_object( $result ))
{
$items[] = theme( 'note_item', $note);
}
*/
return $items;
}
function note_node_ui($nid)
{
$node = node_load($nid);
$result = theme( 'item_list', _note_list_by_node( $nid), 'Notes') .
"<br> " .
drupal_get_form('note_form', NULL, $node);
return $result;
}
Comments
perhaps someone could at
perhaps someone could at least point me in the right direction. Is there a module that does something like this? The forms api guidebook is pretty poor about how to submit nodes from forms outside the normail /node/add menu...
Interested, interesting
I don't know how to do this, but I'm also interested in figuring out how to add 'node' content from somewhere other than the standard 'node/add/mynode' path... for achieving some richer web app UI's.
It seems like there might be a module that makes comments into full-fledged nodes(?)... if my recall is correct about that, that might be a similar case? Don't have time to pursue it right yet.
--
Matthew J. Sorenson (emjayess)
d.o. | g.d.o.
I'm getting there!
can now post "notes" on any node type (via a notes tab). The tab also lists all notes for that node. Note nodes have a link to their parents. This module is far from complete. Still need to add in node security, admin settings, and optimizations, among other things. I'm still not convinced that I'm doing this completely right, but for now, it works!
This is the code that renders the form:
And this is the entire node