-
db_query() and db_placeholders() example #1
Have you ever wondered how to properly build a query like this in Drupal:
1SELECT nid, type, title FROM node n WHERE n.type IN('page','story');This requires the use of db_placeholders() to create the placeholder ‘ ‘, ‘ ‘, etc.
1$result = db_query('SELECT nid, type, title FROM {node} n WHERE n.type IN(' . db_placeholders($node_types, 'text') . ')', $node_types);where $node_types is an array of node types.
-
Automatically create a Drupal node when a user registers
Sometimes projects come up that have requirements that cannot be solved with an out-of-the-box Drupal install. A project I worked on some time ago required that I store a node every time a user registered for an account. The details of the project are a little fuzzy for me now, so I’ll just skip right to the example.
1234567891011121314function mymodule_user($op, &$edit, &$account, $category = NULL){switch($op){case 'insert':// Automatically create the note settings (content profile) node for this new user$newnode = new stdClass();$newnode->title = "Note settings user {$account->uid}";$newnode->uid = $account->uid;$newnode->type = 'note_settings';$newnode->status = 1;$newnode = node_submit($newnode);node_save($newnode);break; //insert}}This example shows how simple it is to programmatically create a node, and also shows the proper way to hook into the user system.