How to create a 

Drupal 7 Module


Basic steps

Taken from: https://drupal.org/node/1074362

  1. Choose a short name for your module

    Ours will be "madlibs" but it could be "mad_libs" or "madlib_maker"
  2. Create a module folder in:
    sites/all/modules/madlibs 
  3. Create a ".module" file in the module folder

    Format is "module_name.module" so in our case, "madlibs.module"
    and need only contain
    <?php

    Let's build a .module file now!

Create info file

Taken from https://drupal.org/node/1075072

  1. Create a ".info" file in the module folder

    Format is "module_name.info" so in our case, "madlibs.info"

  2. The .info file must contain the following information:

    name = Module Name
    description = A description of what your module does.
    core = 7.x 


Let's build a .info file now!




Check to see the new module is in your admin/modules area now.



Or, if you want to avoid all that...

Use Features!

  • Advantages
    1. User interface
    2. Follows coding standards
  • Disadvantages
    1. Requires another module
    2. Adds a line about "features[features_api][] = api:2"


Let's make our module

do something!

Easiest thing -- display some text

  1. Enable hook_menu to declare a custom page
    See hook_menu documentation
    Ours needs to be called "madlibs_menu"

  2. Add the page callback to display the page.
  3. Also need an access callback so we know who can 
    see the page.
  4. And a title is nice.
  5. Don't forget to return something in each function.


Adding the javascript

  1. Create a subfolder in your module to host your js.
  2. The JS is located here:
    http://www.bennadel.com/blog/623-jQuery-Demo-Mad-Libs-Word-Game.htm
  3. Drupalize the JS a bit (we won't go into this much).
    https://raw.github.com/mariacha/madlibs/master/sites/all/modules/madlibs/js/madlibs_maker.js
  4. Use drupal_add_js to load it on our page.
    https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_js/7

Improving the UI

Let's make people feel good about finishing the Madlib with a silly picture in a block.



Creating a block

Taken from https://drupal.org/node/1104464

  1. Declare the block using hook_block_info
  2. Tell Drupal how to render the block with hook_block_view
    We're using theme_image to render our image.
  3. Don't forget to add block as a dependency for your module
 dependencies[] = block


We should now have a working version of the module!

See repo at this state: v1.0


But the code's not very pretty...


What can be improved:

  1. The madlib text is all in the .module file -- not very intuitive
    if you want to alter it.
  2. Same for the block's image -- a themer would need to open
    the .module file to change the image.


Step 1: Theme functions!

We have two choices here for theming:

  • Write a theme_hook function that could be overwritten by
    themers in their template.php file
    -- Disadvantage: lots of html inside php... not really intuitive
  • Add a default .tpl.php file that can be copied and replaced in
    a theme.
    -- This one seems better for us.


Telling Drupal to look for our .tpl.php

  1. Create a file "madlibs.tpl.php"
  2. Declare a new theme function using hook_theme.
    Be sure the array key matches the name of the template!
      $themes = array (
        'madlib' => array(
          'path' => drupal_get_path('module', 'madlibs') .'/templates',
          'template' => 'madlib',
        ),
      ); 
  3. Now we have the ability to call theme('madlib') in our
    page callback.

  4. And theme templates can overwrite this template!


Step 2: Altering the block form

Give users the option of uploading their own funny image

See http://fourkitchens.com/blog/2012/07/18/building-custom-blocks-drupal-7

  1. Add hook_block_configure
  2. Add hook_block_save


We should now have a much better, still functional module.

    See https://github.com/mariacha/madlibs/releases/tag/v1.1



    Find more...

    These slides: https://slid.es/mariafisher/dug-nov-2013

    The project: https://github.com/mariacha/madlibs


    Me: mariacha1 on Drupal

    DUG November 2013

    By Maria Fisher

    DUG November 2013

    How to make a module in D7.

    • 873