Drupal 7
Creating and Updating Nodes Programmatically in Drupal 7
Submitted by dale on August 31, 2010 - 6:52pmThe 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.
Basic Node Creation
The following code assumes an unchanged Drupal 7 Standard installation and will create a Drupal 7 article node.
Drupal 7 Blocks: What's Changed Under the Covers
Submitted by dale on August 17, 2010 - 12:14pmBlocks in Drupal 6 (and 5) always felt a little less loved than other portions of Drupal. While the node and theming APIs offered straightforward and often elegant methods to modify behaviour without modifying other peoples' code, blocks weren't so fortunate. Lacking API support, working with blocks often meant writing ugly code. Last week helping with the Drupal Block API documentation I discovered this has changed in Drupal 7. I thought I'd share what I learned.
The information I'll discuss is a summary of the block information from:
- Converting 6.x modules to 7.x (http://drupal.org/node/224333)
- Converting 6.x modules to 7.x - Categorical (http://drupal.org/node/394070)
- Block API Documentation: block.api.php
- Examples Module: block_examples.module (From: Examples for Developers project)
The Examples for Developers Module
Submitted by dale on July 30, 2010 - 2:21pmAn example, like a picture, is worth 1000 words. Until recently Drupal programming examples existed but were spread among documentation pages, blog posts, and the Drupal CVS repository; some searching required. That changed in late 2009 with the introduction of the Examples for Developers module. Its purpose: "to provide high-quality, well-documented API examples for a broad range of Drupal core functionality". Now you can find high-quality, working Drupal 6 and 7 code examples in one place, many with SimpleTests.
The Examples for Developers Module, or simply Examples Module, is actually a collection of modules. Each sub-module contains a single, specific example of how to use an API or implement a feature. At the time of writing there are 17 examples:
Drupal 7 APIs: A Mindset Change for Scalability
Submitted by dale on October 5, 2009 - 9:57amIn a presentation at the September 2009 meeting of the Vancouver League of Drupaliers (Vancouver's Drupal user group) core Drupal Developer and Now Public Development Team Lead Károly "CHX" Négyesi spoke about the importance of APIs in Drupal 7. If you're serious about scalability APIs are no longer an optional convenience. For some this may not be an issue, for others this could be a mindset change. Károly also discussed some other Drupal 7 improvements like functional testing.
This is a video of his presentation.
Drupal 7 APIs: A Mindset Change for Scalability
This video can also be viewed directly at: Drupal 7 APIs: A Mindset Change for Scalability
