Create an Admin configuration page for custom module on Drupal

This is a follow up to my last post on Creating a Custom Module in Drupal 7. In this post I’m going to show you how to manage your custom module (or front end content) via an admin page.

As you know, Drupal 7 is a truly modular framework. As such the admin interface is a key feature of any Drupal 7 module, and it is tightly integrated with the rest of the site. All administrative functions are easily accessible through a menu that appears at the top of the admin page, when you are logged in as a site administrator. Several functionalities of different modules can be configured as well as enabled or disabled via this administrative interface.

In this tutorial I’ll show you how to list down your front-end content in the admin interface using features like create new content, edit content and delete content. So let’s get started.

Step 1

First, create an administrative hook menu that appears at the top of the page when you are logged in as a site administrator.
Open the books.module file which was created in the previous tutorial and add the below code inside the books_menu function.

 

 

The drupal_get_form() uses the default Drupal 7 form API which will return an array which another built-in function drupal_render(), which will convert into a rendered HTML form.

Note
Make sure you clear the cache whenever you make any changes to a menu item or create a new menu item, since these might not be reflected unless you clear the cache. To clear the cache, go to <your_base_path>/admin/config/development/performance
Here you’ll see the menu items appearing at the top of page. Alternatively, you can access your menu via the below URL
<your_base_path>/admin/books-info

Step 2

Before creating the call back function books_admin_list which is defined in the above menu, first we’ll create a function which retrieves the books information from the database and set the grid header and paging with the table sorter. We’ll be using the Drupal 7 default grid, pager and table sorter here to list down the books.

Note
The only reason we are using the above function is because we want to ensure proper code management. Otherwise you can also perform the below activity using a call back function.

Open your books.module file in the text editor then add the below code and hit Save.

 

Step 3

Next we’ll create a call back function for book listings in the admin interface.

Open the books.module file in the text editor and add the below code and hit Save.

function books_admin_list() {

 

If you’ve followed the steps above correctly, you should be able to see your admin grid with the header, paging and table sorter. The Add, Edit and Delete links won’t be working though, since you need to create menu item for these first, before you can use them.

Step 4

Now we’ll create the add new books form as well as the edit books and delete books forms. We’ll be using a similar form template for both, the add and edit, books forms.

A form is defined using a multi-dimensional array. Each element in this form array has its own key. So $form['title'] is the key for the field. This becomes the name of the element in the final rendered HTML form. The key used for each element must be unique though. Each form element is in itself an array with attributes. The attributes we are defining here are:

#title                                     –              field title

#type                                    -              type of field which Can be any html form field like textarea, textfield or select.

#required                            -              If required is set to TRUE, this field is required and a validation error will occur if the user submits the form.

#default_value                  -              set default value for the field

#rows                                  -              attribute use when field type is textarea

#cols                                    -              attribute use when field type is textarea

 

Next, open your books.module file in the text editor and add the code below and hit Save.

 

And that’s it. Hopefully after reading this blog you’ll be able to create your own custom module along with an administrative interface.

 

The following two tabs change content below.

Umair Butt


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">