Login/Register

For Activists

Where Do I Install Custom Modules

Create a new subdirectory under [base drupal location]/sites/all/modules . Install optional and custom modules in the new subdirectory.

Don't use the "modules" directory that is created with your base Drupal installation. When it comes time to upgrade Drupal itself (e.g. 6.14 to 6.15), you need to be able clearly identify optional and custom modules. Therefore, create a new subdirectory under [base drupal location]/sites/all/modules.

Comments

Great Tip

Hi,
Thanks for the simple, yet very useful tip. On Drupal 6 it's really not obvious that's where you are supposed to install optional modules.

My Profile - Proof of Concept Module

This is another installment of our series on beginning module development for Drupal

We'll build a "proof of concept" module called "My Profile" which demonstrates the following:

My Profile - Object Class Pattern

A simple object pattern for our custom data.

The following listing demonstrates 2 classes:

My Profile - Installation

In this installment we'll focus on the install and un-install routines for our My Profile module. The first article (Part 1) is located here.. Part 1 discusses the data structure, database table and class model (to store and retrieve our custom data).

The following code should be stored in a file listing called "my_profile.install"

. Our module's name is "My Profile".

My Profile - Persisting User Profile Data

In part 1 of this series we showed you how to custom the Drupal account registration form. We now need to make sure that the new data is stored in the database (after a user completes the user registration form).

We implement hook_user()

My Profile - Maintenance Table

This post is part of a series on our proof-of-concept module "My Profile". We are going to detail a maintenance table, presumably for a system administrator. The maintenance table allows an administrator to add, modify and delete records.

Before we get in to the code, let's refresh folks on what "My Profile" does and how it fits in to the rest of our Drupal installation.

My Profile adds a simple text field to each Drupal (website) user account. That text field is called "Alias".

A My Profile Alias is optional for user accounts.

My Profile - Maintenance Forms and Handlers

In our previous post we constructed a maintenance table. That allows our administrators to either add a new alias, modify a current alias, or delete a current alias.

In this post we will detail the edit and delete current alias form and routines.

We will implement hook_form() to display a form that either modifies or deletes a current user alias.

My Profile - Dynamic Ajax Form Processing

In this post we are going to discuss the final method for administering our user alias records. That method is the add new alias method.

Let's review the business requirements briefly.

A user alias is not mandatory. However, a single user account can never have more than one alias. So we have 0:1 (zero to one) relationship between alias and user accounts.

So for our add dialog, that means we need to display only user accounts that do not have a related alias.

My Profile - Populating Forms from the Database

In the prior post we showed you how to build a parent form which performs an asynchronous (AJAX) call to dynamically repaint a child form.

In this post we'll detail the child form.

First we need to create a path (url) which maps to our form. Drupal calls it's request dispatching mechanism "Menu Callbacks". We implemented hook_menu() already, we'll add a new callback definition:

My Profile - Add Configuration Controls

In this post we are going to add a configuration page for the system administrator.

Our "My Profile" project has grown. So far the system is set up so that logically and administrator performs all the work. The design assumption so far has been that only a user granted special permissions can edit or delete "my profile" records. A new user is allowed to enter their "my profile alias" when they create an account. However, we haven't facilitated a user editing their alias at some later date.

My Profile - Modiying the standard My Account/User Profile

In this post we'll modify the standard "My Account" user/profile menu and edit forms. We'll code the routines that allow users to edit their own "my profile alias".

In out previous post we discussed the fact that an administrator can turn this feature on or off. In other words, our code needs to check to see what the current system settings are before we allow a user to edit their alias.

First lets add our menu item. Note, we are appending the standard path "user". The following item is added to our hook_menu() impementation:

Module Development in Drupal 7 - Presentation Outline

What Are Modules
  • Customize, change and extend Drupal business functions.
    • You don't change the distributed Drupal source code. You add computer instructions in a module.

Community Modules versus Custom Modules

  • Community modules are generalized solutions.
  • Use Custom Modules for very specialized, narrowly scoped solutions. Modules are not required to be downloaded from Drupal.org.

Drupal 7 Runtime Requirments

  • PDO  -  Drupal 7 requires PDO enabled in your PHP environment.
    • PDO provides transaction support
    • PDO supports prepared statements
      • Prepared statements are faster (database engine caches the exection plan).
      • Prepared satements are safer, much less vulnerable to sql injection attacks.

Module Requirements

  • ".install" files now support implicit database table installation (and table drop). All you need to do is include schema expression.
  • ".info" files now allow you to declare file listings that are dynamically loaded. You don't need to use a PHP "include" expression for Drupal to find methods and objects provided in the dynamic load files. Dynamic loading allows Drupal to reduce your module's memory footprint (usage).

 Drupal Form API

  • Redirect moved from the $form object to the $form_state object.
  • Drupal 7 adds a set of "ajax" attributes that peform some partial page repaint.
    • drupal_set_message() and form_set_error() invoke full page paint. Therefore, we moved our validation and message notification in to our ajax response.
    • ajax response can be either a:

 Drupal Theme

  • Arguments to theme_table() are now required
    • Current version requires a minimum of 2 rows (a bug).

Drupal's Database Abstraction Layer

  • db_insert() is new to Drupal 7.
    • Closest relative in Drupal 6 is the drupal_write_record() method.
    • Both methods return the database autogen (serial) field values.
    • Db_Insert is a more elegant solution (easier to read, has an implicit return value -- drupal_write_record stores response in an object passed by reference).
  • db_query() in Drupal 7 returns a prepared statement
    • Drupal 6 db_query() returned a database query result object.
  • Method Chaining -- Since PDO is an Object Oriented Library it allows for method chaining. One exression can invoke many methods. Note! You may want to alter how much chaining you are using to control granularity of your exception handling.

 

Drupal 7 Dynamic Class Loading

Drupal 7 introduces dynamic class loading. When you package your modules, you need to supply Druapl with a ".info" file. Inside the ".info" file you now declare an array of file listings. Drupal's new Module Registry inventories the file listings and dynamically loads method and objects as needed.

In our example below, we have have a file listing called "my_color_class.inc". The file listing includes several application objects. Before Drupal 7 we would need to use an "include..." statement in any other file listing before we could use those objects.

my_color.info

Drupal 7 Automatic Database Table Installation

Drupal 7 automatically adds database tables described with hook_schema. In our example below, even though we don't have an implicit hook_install or hook_uninstall method defined, Drupal adds the "my_color" database table when our module is enabled, and drops the "my_color" database table when our module is un-installed.

Drupal7 Form API Ajax Directives

Drupal 7 adds "ajax" attributes to the Form API (FAPI).

In our example below, we've added ajax attributes to our "report" button. When a user "clicks" on the button, we will send a request to a server side handler named "my_color_ajax_response". When the server side handler sends a response, we replace the contents of the html div element "report_table". We use the tag "id" attribute to identify the appropriate target html div tag.

Drupal7 Generate Report

We are using the Drupal7 implementation of theme_table to render our report. As of version alpha 6, theme_table() requires you to include at least 2 data rows (note! may be a bug).

In the example below, we check to see if we have any records to report.

If we don't have any records to report, we notify the user there is no records, create a default display.

If we do have records to display, we add the array elements to our $vars array structure.

Drupal7 Ajax Response Handler

In our hook_form() implementation, we added ajax attributes to a couple of our form elements. The example below is our ajax response handler. In Drupal7 you can use a simple response handler which simply updates a single element (not shown here), or you can send a batch of client side commands. Our example below sends a batch of client side directives.

Drupal 7 Database Abstraction Layer

In example below we demonstrate some of the new Drupal 7 database abstraction layer features.

To add a record we are using db_insert(). "db_insert()" is more elegant than it's closest Drupal 6 counterpart (e.g. drupal_write_record). The method db_insert() returns the database key generated by the database.

Drupal 7 Module Development - Application Screenshots

In our "proof of concept" module, we employ ajax to perform a partial page repaint. The following are screen shots of the "proof of concept" module (my_color). We demonstrate the initial form and report state, invalid data entered and finally valid (success) data entered.

Create a Simple Drupal Module - Use the Form API -- An Introduction

In this diary, we'll construct a simple custom drupal module.

Feel free to post questions in the comment area. Beginners are welcome. In this diary,  we won't assume "you are just supposed to know that". 

The goals of this post is to detail:

Creating A Simple Drupal Module -- Files and Directories

This this diary we are demonstrating how to create a simple Drupal module ( see introduction for more details) .

Our modules name is "My Color".

We will create one directory and three files: