Tag Archive for 'Drupal'

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

Drupal Tip #3 – Determining Drupal Version

http://www.mydrupal.ca/2010-06-23/what-version-drupal-your-site-using has a helpful check list to go through when you need to determine the drupal version with missing Changelog files:

1. First thing to do is to read the CHANGELOG.txt file. It’s located in the root directory of your Drupal website. All versions of Drupal from 4.5-present have this file. Check the top entry in this file. It will have the Drupal version number. Unfortunately for our purposes today, many developers delete this file on live sites because it is perceived as a security risk.
2. Second thing to do is to check the VERSION constant in modules/system.module. This was added in Drupal 5.0.
3. If you can’t find the VERSION constant in modules/system.module then your version number is most likely 4.7 or earlier. In Drupal 4.7, the site ‘offline for maintenance’ feature was added.
4. In Drupal 4.6, a site-wide contact form (modules/contact.module) and search block were added.
5. In Drupal 4.5, all GIF’s in the default themes were replaced with PNG’s. If you can’t find the modules/contact.module, then your site is most likely Drupal 4.5.
6. You can actually download the 4.x versions on Drupal.org. Then you can compare the files in more detail. This article covers the major releasees and not the minor releases. For example upgrading from 4.51-4.52, is called a minor release.

Drupal Tip #2 – Limiting long strings

Use truncate_utf8 to quickly limit strings to certain lengths, and define whether the break should occur at character level or word level. Also optionally appends ‘ …’. Point to take note is that the ‘ …’ would not add to the resultant length – drupal compensates it by limiting the original string 4 characters less. This would be obvious in the example below, as each output never exceeds the 20 character limit.

For example:

$sentence = 'a quick brown fox jumps over the lazy dog';
echo truncate_utf8($sentence, 20); //a quick brown fox ju
echo truncate_utf8($sentence, 20, TRUE); //a quick brown fox
echo truncate_utf8($sentence, 20, TRUE, TRUE); //a quick brown ...
echo truncate_utf8($sentence, 20, FALSE, TRUE); //a quick brown fo ...

// Output side-by-side:
//a quick brown fox ju
//a quick brown fox
//a quick brown ...
//a quick brown fo ...

Drupal 4.7 – 6:
truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)

Drupal Api Reference: truncate_utf8

Drupal Tip #1 – Formating Time Ago

Drupal has a nice little function called format_interval that formats a time interval in a “x secs ago” fashion. Note that it accepts a time interval, not an absolute time value, thus you need to subtract the time value from time().

For example, to print your node’s created time, use:

<?php echo 'Submitted ',format_interval(time()-$node->created),' ago'; ?>

Drupal API ref for format_interval()

node-view-[viewname].tpl.php not working

If you are using row style: node in your view, its recommended that you use the node-view–[viewname].tpl.php to theme your output instead of views-view-row-node–[viewname].tpl.php.

However, you must make sure node.tpl.php exists in your theme (or subtheme) in order for node-view-* to be picked up by Drupal.

Custom content type not showing up?

You’ve created a custom drupal module to define a content type, its showing up in the create content menu, but its not showing up in the admin/content/type menu. What could be wrong?

Check that you have declared the [module_name]_form() function in your .module file.

Function header:

function [module_name]_form($form_state)

Note: if its not even in the create content menu, you need the [module_name]_node_info() function.