Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

build a custom drupal module in this tutorial

Building a custom module part 1

This tutorial is written for new Drupal developers or PHP developers who want to learn Drupal. The following are steps to create a simple sample module ,so you can learn and be inspired to contribute your own.

We are going to do the following in this tutorial:

  • Create a file structure for a module.
  • Create a simple table.
  • Register a path to display the custom form.
  • Create a custom form with 4 fields.
  • Capture and save the form values in the database.
  • Retrieve and re-populate the form with user's input.

Let's get started.

 

Step 1: Create a File Structure

Create the structure for your first module. We are going to call it simple recipe.

Create the following folder and files:

  • sites/all/modules/custom/simple_recipe
  • sites/all/modules/custom/simple_recipe/simple_recipe.info
  • sites/all/modules/custom/simple_recipe/simple_recipe.install
  • sites/all/modules/custom/simple_recipe/simple_recipe.module

Note: You don't have to put our module in the /customs/ directory. That's just used as an example folder name.

 

Step 2: Create Columns for Storage

We are going to need few columns to store our recipe:

    a) A unique auto increment ID to identify the recipe.

    b) An int column for user id, so we know who the author is.

    c) A column to store all the form values, this way, we get the flexibility to add/remove form api fields.

    d) A status field for this record. 1 for active 0 for disabled.

    e) A Unix timestamp for the creation time.

Now that we have that covered, let's declare the schema in the install file.

Please note that with the Drupal platform, there are endless ways to solve the same problem. This is just an example.

We can use node to store the data, however, for demonstrative purposes, we are going to store the whole form_values instead.

  1. simple_recipe.install

 

Enable the module and make sure we have the simple_recipe table in database.

 

Step 3: Register a Path To Display The Custom Form

We are going to need to register a path to create this module. Let's edit the simple_recipe.module and add the following code.

  1. simple_recipe_menu()

 

Now flush cache.

The variable %user in the path will auto load a user object by its id and pass it in our access arguments check function. We will implement the access callback later.

 

Step 4: Create a Custom Form

We are going to have four (4) fields for this form.

  1. Recipe name (text field)
  2. Ingredients (textarea)
  3. Instructions (textarea)
  4. Difficulty (select)

Edit the simple_recipe.module and add the simple_recipe_form() function.

  1. simple_recipe_form()

 

Please pay attention to the form element property '#default_value', this is how we repopulate the form when data is present. For textfield and textarea, clean the content before its outputted for better security.

 

Step 5: Save The Form

At this point, if you click save, the form will be submitted. We will need to add a submit handler to capture and store the data.

Add the following function to simple_recipe.module file.

  1. simple_recipe_form_submit()

 

This function will be called once the form gets submitted.

Now that we have the form, we will call drupal write record to save it.

  1. _simple_recipe_save()

 

Step 6: Re-Populate The Form

We need to load the recipe data from the database, and then extract the data and feed it back to formAPIi.

Add the following function to simple_recipe.module file.

  1. _simple_recipe_prepare_form_values()

 

However, if you have a complicated from with lots of form elements, saving the form values can be quite time consuming. Serialization is our friend. http://en.wikipedia.org/wiki/Serialization

DOWNLOAD the source code here in a zip file for you to check it out.

References here:

 

Do you work at an agency that needs help with a Drupal project? Here at Cheeky Monkey Media we love to help and are available to work in the background for your agency. (We don't need to always be the Alpha monkey.) Call us if you're interested in hiring us as helping hands! We also offer Drupal Support Services for websites that need help, but not necessarily a full redesign.

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us

Comments