Adding variables to the $node object

Open Publishing... - April 17, 2008 - 18:37

Drupal's $node object contains several variables which are very useful in module development. For example $node->name returns the author's name and $node->created returns the time the node was created. In addition all CCK fields become variables of the $node object. So if you were to create a CCK field called "level", its value could be accessed by $node->level.

I'm currently developing my own module and would like to create my own variable for the $node object. The value of the variable will be a value which is stored in the database in a custom table. The table keeps track of nid, so it should be easy to associate a node with its value in this table. But how would I give this value a variable name in the $node object without using CCK?

Custom content type

Davy Van Den Bremt - April 17, 2008 - 20:29

You will have to create a new content type using a module. You can find how to do this for Drupal 6 in the handbook:

http://drupal.org/node/231019

You can do this in Drupal 5 etc. too but I can't find it directly.

There has to be an easier

Open Publishing... - April 18, 2008 - 01:16

There has to be an easier way than that, an easier way than creating a new content-type and implementing all of the functions...right?

I know CCK can add variables to existing nodes (seemingly) very easily. Couldn't I just use a similar method to create my own custom variable (without using CCK)? If nothing else, couldn't I edit some of the core Drupal code?

I think the answer is no

alexis - April 18, 2008 - 11:38

First, you don't create 'variables' for nodes, you create 'fields', which later can be accessed as $node->field, and there are two ways of doing it, as far as I know, with CCK (which is the 'easy' way) and creating your own content type module, as described on the link posted above.

If you want to add the same field to different node types you could also use hook_nodeapi.

It may take a while getting used to the logic but trust me, once you get the idea setting up new content types with your own modules is quick and easy, I prefer this method to using CCK in many cases. There are discussions about pros and cons of each method in the forum, search for them.

Good luck!

Alexis Bellido
Ventanazul: web development and Internet business consulting shouldn't be boring

 
 

Drupal is a registered trademark of Dries Buytaert.