Ok, we have all heard it, and many of us have said it when we were starting out on our journey to learn Drupal development, "I didn't know how to make a module... so I just put some PHP code in a basic page." It makes me cringe but hey, if you don't know how to make a module and you are in the middle of a project it can seem daunting.
Well, I am happy to say after you reach the end of this blog post you won't ever have to do this again. Wait, don't run, this is a very short blog post. Making a module is very easy! The source code for this example is available on the Custom Page Example sandbox project. Download the code.
Step 1: Make your module folder
I suggest putting your custom module folders in sites/all/modules/custom and modules you download from Drupal.org in sites/all/modules/contrib (short for contributed). You could put all your folders in just sites/all/modules but I like to be able to tell quickly which modules have been created specifically for a project.
To make your folder you will need to choose the machine name of your module. Basically this is the unique identifier that Drupal will use to tell your module apart from all others. To keep things simple, let's call our module, custompage. So this means you will create the folder sites/all/modules/custom/custompage.
Step 2: Make a .info file
The .info file determines how your module will show up on your modules administration page. It also lets Drupal know which version of Drupal your module is compatible with, in our case Drupal 7.
You will use the same machine name that you created in Step #1 for the file name. So your .info file will be sites/all/modules/custom/custompage/custompage.info.
The .info module file only needs 4 lines:
<code> name = Custom Page<br /> description = A custom page, no PHP in nodes!<br /> package = Current Project<br /> core = 7.x</code>
Here is a screenshot from the module administration page to demonstrate what effect these properties have:
Step 3: Make a .module file
The .module file will contain your actual PHP code. In order for the PHP to run the file must start with "<?php".
After you have created this file you will need a way to tell Drupal to execute some PHP code when the browser is pointed at a particular URL ( or system path). To do this we have to implement hook_menu, a Drupal Hook. You can read more about the Drupal hook system here, but for our purposes it just means we are going to create a specially named function that Drupal will know to call and will let us define systems paths that we will associate with our other PHP functions.
Here is the function:
Look at the comments in the screenshot above or download the sandbox project to see the details of how this function works. For our current purposes it is important to know that this will let the function custompage_output control the output for the system path custom-page (for example http://mysite.com/custom-page) and any user with the "View published content" will be able to access this page.
Now that we have told Drupal the system path that we want to control we have to create the function custompage_output referenced above to actually return back our output. This function couldn't be simpler.
As you can see we are only returning the string "Hello World" here but this is where you could put any PHP Code that you wanted.
Remember this is just a start. You still should not just put all your business logic in page callbacks. If you are interested in getting a quick start in Drupal module development Six Mile Tech offers an Intro to Drupal Module Development course which explains the key concepts of programming for Drupal.