Monthly Archives: December 2010

Creating nodes programmatically in Drupal (including cck and location fields)

Creating a new node in drupal is trivial, but when the node includes custom cck fields, then a few things can trip you up. To save time and energy, here’s a quick guide and some tips to make your drupal life easier.

Creating a basic node:

global $user; //get current logged in user
$new_node = new stdClass();
$new_node->type = 'YOUR_NODE_TYPE_HERE';
$new_node->uid = $user->uid; //you can specify some other userID here if you want
$new_node->name = $user->name;
$new_node->title = $YOUR_NODE_TITLE;
$new_node->body = $YOUR_NODE_BODY;
$new_node->status = 1; // published
node_save($new_node);

The function node_save() does a few things behind the scene. It checks if the nid property is empty to determine if this is a new insert or an update, then calls the appropriate functions so that you would not have duplicates. Put simply, if you want to do an update, populate the nid, and leave it out otherwise.

Creatnig a node with CCK fields involves this additional line (for each field):

$new_node->field_your_field[0]['value'] = $YOUR_FIELD_DATA;

Point to note is the [0]['value'], because CCK treats each field as multiple-value fields even if you specified otherwise in the field settings. If you see the error Fatal error: Cannot unset string offsets in …/modules/cck/content.module on line 1248, you’re probably missing the [0]['value'] and setting the value directly into $new_node->field_your_field.

To create a node with location, we need to save the location first to get a location ID before saving it along with our node:

$location = array(
'street' => $address,
'postal_code' => $postal,
);
$locationID = location_save($location);
$new_node = new stdClass();
// ... the usual stuff ...
$new_node->locations[0]['lid'] = $newLocationId;
node_save($new_node);

There you go. Another method is to manually craft an array to simulate a $_POST from a drupal form. To do that you basically have to know the form structure, populate the require fields and pass the array on to the drupal_execute() call. Refer to the example here for more details.

Drupal references:
Node_save: drupal documentation

Size mismatch when getting BitmapData from Camera

Recently while trying to capture a frame from my webcam in Adobe Air, I realize the captured image is always smaller than the height or videoHeight from the Video object. The BitmapData documentation revealed that the BitmapData.draw() operation does not respect any matrix transformation applied on the Camera or Video object, so we need to do the scaling ourselves. A quick and dirty way would be:

 

//var video:Video; and a Camera object is already attached
var matrix:Matrix = new Matrix();
matrix.scale(video.width / 320, video.height / 240);
bitmapData.draw(video,matrix,null,null,null,true);

 

 

Wireframing Kits

Just like you shouldn’t start building a project from scratch, you shouldn’t start crafting a wireframe out of a blank canvas as well. The place where you should start is here:

http://www.smashingmagazine.com/2010/02/05/50-free-ui-and-web-design-wireframing-kits-resources-and-source-files/

Great looking stencil kits for building everything from iPhone, browser, facebook, MacOS apps – even Blackberry. Preview the iPhone kit below

Font Recognizer

Not everyone can recognize a font by their distinct cross bar or descender. For the less enlightened – me included – this tool that helps you recognize fonts. Submit an image and it will try to match it to their database of fonts.

http://new.myfonts.com/WhatTheFont/

If you’re in the mood for a little more on typeface anatomy, check out http://www.fonts.com/AboutFonts/Articles/fyti/Anatomy.htm and http://wallpampers.com/misc/2799