programming

Creating and Updating Nodes Programmatically in Drupal 7

The steps for programmatically creating a node are:

  • Create a PHP object representing the node data
  • Save the object using the node_save() function

While the mechanics are simple, there is an important responsibility involved. The Drupal work flow does data validation before calling the node_save() function; node_save() does no validation. By calling node_save() directly your code takes the responsibility for providing valid data.

Drupal 7 Changes

A quick note for those of you familiar with Drupal 6. You'll notice two changes in Drupal 7:

Body Field Is No Longer Special

In Drupal 6 the body field was special. Specifically, it had a different data structure than other fields and it always existed, even if it wasn't used. With Drupal 7 the body field is a standard field provided by core and is truly optional.

Language

Language specification is required for the node and some fields.

Filter Format

The format of the text field filter is now specified by machine name, not integer. For example: full_html, filtered_html, or plain_text. This is great news for moving contents between different Drupal systems.

Basic Node Creation

The following code assumes an unchanged Drupal 7 Standard installation and will create a Drupal 7 article node.

New Zend Podcast: PHP Abstract

Zend has a new PHP Developer Podcast called PHP Abstract:

PHP Abstract LogoEach episode features a different special guest bringing you their expertise, compressed into a five to seven minute package.

We will be covering a broad range of topics. From security to project planning and hiring good developers and much more.

Their first podcast, PHP Abstract Episode 1 - PHP Security Tips, featured Eli White. What a relief to have someone skip over the same rehashed security material into things less commonly talk about.

Off to a good start in my books. At under 10 minutes a shot I'll be subscribing to check them out. (And have already suggested Dries as a great guest choice! ;)

Subscribe to RSS - programming