Advise on first module : Package Tracking

codemann - May 16, 2008 - 09:30

I'm about to start creating my first Drupal module, and off course I'm try to think very good how to make it fit perfectly in Drupal. I hope to get a second opinion here before I start working on it, so that I know that I'm using the correct way of thinking.
I've read a lot of the available examples and manuals, so I hope I'm ready :-)

I'm building a tracking module for a shop. When they receive a package, they get a tracking id, a name and an address, but not more info. Someone will input this data, and customers can check through the website the status of their package by looking up the tracking id that they got when they placed their order.
When a customer comes to get the package, we want to stimulate him to get some extra data, like a phone number and an email address.

I'm thinking about creating 2 content types :
1. Customers : name, address, phone, fax, ...
2. Packages : tracking id, status, link to customer

Is creating content types for this the correct way of thinking and is it possible to link a package to a customer? I don't want to use the Drupal user for this yet, because we don't know an email address at this time.

Then, when a user agrees to give his email address I want to create a Drupal user so he will be able to login. His customer info will be attached to the Drupal user and I want to trigger to send the welcome email to him automatically.

This also gives us the possibility to show a history of packages, give him the option to automatically receive emails when packages arrive, and many many more things.

I still have to read up on how to do this all technically, but I want to make sure I'm on the right path. I've used the existing Drupal modules, so I know the Drupal way of thinking, these thaughts are mainly based on that. But I just don't know if I'm thinking correct.

If anyone has any other ideas, suggestions or anything, that would be very much appreciated. Reading about the API gave me already many "wow!" reactions, I'm amazed by the simplicity of writing modules. Keep up the good work!

You are on right track

pembeci - May 17, 2008 - 01:05

You can use Node Relativity module to assign package nodes to a customer node:

The module allows parent-child relationships between nodes to be established, managed and searched. You can restrict the types of nodes that can be parented and the ordinality of parents. These are used to contextually create links to add new child nodes or attach existing ones. There is also a navigation block that uses a node's ancestry to list out breadcrumb-style links.

I used it for a similar purpose and I am happy with the results.

Are you planning using CCK for your new content types or create them at your custom module? Using CCK would be easier if you don't mind the theming and long field names. CCK has a user reference field module, so you may easily attach a customer node to a Drupal user. There are many more CCK modules which will save you a lot of form processing, validation time.

Good to know

codemann - May 19, 2008 - 09:11

Good to know that at least my idea of making the 2 content types was correct, and thanks for your reaction.

My plan was to completely write it myself so I could make it perfectly how I wanted it to be, but you made me curious to at least try to use the existing modules.
So I quickly setup a Drupal 5.x (existing website is also in Drupal 5.x), installed the CCK module with the CCK address module. Great address module by the way, with the extention I even got Belgium included, so I was all happy happy and joy joy.

But then I soon ran into one of the reasons why I wanted to write it myself : that annoying "Title" and "Body" field. I maybe understand the use of these fields wrong, but I don't see how to use them here properly.
The whole address module makes a nice address group, where all those fields are in. So I wanted to make another group "personal info" where I could put the fields name, company name, ... in. The title field exists, so you have to do something with it, so I decided to use it for the "name" of the customer. But the default fields title and body can't be put in a group. So I will always have those 2 fields (title/body) that I can't put in any groups. Or should I use them differently?

Title and Body are required by Drupal

pembeci - May 19, 2008 - 12:47

If I am not mistaken Title and Body are required fields by any content type. So even if you write your own custom content type you should have these. Title is somewhat necessary, there are a lot of node listings which require a common field to display. Body is for some content types not necessary but you can leave it empty and at the node/add form you can hide it (if CCK doesn't allow you this you can use hook_form_alter).

As to your problem, making the title customer's name makes sense. For grouping:

* you can duplicate the name field in personal info, the easiest solution if not pretty
* use a computed field for name where the value will come from the title field
* or you can just leave the name field out of personal info group but at your theming include it in that group.

If you don't have any time considerations, creating the content types at your own module will be good practice. With CCK you can also "make it perfectly how you wanted it to be". You will save a lot of time at form creation, validation, processing etc. but you may spend some more time for theming and other customizations compared to defining your own custom content types. So your choice depends on how much time you have for this project and what you want to learn during the process.

 
 

Drupal is a registered trademark of Dries Buytaert.