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.

Building a custom module part 2

Building a custom module part 2

This tutorial is written for new Drupal developers or PHP developers who want to learn drupal. You can find the part 1 of the tutorial here: Drupal Tutorial Part 1

Last time, we created a simple recipe module with save and load functionality. The user interface is not very friendly yet, and users have to enter a recipe id in the URL to load it.

Today, we are going to improve the usability of the module by adding some UI element to it. By the end of the tutorial, you will be able to add, and list recipes. Editing, deleting, enable/disable recipes will be covered in the next tutorial.

This is what we will do in this Drupal tutorial:

  1. Use theme function to create a table
  2. Use Devel tools

 

Step 1: Install the Devel Module

First, let's get the devel module to help us out.

Install the devel module by the following drush command:

  1. drush en devel -y

You can also download the module manually from Drupal.

If you install it manually, make sure you enable the devel module after the installation, otherwise you will get a white screen by calling an undefined function.

 

Step 2: Get the Data from the Database

Modify the simple_recipe.module file located at sites/all/modules/custom/simple_recipe

To add a new function, simple_recipe_get_recipes($uid, $status), we are going to have two parameters for this; A drupal user id, and receipt status.

  1. /**
  2. * Load recipes from given user.
  3. *
  4. * @param int $uid
  5. * User id.
  6. */
  7. function simple_recipe_get_recipes($uid, $status = 2) {
  8. $query = db_select('simple_recipe', 'sr')
  9. ->fields('sr')
  10. ->condition('sr.uid', $uid);
  11. // Decide what to load.
  12. switch($status) {
  13. case 0:
  14. // Disabled recipes.
  15. $query->condition('sr.status', 0);
  16. break;
  17. case 1:
  18. // Enabled recipes.
  19. $query->condition('sr.status', 1);
  20. break;
  21. default:
  22. // Load every recipes.
  23. break;
  24. }
  25. $result = $query->execute()
  26. ->fetchAll();
  27. return $result;
  28. }

 

Step 3: Register a Path for the Recipe Listing Page

Now we are going to create a new page to list all the data. We will need to register a path for this page, so let's modify the simple_recipe_menu(),

  1. /**
  2. * Implements hook_menu().
  3. */
  4. function simple_recipe_menu() {
  5. $items = array();
  6. $items['user/%user/simple_recipe'] = array(
  7. 'title' => 'Simple Recipe',
  8. 'description' => 'The recipe form',
  9. 'page callback' => '_simple_recipe_list_recipes',
  10. 'page arguments' => array(1),
  11. 'access callback' => '_simple_recipe_access_check',
  12. 'access arguments' => array(1),
  13. 'type' => MENU_LOCAL_TASK,
  14. );
  15. $items['user/%user/simple_recipe/add'] = array(
  16. 'title' => 'Simple Recipe',
  17. 'description' => 'The recipe form',
  18. 'page callback' => 'drupal_get_form',
  19. 'page arguments' => array('simple_recipe_form'),
  20. 'access callback' => '_simple_recipe_access_check',
  21. 'access arguments' => array(1),
  22. 'type' => MENU_LOCAL_TASK,
  23. );
  24. return $items;
  25. }

As you can see, we are going to use the user/%user/simple_recipe path for listing all the recipes. user/%user/simple_recipe/add for inserting a new recipe.

 

Step 4: Populate the Recipe Listing Page

It’s now time to add another function _simple_recipe_list_recipes()

  1. /**
  2. * List all the recipes from a given user.
  3. * @param User Obj $account
  4. * @return string
  5. */
  6. function _simple_recipe_list_recipes($account) {
  7. $recipes = simple_recipe_get_recipes($account->uid);
  8. dsm($recipes);
  9. return ' ';
  10. }

Note:

dsm() is a function from the devel module, you pass a variable that you want to inspect as the argument. It will generate a human-friendly version of the variable for you.

Now, let's visit user/1/simple_recipe

If you(admin) have previously entered any recipes,  you should be able to see them in the krumo output.

Now that we have the recipes loaded, it’s time to create a table to hold them.

  • create an array to hold table’s header
  • create an array to hold table’s row
  • loop through each recipe and fill rows array
  • call theme function to create the html table

The _simple_recipe_list_recipes function should looks similar to this one:

  1. /**
  2. * List all the recipes from a given user.
  3. * @param User Obj $account
  4. * @return string
  5. */
  6. function _simple_recipe_list_recipes($account) {
  7. // Lets change the page title.
  8. drupal_set_title(t('Recipes'));
  9. // Declare table header.
  10. $header = array(
  11. t('Name'),
  12. t('Date Saved'),
  13. t('Options'),
  14. );
  15. // Declare table rows.
  16. $rows = array();
  17. // Load all saved recipes.
  18. $recipes = simple_recipe_get_recipes($account->uid);
  19. // Added each recipe as a table row.
  20. foreach ($recipes as $recipe) {
  21. $operation = $recipe->status ? 'Disable' : 'Enable';
  22. // Unserialize the data, so we can actually get its value.
  23. $recipe_values = unserialize($recipe->form_values);
  24. // Add a table row.
  25. $rows[] = array(
  26. l(t($recipe_values['name']), "user/$account->uid/simple_recipe/edit/$recipe->rid"),
  27. format_date($recipe->timestamp, 'medium'),
  28. l(t('Edit'), "user/$account->uid/simple_recipe/edit/$recipe->rid") . ' | ' .
  29. l(t('Delete'), "user/$account->uid/simple_recipe/delete/$recipe->rid") . ' | ' .
  30. l(t($operation), "user/$account->uid/simple_recipe/statue/$recipe->rid"),
  31. );
  32. }
  33. // Create a button to create recipe.
  34. $create_btn = l(t('Create Recipe'), "user/$account->uid/simple_recipe/add", array("attributes" => array("class" => array('button'))));
  35. if (!empty($rows)) {
  36. return $create_btn . theme('table', array('header' => $header, 'rows' => $rows));
  37. }
  38. else {
  39. return '
  40. ' . t('You don\'t have any recipe saved yet.') . '
  41. ' . $create_btn . '
  42. '; } }

Your end result should look like this:

Custom Recipe Module

That’s all for this tutorial, I hope you enjoy reading it. You can find part three of the tutorial here.

Find the source code here:

Download Source Code

Need help with a Drupal project? Did you know that Cheeky Monkey Media works with other agencies that need extra hands on projects? Our Drupal Web Developers are ready to help. Call us!

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