Amitav Roy

Blog on web and travel

Drupa 7 module development: Making settings form in drupal using forms api

Posted on 15 Sept 2011 by Amitav Roy

Drupa 7 module development: Making settings form in drupal using forms api

I have this block on my site where I display a featured article. Nothing fancy, its just an article which I would like people to have a look and more importantly the article should have a link on the home page so that Google can crawl it quickly.

But, somehow it is not quite the Drupal way of doing things. So, I decided to have a very simple interface (a Drupal setting page) to set the node id which will be displayed as the featured article. The node-id will be stored in the database and when the page is viewed, the node load will happen with the node id stored in the dB.

Cool, so what are the things that we are going to do:

  1. Create a module (in my case, it is amitav_settings)
  2. Implement hook_menu() for the settings page
  3. Form for the settings page
  4. Store the variables in the variables table in Drupal DB
  5. Use watchdog method to add error messages to the watchdog table.
  6. Use drupal_set_message to show messages to the end user.

NOTE: If you are new to Drupal module development, then I suggest you get the basics right at first. You can get that here: Writing Drupal .info file. Once done you can always refer to this tutorial.

Here is the first part of the code. Here is am implementing drupal hook_menu()

There are two items, one admin/config/amitavroy which is like the landing page for all the settings page. All other settings will be listed inside this. And that is the reason I have implemented system_admin_menu_block_page function in page callback.

The second item is the actual menu item where the form will be generated. In this page, I am using drupal_get_form to generate the form. And the whole form will be passed as an array in the form of argument from the function featured_articles which will return the form element. [This is the typical way of implementing Drupal hook_menu and generate form.]

The function featured_articles() will be searched for in the file “includes/” as per line number 20.

So, now let’s look at the function featured_articles and the other functions which are supporting it.

The function uses a def_node_id variable to store the already set featured article node id. This is important so that when you change the setting, you know what the current setting is.

You will see _form_default_value() function is called which uses variable_get() function to get the current feature article node id.

Then it’s just a simple form element ‘textfield’. Default value of the text field is $def_node_id[‘featured’] which we got using the variable_get() method.

Then we come to the submit_setting_for_site($form, $form_state) function. It calls a function which does bulk of the checking and error reporting jobs. I have used the watchdog function to add a record that the featured article value was changed.

[NOTE: I have used the watchdog only when we get a successful event. You can use it for failure too. But, I have just set an error message when the thing goes wrong.]

The variable set message stores the value and the variable name in the variable table of Drupal which is a huge advantage for us.