In Drupal 7, the concept of module differs a little of what they are in other CMS. While most solutions more or less complete face to the user, in Drupal are used to be more focused to expand tools for programmers or interface administrators.
On this CMS, and because of Panels, Views and Caos Tools modules’, the creation of new functionalities for the visitors are made to mouse without the need of modifying codes.
Sometimes we found with the need of calculations or operations with the stored data on Drupal that I can not do directly with the interface, either we need to “on the back” of the visitors themselves.
The basics of Drupal 7
The most simple module in Drupal 7 consists of two files only, INFO and MODULE. With the first one, we will give information to the CMS about the name, the description, Drupal ‘s version that allows, module’s version, associated files, dependencies with other modules, etc…
However, before we start to write, we should select “machine’s name” for our module. This name must be easily distinguishable of the others, without spaces and without strange characters, since that will use it within the code to associate our response functions with it. We will use for this tutorial the name of “firstmod”.
Another important detail is…
Where will we keep our module?
The fastest answer resides in /sites/all/modules/ but if we have a multidomain Drupal installation you could also go in the folder modules/ of any of them. On this case, we will use the fastest and the most generalised answer.
Once decided on these two important points will move on to create the directory for our module: /sites/all/modules/firstmod/
Creating firstmod.info and firstmod.module files
Then, we will create both files: firstmod.info and firstmod.module.
Let’s see, first of all, the content of firstmod.info:
- name: It is the title of the module in the list.
- description: It is the description to show.
- core: It is the Drupal’s version available. Note that only indicates the major version 7.
- package: It is the group that owns the module. It can be an existing one or a new one.
- dependencies: It indicates the modules that are required for the operation of our own, in this case, put ctools as an example but it is not necessary from beginning.
- configure: It is the URL path of the new module configuration form. It is optional and will require certain indications in the file module to make it work.
Second, it is time to see the content of firstmod.module:
Yes, as we can see, the firstmod.module file could be completely empty, thus creating the module most simple possible…
And the result is the next one:
Before continue, you have to introduce the concept of HOOK.
They are personalisation of actions. Or in another language, Hooks allow us to modify behavior of a method or function without modifying his code, always be prepared.
We have a function that returns a list with the cities of Spain, and which is prepared to use Hooks about their answer. When that functionality is used, before we answer it, invoke all the hooks that “exist” for her. Any of these hooks can take the list of cities and filter them, reducing the response to cities with more than 10000 inhabitants. Where is called the function of cities now beyond will receive the list of cities in Spain with more than 10000 inhabitants and not all the list of cities.
However, at any time, you can disable the filter Hook and when the function is called again will return the full list, all of this without touching the original function.
The configuration form
On the other hand, to add a configuration form to our module we have to create a Hook with hook_menu() of Drupal 7. The way to make this is replacing the word hook_ with the name of our module, standing firstmod_menu(). Drupal 7 will find the hook and make it work when necessary.
Then, we add the following code to firstmod.module:
In this hook we return a list of menu options to be added to the administration panel. The key of the array will be the route to the configuration, the same as we declared in the info file.
- title: will be the title to show in the option. It is on English because Drupal uses this idiom as base and with t() function that translates it to the final idiom.
- page arguments: it indicates which will be the function responsible for declaring the configuration form with the FORM API and “access arguments” indicates the necessary permission to access. If we leave the module as it is now, it can be accessed with the user 1. A special user in Drupal with all the permissions. Also, since we have not declared the permit, we will not be able to give you access either.
Therefore, we will declare the permit using another hook. On this case of hook_permission() adding the next code to firstmod.module:
We can give access to other users. However, we don’t have the configuration form. To create it, we will have to implant the function that we declare in the hook_menu(): ‘firstmod_admin’.
We are going to add again to firstmod.module:
Then, in this hook we take back an array, tunned as a configuration form, with a field called firstmod_something. Almost everywhere we put forward firstmod for assure us not “crash” with other modules in their configurations and methods.
- variable_get(): it returns a global variable in Drupal. Using this function we ensure that the value is available for all requests to the server, regardless of the server that responds (sometimes websites use a lot of servers as it is just only one. We should look PHP documentation about this objective)
Let’s see how it looks.
For this, we activate the module…
After, we move to the configuration dashboard…
To give permission to other users…
This example and guide it serves as a starting point to create our Drupal 7 module . The next step is to see the documentation from drupal.org and look the modules created by others… one of the advantages of the open source es that we can learn from others.
Do you have any doubts? leave your comments or contact with Agilia Center.