Ted Bowman's picture

I recently ported my old module Password Require, which allows password protecting any form in Drupal, to Drupal 7.  This module was originally created for my presentation at the Florida Drupal Camp in 2010, as an introduction to module development.

 

It is an example of how little code you need to add useful functionality to Drupal. But of course after you add an administration UI and try to make it generally easier to use the code starts to add up, not tons but more than a good first module example.

So as a learning tool for those who want to start learning module development for Drupal I have trimmed this module down to 36 lines of code(not counting the comments).

Download the simplified Password Require module.

The video above demonstrates the functionality of the module as it appears on Drupal.org and the video below gives an explanation of how the trimmed down code works.  I have also attached the simplified version of the module to this post.

The module demonstrates the use of hook_form_alter and using a validation function to validate the input of an individual form element.  It consists of only 2 php functions.

As an example of the difference between coding a contrib module for Drupal.org and a custom module for a client, the shortened version of the module is probably closer to what I would make for a client who needed the custom functionality for their site(if the module didn't already exist).  Often a client is not going to want to pay for the time it takes a make more generalized module that will fit other needs beside their own.

Comments

I'm a bit surprised after clicking the title of the post to come here that the 36 lines of code are not anywhere on the page :)

Ted Bowman's picture

Well the code is in the video and the attachment.

Yeah sorry, I didn't initially see the attachment.

I skip videos due to the time it takes to get the info compared to reading :)

Ted Bowman's picture

Thank for the feedback. I have added link to the zip file in the middle of the article.

What is the purpose of the limit? Limit pwd guesses? If so, there's a small error in the validator, where user_authenticate is checked before the number of attempts and the user is not logged out, but _redirected_ to the logout page. Such a redirect doesn't have to be followed by the client.

Best to partiallly copy user_logout into your module, then call that.

Add new comment

Online Trainings

Six Mile Tech offers online trainings via Skype, Google Hangout, or a screen sharing technology of your choice. These trainings can be used for generalized Drupal trainings or to tackle a specific problem for a site you are working on.