Drupal 7

How to set field default values programatically in Drupal 7

I'd like to share a way to set default values of fields to new nodes (more generally "entities") programatically in Drupal 7.

At the time when Drupal 7 was released, it seems that the APIs of Drupal were not in high demand. So Drupal 7 doesn't provide a full set of APIs to make use of field default values. It uses field default values in a limited case, only when a node creation/edit form is rendered. So there's no sample code in the core which shows how to set default values of fields to nodes programatically.

Libraries included in Drupal 7 core

There're many JavaScript libraries included in the Drupal 7 core. Many of them are rarely used in custom modules (I haven't used many of them) but they're helpful in some situations and it's nice to kwow what are inside the core as baseline knowledge for Drupal development.

Here is a list of the libraries. Each library comprises of a machine name, title, website and description in the following format.

Using OOP styles for Drupal 7: callbacks

Drupal 7 was created mainly with a procedural style but OOP styles can be used in many parts of it.

For example, there're many callbacks used in the Drupal 7 system. Originally they're expected being functions but some callbacks can be replaced to methods. Here are some examples.

Batch callbacks.

We can use methods for batch callbacks. Because the callback is stored in the database with a serialized manner and invoked with call_user_func_array().

Drupal 7 number quizzes

Here are quizzes on magical numbers related with Drupal 7.

How many numbers do you know of them? If you already know all of them, you're an awesome Drupal master certified by hgoto (me)! (But there's no certificate :) )

21 (days)

The default session expiration time. After 21 days (actually 2,000,000 seconds) since the session was created, user sessions expire.

Drupal 7: How to disable a content type

While developing a site with Drupal, at times I'd like to disable a content type without removing it. I just want to disable one.

The database table where content types are stored is node_type. And there's a column disabled in it. We can disabled a content type by changing the column value to 1.

There are APIs for that. We can use node_type_load() and node_type_save() to do it.