How to create a
Drupal 7 Module
Basic steps
Taken from: https://drupal.org/node/1074362
-
Choose a short name for your module
Ours will be "madlibs" but it could be "mad_libs" or "madlib_maker"
- Create a module folder in:
sites/all/modules/madlibs
-
Create a ".module" file in the module folder
Format is "module_name.module" so in our case, "madlibs.module"
<?php
and need only containLet's build a .module file now!
Create info file
Taken from https://drupal.org/node/1075072
-
Create a ".info" file in the module folder
Format is "module_name.info" so in our case, "madlibs.info"
-
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
- User interface
- Follows coding standards
- Disadvantages
- Requires another module
- Adds a line about "features[features_api][] = api:2"
Let's make our module
do something!
Easiest thing -- display some text
- Enable hook_menu to declare a custom page
See hook_menu documentation
Ours needs to be called "madlibs_menu"
- Add the page callback to display the page.
- Also need an access callback so we know who can
see the page. - And a title is nice.
- Don't forget to return something in each function.
Adding the javascript
- Create a subfolder in your module to host your js.
- The JS is located here:
http://www.bennadel.com/blog/623-jQuery-Demo-Mad-Libs-Word-Game.htm - 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 - 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
- Declare the block using hook_block_info
- Tell Drupal how to render the block with hook_block_view
We're using theme_image to render our image. - 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:
- The madlib text is all in the .module file -- not very intuitive
if you want to alter it. - 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
- Create a file "madlibs.tpl.php"
- 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', ), );
- Now we have the ability to call theme('madlib') in our
page callback.
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
- Add hook_block_configure
- Add hook_block_save
We should now have a much better, still functional module.
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.
- 879