33
Drupal 6 Web Application Development Tutorial Table of Contents Welcome to the world of Drupal !!!........................................................................................ 2 Installation ............................................................................................................................ 3 Drupal’s Model of a Web Application ..................................................................................... 8 Application Development .................................................................................................... 11 Library System Example ....................................................................................................... 12 Going Further ...................................................................................................................... 33 Document Version Control Author Date Comments Gokulmuthu 24-Aug-2012 First published version Gokulmuthu 28-Aug-2012 Added more features to the example. Gokulmuthu Narayanaswamy http://www.practicalphilosophy.in/drupal

Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial

Table of Contents

Welcome to the world of Drupal !!! ........................................................................................ 2

Installation ............................................................................................................................ 3

Drupal’s Model of a Web Application ..................................................................................... 8

Application Development .................................................................................................... 11

Library System Example ....................................................................................................... 12

Going Further ...................................................................................................................... 33

Document Version Control

Author Date Comments

Gokulmuthu 24-Aug-2012 First published version

Gokulmuthu 28-Aug-2012 Added more features to the example.

Gokulmuthu Narayanaswamy

http://www.practicalphilosophy.in/drupal

Page 2: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 2 of 33

Welcome to the world of Drupal !!! Gone are the days when you need to write tons and tons of code to develop web applications. With the

advent of Drupal, almost the entire application development can be done without writing a single line of

code. If you are looking towards using Drupal merely as a CMS, then this is tutorial is not for you. If you

are planning to use Drupal to develop a web application, or if you want to just try to get a taste of

Drupal based web application development, then you can use this tutorial to kickstart your

development.

The strength and weakness of Drupal is that the development environment is built piece by piece by the

community around the core. So to be able to get a good feature set, you need to download quite a

number of modules and install into the Drupal framework. To make this step easy, I have already

collected a good collection of modules that I have used in my applications and packaged them. You can

download my Drupal Application Development Base and other software to run Drupal from my Drupal

site at http://www.practicalphilosophy.in/drupal. If you are following through this tutorial, you can

download the components as and when they are mentioned.

Versions As of when I am writing this tutorial, the most popular Drupal version is 6. Drupal 5 started the

revolution. Though there are a number of sites still running Drupal 5, for all new development, Drupal 6

is recommended. Drupal 7 is coming up well. Still there are not as much contributed modules for Drupal

7 as for Drupal 6. So, I have decided to stick to Drupal 6 for now.

Caution This tutorial is only for starting off learning Drupal. So the passwords mentioned are very trivial. You

should use much stronger passwords in your final deployment. Also, as this tutorial is designed for

people to try out Drupal, I have assumed that you are doing this on Windows machine. Finally you may

want to deploy it on a Linux machine or on a cloud service. It is very easy to move a Drupal installation

from one server to another, provided the versions of Apache, PHP and MySQL used are compatible.

Please use this tutorial at your own risk. I am not responsible for any damage caused by your following

or not following any steps mentioned in this tutorial. I am not obliged to support you if you get stuck.

Please use the Drupal support forums for help or seek professional assistance.

Page 3: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 3 of 33

Installation

Dependencies Drupal needs PHP and MySql. PHP needs to be hosted on a HTTP server like Apache or Lighttpd. If you

are developing on a Linux machine, most probably the entire development environment is present. If

you want to try it out on a Windows machine, which I assume you are doing it here, WAMP is an

excellent choice of platform. You can download WAMP here.

http://download.cnet.com/WampServer/3000-10248_4-10797035.html This 2.2e version of WAMP

comes with Apache 2.2.21, Php 5.3.10 and Mysql 5.5.20, which are suitable for running Drupal 6.

Installing WAMP server Download and install the WAMP server on your Windows machine. It is possible that there is already

another HTTP server running on your machine. To be on the safer side, let us change the port to run

HTTP server.

To do this, click on the SysTray icon and select Apache > httpd.conf. This will open the HTTP server

configuration file in a text editor. Find the line which says Listen 80. Change the 80 to 8080, save the file

and click on the SysTray icon and select Restart All Services. (There are more efficient ways to do this.

But let us keep things simple for now.) If the icon is still not green, possibly the port 8080 is also being

used. Try some other port number like 9090. This value can be between 1024 and 65535. When the icon

is green, if you open http://localhost:8080/ (where 8080 is the number that you entered in the

httpd.conf) in a web browser, you should see the home page of the local WAMP server.

Installing Drupal Download the Drupal 6 Application Development Base from http://www.practicalphilosophy.in/drupal.

Note that some modules in this version are slightly enhanced to extend their features. Extract the zip file

in the WAMP server’s www directory. To open this directory, click on the SysTray icon and select www

directory. You should have a directory named drupal6, which contains folders and files like below.

Page 4: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 4 of 33

Download the Drupal 6 Application Development Base database from

http://www.practicalphilosophy.in/drupal. You will get a file named drupal6.mysql_.zip. Rename it to

drupal6.mysql.zip.

Open the home page of the local WAMP server. Under the heading Tools, you can find a link to

phpmyadmin. Click on that. The phpMyAdmin page will open. At the center, you can find a link called

Users.

Page 5: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 5 of 33

Click on that. In the next screen, you can find a link called Add user.

In the pop up dialog, enter drupal6 as the User name. Select Local as the Host. Enter password as

drupal6 in both the password fields. Select Create database with same name and grant all privileges.

Click on Add user below the form.

You can find that database is created. Click on drupal6 in the list of databases on the left.

Page 6: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 6 of 33

Click on the link Import.

Use the Browse button to select the file drupal6.mysql.zip that you had downloaded. Click on the button

Go at the bottom. Wait for some time. When the database is imported, it will show Import has been

successfully finished.

Edit the file drupal6\sites\default\settings.php in the www directory. Make sure that the $db_url is set

like this:

$db_url = 'mysql://drupal6:drupal6@localhost/drupal6';

Page 7: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 7 of 33

Make sure that the $base_url is set like this:

$base_url = 'http://localhost:8080/drupal6'; // NO trailing slash!

If you are running the HTTP server on a port different from 8080, you need to mention that number

instead of 8080.

Save the file and close the editor.

Now, open the home page of the local WAMP server. Under the heading Your Projects, you should see

drupal6. Click on that. You should see the local Drupal 6 Development Base website.

This is the start of your unforgettable journey.

Be ready to be amazed!!!

Page 8: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 8 of 33

Drupal’s Model of a Web Application Before we start developing a web application, we need to understand Drupal’s model of a web

application. Here, you can find the vocabulary and the model introduced. (These are based on Drupal 6.

Drupal 7 has a different vocabulary.)

Role There would be several people using the application. Each person would want to do several things with

the application. For example, a library system would be used by the stock manager, librarian and

member. An online test system would be used by the question bank manager, examinee and examiner.

A project management system would be used by the project manager, software developer and test

engineer. Each type of people who use the application is called a role. The role of the user who has not

logged into the application is called anonymous user. The general role of all users who have logged in to

the application is called authenticated user. All other roles are various types of authenticated users.

User Users can be allowed to register themselves or can be allowed to be created only by some role. If users

are allowed to register themselves, approval can be enforced or can be automatically approved based

on email validation. Users can be allowed to change their password, email address and login name.

Content type The application would manage several types of contents. Instances of these content types can be

created, updated, retrieved (or viewed) and deleted (CURD). An instance of a content type is called

content or a node. For example, a library system would manage books, reservations, new book requests

and delivery requests. An online test system would manage courses, questions, question sets and tests.

A project management system would manage projects, requirements, test cases, releases, defects and

documents.

Basic information like creation date, user who created the content and last updated date is maintained

automatically for every content. Optionally, content revisions can be maintained to keep track of

changes to the content.

Field A content type would have several fields. Fields can be of types like integer, floating point, date, single

line text, multiline text, reference to a user of a particular role, reference to a content of a particular

type, email address and image. The fields can have single or multiple values. They may be mandatory or

optional. Depending on the field type, they can be presented for entry or editing in several ways. For

example, an integer field can be presented for entry or editing as a simple entry box, as a selection list or

as a series of radio buttons.

By default, every content has a mandatory field called Title. This can be auto-generated if required. This

field is displayed when showing this content in a list. This is usually shown as a link to a page with all the

fields in the content.

Page 9: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 9 of 33

Fields can also be automatically computed based on the values of other fields or by a PHP code snippet.

Also, though Drupal allows using the same field in multiple contents, it makes the database queries less

efficient in Drupal 6. It is advisable not to use existing fields in contents.

View The application would consist of various lists. Lists are implemented using Views. For example, a library

system would involve list of books rented by a member, list of delivery requests for an area and list of

members whose subscription would expire in the next 15 days. An online test system would involve list

of examinees for a course, list of questions for a course, list of questions for a test, list of answers by an

examinee in a test, list of marks of an examinee in all tests of a course and list of marks of an examinee

in all courses. A project management system would involve list of requirements of a project, list of test

cases for a requirement, list of defects in a release and list of releases of a project.

Permissions In an application, each content type would be allowed to be created, updated, retrieved or deleted only

by specific roles. The person who created a particular content is called the author of the content. The

permissions to update, retrieve and delete operations would apply at two levels – own or any. Some

roles would be allowed to update only the contents of which the person trying to update is the author

(own content). Some roles would be allowed to update the contents which were not created by them

also (any content).

Permissions can also be applied at field level. For example, even if a role is allowed to view all content of

a content type, some fields in the content type can be denied permission to be viewed. Permissions can

also be transitive. For example, if a content type has a field which is a reference to another content,

permissions can be transferred like, “anyone who is able to edit the referenced content can view this

content”. Or, for example, if a content type has a field which is a reference to a user, permissions can be

granted to the user who is referenced.

Rules Rules are actions to be taken upon various events. The events can be like creation of a content, updation

of a content, deletion of a content, change of a field in a content and setting the value of a field in a

content to a particular value. Actions can be like sending mail to some people, changing permission of a

content and setting the field of a content to particular value.

Pages and blocks In any page, there would be a primary node, form or view shown. In addition to that, there can be

several blocks with nodes, forms and views shown in each of them. Depending on the theme, blocks can

be presented in the header, left column, right column, content (center column) or footer. The first page

that is shown when a user logs in is called the front page.

Page 10: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 10 of 33

Menu and navigation You can show a block of navigation tree at the left or right column to help the user find the way to do

what he wants to do. You can also show a list of links at the top right. The links shown at the top right

are usually called Primary links.

Page 11: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 11 of 33

Application Development Before you start developing your web application, you should have a clear picture of the roles, content

types, fields, views and permissions in your application. You can do a lot of tuning easily as you develop

the application. But it helps to think on these lines well before starting to develop. Then you can add the

rules, blocks and menus. Then you can tune the look and feel of your site for various roles.

Drupal allows integrating user login with various systems like OpenID (users can login using their Yahoo

id and password), LDAP (users can login using existing login on a Windows or Linux server), etc. There

are modules that can be installed for other login systems.

Drupal has modules to do e-commerce. There are also modules and features which implement a lot of

functionality like shopping carts, project management, defect tracking, etc. Those are not included in

this development base. You can install later if needed. Once you are familiar with the basic application

development, you can search in the Drupal website for various contributed modules.

The login which is allowed to do the development and management of the website is called the

administrator. In the system that you have installed in this tutorial, the administrator account has login

as admin and password as admin. The top black bar has drop-down links to almost all development and

administration actions.

To continually test the application as you develop, you will have to login as people of different roles. So

it is better to have different browsers installed on your system. You can do the development from one

browser and do the testing from another browser.

Page 12: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 12 of 33

Library System Example

Design Let us design a library system. We will first create the basic features and then add more. We can create

several applications that will interact on the same website. So, the keys should have the name of the

application so that they do not clash.

Roles

Role name Key Capabilities

Unauthorized user unauthorized user Can browse books

Member library_member Can browse books Can view history of borrowed books

Librarian library_librarian Can browse books Can issue books

Stock manager library_stk_mgr Can create/edit/browse books

Users

Users can be created only by the librarian.

Content types

Content type name Key Description

Library book library_book A book in the library

Book issue library_issue An instance of an issue of a book

Fields

Here we design the fields for each content type.

Book

Field name Key Type Multiple values?

Mandatory? Default value

Description

Title title Text Not applicable

Yes None Title of the book

Author book_author Text Yes Yes None Author(s) of the book

Id book_id Serial number

No Yes Auto generated serial number

Unique number of the book

Status book_status Integer from a list

No Yes 0-Available 0-Available 1-In circulation 2-Under repair 3-Lost 4-Removed

Page 13: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 13 of 33

Genre book_genre Text from a list

Yes Yes None General Fiction Non-fiction Self-help Children Adventure Travel Hobby Activity Sports Classic

Book issue

Field name Key Type Multiple values?

Mandatory? Default value

Description

Title title Text Not applicable

Yes Auto generated based on date and book id

Book issue_book Node reference to book

No Yes None Book being issued

Member issue_member User reference to member

No Yes None Member to whom the book is being issued to

Status issue_status Integer from a list

No Yes 0-Issued 0-Issued 1-Returned 2-Lost

Date of return

issue_return Date No No None Date of return, if applicable

Views

View Key Arguments Description

List of books all_books None List of all books with filters

Book borrow history all_ issues Member List of books borrowed with filters

Permissions

Unauthorized user Member Librarian Stock manager

Page 14: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 14 of 33

Create book No No No Yes

Edit book No No No Yes

Delete book No No No No

View book Yes Yes Yes Yes

Create issue No No Yes No

Edit issue No No Yes No

Delete issue No No No No

View issue No Where member is the borrower

Yes No

Rules

When we issue a book, we should change its status to “In circulation”. When a book is returned, the

status should be changed back to “Available”.

Pages and blocks

Unauthorized users should see the list of books.

When member logs in, the issue history should be displayed in reverse chronological order.

When librarian logs in, the issue form should be displayed.

When stock manager logs in, the book add form should be displayed.

Menu and navigation

Extra menus at the top:

Unauthorized user Member Librarian Stock manager

Book list No Yes Yes Yes

Issue history No Yes Yes No

Create book No No No Yes

Issue book No No Yes No

Manage users No No Yes No

Implementation Once you have the design of the application done, implementation is merely transferring it on to the

system.

On one browser, login as admin with password as admin. On another browser we need to login as

different roles for testing.

Roles

We need to create the roles. Select User management > Roles from the admin bar on the top. The two

default roles are already present. Create the other roles - library_member, library_librarian and

library_stk_mgr.

Page 15: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 15 of 33

Users

We need to give librarian the permission to create users. Select User management > Permissions from

the admin bar. This is the master table for permissions. Scroll to the bottom. Under user module, you

can find the various actions on users. Select the check box for administer users for librarian. Click Save

permissions at the bottom of the page.

This is the time to create some test users. Select User management > Users > Add user from the admin

bar. Create test users as below:

Username Email address Password Roles

m1 [email protected] 123 library_member

m2 [email protected] 123 library_member

l1 [email protected] 123 library_librarian

s1 [email protected] 123 library_stk_mgr

Click on the List tab or select User management > Users > List from the admin bar to see the list of

users.

On the other browser, login as l1. Open the URL http://localhost:8080/drupal6/?q=admin/user/user/list.

You should see the user list. Logout and login as m1. Open the URL

http://localhost:8080/drupal6/?q=admin/user/user/list. You should see Access denied.

Content types and fields

Next we need to create our content types with the fields that we designed.

Select Content management > Content types > Add content type from the admin bar. Enter the details

as:

Name: Library book

Type: library_book

Description: A book in the library

Click on Submission form settings. As we do not want a body for the book, remove the text Body from

the Body field label.

Click on Save content type at the bottom of the form.

Select Content management > Content types > Edit Library book > Manage fields from the admin bar.

Enter the details as below and click Save.

Page 16: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 16 of 33

In the Help text, type in Please enter the full name of one author of the book. Check the box Required.

Change the Number of values to Unlimited. Click on Save field settings at the bottom of the page.

Enter the details as below and click Save.

Enter the details as below and click Save.

In the Help text, type in Current status of the book. Check the box Required. In the Allowed values list,

enter as below:

Page 17: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 17 of 33

Click on Save field settings at the bottom of the page.

To set the default value, click on the configure link of the Status field. Click on Default value. Select

Available.

Click on Save field settings at the bottom of the page.

Enter the details as below and click Save.

In the Help text, type in Genre of the book. Check the box Required. Change the Number of values to

Unlimited. In the Allowed values list, enter as below:

Click on Save field settings at the bottom of the page.

Select Content management > Content types > Add content type from the admin bar. Enter the details

as:

Name: Book issue

Type: library_issue

Description: An issue of a book in the library

Page 18: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 18 of 33

Click on Submission form settings. As we do not want a body for the book, remove the text Body from

the Body field label.

Click on Automatic title generation. Select Automatically generate the title and hide the title field.

Leave the Pattern for the title blank. We need to come back after creating the fields.

Click on Save content type at the bottom of the form.

Select Content management > Content types > Edit Book issue > Manage fields from the admin bar.

Enter the details as below and click Save.

Check the boxes Create Book and Edit Book, regardless of the node author, because we do not want to

allow editing this field after issuing a book. Check the box Required. Select Library book as the Content

types that can be referenced as shown below.

Page 19: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 19 of 33

Click on Save field settings at the bottom of the page.

Enter the details as below and click Save.

Check the boxes Create Member and Edit Member, regardless of the node author. Check the box

Required. Select library_member as the User roles that can be referenced. Select Active users as the

User status that can be referenced. Click on Save field settings at the bottom of the page.

Create the Status field of issue as an integer that will take values and default value as mentioned in the

design.

Enter the details as below and click Save.

Change the Input format to something that is closer to the convention that is used in your country. In

the Help text, type in To be filled when the book is returned. Change the Granularity to have only year,

month and day. Change Time zone handling to No time zone conversion. Click on Save field settings at

the bottom of the page.

Now select Content management > Content types > Edit Book issue > Manage fields from the admin

bar. Select Automatic title generation. Type in [field_issue_member-name]-[yyyy]-[mm]-[dd] in the

Pattern for title . If you click on the Replacement patterns link, you can see the list of patterns allowed.

Click on Save content type at the bottom of the page.

Permissions

Next we need to set the permissions. Select User management > Permissions from the admin bar. Scroll

down to the section node module. Set the permissions for library_librarian to create library_issue

content and for library_stk_mgr to create library_book content.

Also, give permission to libray_librarian to create field_issue_book and create field_issue_member.

The other permissions can be set in the content type pages. Scroll down to the bottom of the page and

click on Save permissions.

Now select Content management > Content types > Edit Library book > Access control from the admin

bar. Set the access permissions as below:

Page 20: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 20 of 33

Click on Submit at the bottom of the page.

Now select Content management > Content types > Edit Book issue > Access control from the admin

bar. Set the access permissions as below:

Click on Submit at the bottom of the page.

Now select Content management > Content types > Edit Book issue > Manage fields > Member from

the admin bar. In Node access user reference, under Grants for referenced users, enable View.

Click on Save field settings at the bottom of the page.

Page 21: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 21 of 33

Menu and navigation

Let us now create the menu links for the actions possible so far. Select Site building > Menus > List

menus from the admin bar. Click on Primary links.

First let us add a link for library_stk_mgr to create a book. Select Add item tab. On the admin bar, move

the mouse over Content management > Create content > Library book. Right click and copy the link

address. Paste it in the Path entry field. Remove all the text before node. Type in Create book in the

Menu link title. In the Description, type in Add a new book to the library.

Click on Restrict item visibility. Select only library_stk_mgr.

Click on Save at the bottom of the page.

Similarly, add a menu link to issue a book for the librarian. You can enter details like this:

Page 22: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 22 of 33

Set permissions like this:

Click on Save at the bottom of the page.

Now we need to rearrange the order of the links. Drag the menu items to order them like this:

Click on Save configuration at the bottom of the page.

Page 23: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 23 of 33

Test

Now that we have created the content types, created some menu links and set permissions, login in the

second browser as m1, l1 and s1, and see how the menu items appear for each login.

Login as s1. Create these 5 books using the Create book link on the top.

Title Author(s) Genre(s)

The Alchemist Paulo Coelho Adventure

Tuesdays with Morrie Mitch Albom Non-fiction

7 Habits of Highly Effective People Stephen R. Covey Non-fiction, Self help

The One Minute Manager Ken Blanchard , Spencer Johnson

Non-fiction, Self help

Who Moved My Cheese? Spencer Johnson Non-fiction, Self help

Login as l1. Issue these 3 books using the Issue book link on the top.

Title Member

The Alchemist m1

Tuesdays with Morrie m2

7 Habits of Highly Effective People m2

Views

Now, let us create the lists to show to various users.

Select Site building > Views > List from the admin bar. Click the Add tab. Enter View name as all_books.

Enter View description as List of books. Click on Next at the bottom of the page.

This shows the Views configuration page. Click on the + sign next to Filters. Select Node as the Groups.

Select Node: Type and click on Add. Select the Operator as Is one of and the Node type as Library book.

Click Update.

Click on the + sign next to Fields. Select the following fields:

Content: Author (field_book_author)

Content: Genre (field_book_genre)

Content: Id (field_book_id)

Content: Status (field_book_status)

Node: Title

Click on Add. When asks details for each field, just scroll down and click on Update.

Click on the link Unformatted against the Style: in the Basic settings. Select Table as the style and click

on Update. Select Title as the Default sort and click on Update. Now you can see the table in the

preview.

Page 24: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 24 of 33

Click on the double arrow next to Fields. Rearrange the order as below:

Click on Update.

In the Basic settings, change the Use pager to Full pager. Click on Update.

Click on Save.

Keeping the type of view below the Defaults as Page, click on Add display. Keeping the Page tab

selected at the left, click on the link None next to Path: in the Page settings. Type in all_books in the

URL entry box. Click on Update. Click on the link No menu next to Menu: in the Page settings. Enter the

details as below.

Click on Update. Click on Save.

Select Site building > Menus > List menus in the admin bar. Select Primary links. Drag the Books menu

item to the top. Click on Save configuration. You can see the Book menu item at the top of the page.

Click on that. You can see the book list displayed.

Page 25: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 25 of 33

Select Site building > Views > List from the admin bar. Click the Add tab. Enter View name as all_issues.

Enter View description as List of books issued. Click on Next at the bottom of the page.

This shows the Views configuration page. Click on the + sign next to Filters. Select Node as the Groups.

Select Node: Type and click on Add. Select the Operator as Is one of and the Node type as Book issue.

Click Update.

Click on the + sign next to Fields. Select the following fields:

Content: Book (field_issue_book)

Content: Date of return (field_issue_return)

Content: Member (field_issue_member)

Content: Status (field_issue_status)

Node: Post date

Click on Add. When asks details for each field, just scroll down and click on Update.

Click on the link Unformatted against the Style: in the Basic settings. Select Table as the style and click

on Update. Select Post date as the Default sort, change the Default sort order to Descending and click

on Update. Now you can see the table in the preview.

Click on the double arrow next to Fields. Rearrange the order as below:

Click on Update.

In Fields, click on the link Node: Post date. Change the Label to Issued date. Click on Update.

In the Basic settings, change the Use pager to Full pager. Click on Update.

Click on Save.

Keeping the type of view below the Defaults as Page, click on Add display. Keeping the Page tab

selected at the left, click on the link None next to Path: in the Page settings. Type in all_issues in the

URL entry box. Click on Update. Click on the link No menu next to Menu: in the Page settings. Enter the

Page 26: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 26 of 33

Type as Normal menu entry, Title as Issued books, Description as List of all book issues. Click on

Update. Click on Save.

Select Site building > Menus > List menus in the admin bar. Select Primary links. Drag the Issued books

menu item to below the Books menu item. Click on Save configuration. Click on edit link on the row of

Issued books. Click on Restrict item visibility. Select only library_librarian. Click on Save.

Select Site building > Views > List from the admin bar. Click on the Edit link on the row of all_issues.

Keeping the type of view below the Defaults as Page, click on Add display. Keeping the Page tab

selected at the left, click on the link None next to Path: in the Page settings. Type in my_issues in the

URL entry box. Click on Update. Click on the link No menu next to Menu: in the Page settings. Enter the

Type as Normal menu entry, Title as Issue history, Description as History of all my book issues. Click on

Update. Click on Save.

Click on the second Page in the vertical tabs in the left. Ensure that the Path is displayed as my_issues.

Click on Content: Member in the Fields. Scroll down and click on Remove. Click on Save.

Select Site building > Menus > List menus in the admin bar. Select Primary links. Drag the Issue history

menu item to below the Books menu item. Click on Save configuration. Click on edit link on the row of

Issued books. Click on Restrict item visibility. Select only library_member. Click on Save.

Test

Login as m1. Click on Issue history at the top. Only the book issued to m1 should be displayed. Login as

m2. Click on Issue history at the top. Only the books issued to m2 should be displayed. Login as l1. Click

on Issued books on the top. All the books issued should be displayed.

Rules

If you had noticed, the status of all the books are shown as Available, even though we have issued some

books. When we issue a book, we should change the status of the book to “In circulation”. Similarly,

when the book is returned, we should change the status of the book back to “Available”. Is the book is

lost, we should change the status of the book to lost.

Select Rules > Triggered rules > Add a new rule in the admin bar. Set the Label as Change status of book

during issue. Select Event as After saving new content. Click on Save changes. Click on Add a condition.

Select the condition as Content has type. Click on Next. Select Content types as Book issue. Click on

Save. Click on Add an action. Select the action as Load a referenced node. Click on Next. Select the Field

as field_issue_book. Change the Label as Issued book. Change the Machine readable variable name as

issued_book. Click on Save. Click on Add an action. Select the action as Populate a field. Click on Next.

Select Content as Issued book. Select the field as field_book_status. Click on Continue. Change the

Status to In circulation. Click on Save.

Select Rules > Triggered rules > Add a new rule in the admin bar. Set the Label as Change status of book

during return. Select Event as After updating existing content. Click on Save changes. Click on Add a

condition. Select the condition as Content has type. Click on Next. Select Content types as Book issue.

Page 27: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 27 of 33

Click on Save. Click on Add a condition. Select the condition as Field has value. Click on Next. Select

Field as field_issue_status. Select Status as Returned. Click on Save. Click on Add an action. Select the

action as Load a referenced node. Click on Next. Select the Field as field_issue_book. Change the Label

as Issued book. Change the Machine readable variable name as issued_book. Click on Save. Click on

Add an action. Select the action as Populate a field. Click on Next. Select Content as Issued book. Select

the field as field_book_status. Click on Continue. Change the Status to Available. Click on Save.

Select Rules > Triggered rules > Overview in the admin bar. Click on the link clone on the row Change

status of book during return. Change the Label to Change status of book when lost during return. Click

on Save changes. Click on the condition Updated content’s field ‘field_issue_status’ has value. Change

the Status to Lost. Click on Save. Click on the action Populate Issued book’s field ‘field_book_status’.

Change the Status to Lost. Click on Save.

Test

Select Content management > Content > List from the admin menu bar. Select Update options as

Delete. Select the three contents of type Book issue. Click Update. Select Delete all.

Login as l1. Issue these 3 books using the Issue book link on the top.

Title Member

The Alchemist m1

Tuesdays with Morrie m2

7 Habits of Highly Effective People m2

Click on Issued books link to see the books issued. Click on Books to see the updated status of the

books.

Adjustment to views

We need to do more to the Issued books view for the librarian to work further.

Select Site building > Views > List on the admin bar. Edit all_issues. Select the page which has path as

all_issues. Add the Node: Edit link as a field to be displayed with label as Edit. Click Update and

override. Select the + next to Filters. Select Content: Member (field_issue_member). Click on Add.

Click on Expose. Change the Label to Member. Click Update and override. Select the + next to Filters.

Select Content: Status (field_issue_status) - Allowed values. Click on Add. Click on Expose. Change the

Label to Status. Click Update and override. Click Save.

Test

Login as l1. Click on Issued books link to see the books issued. Click on the edit link of the row 7 Habits

of Highly Effective People. Change the Status to Returned and set today as the date of return. Click

Save. Click on the edit link of the row The Alchemist. Change the Status to Lost and set today as the

date of return. Click Save. You can test the filters now. Click on Books link at the top to see the status

various books.

Page 28: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 28 of 33

Enhancement

Book issue records which are in Returned state should not be editable. We should remove the default

permission to edit. We will give special permission only for issue records which are in Lost or Issued.

Select Content Management > Content types > Edit Book issue > Access control from the admin menu

bar. Remove the Edit permission for library_librarian. Select Enable per content node access control

settings. Click Submit.

Select Rules > Triggered rules > Add a new rule from the admin menu bar. Enter Label as Grant

permission to edit issue when creating, and Event as After saving new content. Click on Save changes.

Add a condition to apply this rule only to created content of type Book issue. Add a condition to apply

this rule only if field_issue_status field does not have value Returned. Add an action to Grant content

permission by role. Here give Edit permission to library_librarian.

Select Rules > Triggered rules > Overview from the admin menu bar. Click Change status of book during

return. Click on Rule settings. Change the label to Change status of book during return and reset

permission of issue. Click on Save changes. Add an action to Reset content permissions.

Test

In admin login, click on Issued books link at the top. Edit the Issued and Lost books one by one. Click on

the Access control tab. Grant Edit permission to library_librarian. Actually this would have happened

automatically if we had the rules in place earlier. We can manually fix these now to make the state

correct.

Now login as l1. Click on Issued books link. You will find that the edit link is shown only for Issued and

Lost books. Edit the book The One Minute Manager and change the status to Returned. Set today’s

date as the date of return. Now you can find that edit link has vanished for that book.

Show only available books for issue

When you click on Issue book, all the books are shown, including those in circulation and lost. We should

see only books that are in Available state.

Select Site building > Views > List on the admin bar. Click on the Add tab. Type in books_of_status as

the View name. Type in List of all books with the given status as the Views description. Click on Next.

Select Content: Status (field_book_status) as Argument and Node: Type as filter. Select Is one of and

Library book as the criteria for node type. Change the order of filters to have node type as the first filter.

Add Node: Title as the Field with Label as empty. Click Save.

Select Content Management > Content types > Edit Book issue > Manage fields > Book from the admin

menu bar. Click on Advanced – Nodes that can be referenced (View). In View used to select the nodes,

select books_of_status. Enter this in the PHP Code block:

$uri = $_SERVER['REQUEST_URI'];

if(strstr($uri, /edit') == FALSE)

Page 29: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 29 of 33

return array(0 => 0);

else

return array();

Click Save field settings.

Test

Login as l1. Click on Issue book. You can see that only books that are available are listed. Issue the book

The One Minute Manager to m1. Click on Issue book again. You can find that the issued book is not

listed. You can click on Issued books to see it listed there.

Enhancement

Selecting return date every time is a pain. If the librarian has not set a return date, we should set it to

today.

Select Rules > Triggered rules > Add a new rule from the admin menu bar. Enter Label as When

returning book, set date if not already set, and Event as After updating existing content. Click on Save

changes. Add a condition to apply this rule only to content of type Book issue. Add a condition to apply

this rule only if field_issue_status field does not have value Issued. Add a condition to apply this rule

only if field_issue_return field has empty value. Click on Advanced: Specify the fields value with PHP

code. Type in return array(0 => array('value' => NULL)); in the box. Click Save. Add an action to populate

the field_issue_return field. Click on Advanced: Specify the fields value with PHP code. Type in return

array(0 => array('value' =>time())); in the box. Click Save.

Select Content Management > Content types > Edit Book issue > Manage fields > Date of return from

the admin menu bar. Change the Help text to To be filled if this needs to be a different date than

today. Click Save field settings.

Test

Now login as l1. Click on Issued books link. Edit the Issued book Tuesdays with Morrie. Change the

Status to Returned. Do not fill the Date of return. Click Save. You can see that the current date is taken

as the Date of return.

Enhancement

Status and Date of return fields need not be shown when issuing a book.

Select Content Management > Content types > Edit Book issue > Manage fields > Book from the admin

menu bar. In Field permissions section, select Create Date of return (edit on node creation) and Edit

Date of return, regardless of node author. Click Save.

Select Content Management > Content types > Edit Book issue > Manage fields > Status from the

admin menu bar. In Field permissions section, select Create Date of return (edit on node creation) and

Edit Date of return, regardless of node author. Click Save.

Page 30: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 30 of 33

Select User management > Permissions from the admin menu bar. In field_permissions_module,

enable edit field_issue_return and edit field_issue_status for library_librarian. Click Save permissions.

Test

Now login as l1 and issue a book and return the book, to verify the change in the form and confirm that

everything works fine.

Enhancement

As the librarian will mostly be issuing and returning books, we can make the Issued books page as the

default page. Members and stock manager can have the list of all books as the default page. Also, we

can add the Book issue form to all the pages for librarian.

Select User management > Permissions from the admin menu bar. In front_page_module section,

enable access frontpage for library_librarian. Click Save permissions. Select Site configuration > Front

page. Click Front Page for authenticated user. Select type as redirect. Type in all_books in the Redirect

to box. Click Front Page for library_librarian. Select type as redirect. Type in all_issues in the Redirect to

box. Click Save configuration. Note that you should never redirect the Front page for anonymous user.

Select Content management > Content types > Edit Book issue from the admin menu bar. Change

Enable data entry from a block to Enabled. Click on Save content type. Select Site building > Blocks >

List. On the row of Book issue form block, change the region to Left sidebar. Click on Save blocks.

As the logic that we have used to select the list of books depends on the URI, we can show this block on

all pages except the Edit issue page. Click on the configure link on the row of Book issue form block.

Select only library_librarian as the role to show the block to. Type in node/*/edit in the pages to skip.

Click on Save block.

Also, we can remove the menu item to issue book. Select Site building > Menus > List menus from the

admin menu bar. Click on the link Primary links. Click on the delete link on the row of Issue book and

confirm.

Test

Login as m1. You can see the list of books as the default page. Login as l1. You can see the list of issues

as the default page. Also, see that the Book issue page is shown in a block in the left.

Enhancement

If there are a large number of books, the selection list will not be convenient to use. Also, the librarian

will have the book in his hand when issuing. So he can type in the title of the book. Select Content

management > Content types > Edit Book issue > Manage fields > Book from the admin menu bar. Click

on Change basic information. Change the Widget type to Autocomplete text field. Click on Continue

and click on Save field settings. Rebuild the permissions whenever it asks to.

Test

Login as l1 and play around with the book titles. This is a very neat feature of Drupal.

Page 31: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 31 of 33

Enhancement

Let us add images to books. Go to some website where the books are listed with images like

flipkart.com, amazon.com or mylib.in. Go to Manage fields of Library book content type. Add new field

called Cover with the field name as field_book_cover. Select type as File. Select Form element to edit

the data as Image. Click on Save. Leave everything as it is and click on Save field settings.

Test

Login as s1. Click on Books link. Click on each book, edit, upload its image and save.

Enhancement

Now, let us improve the listing. Open the Views of all_books. Edit the page view. Add another page view

with the path all_books_compact. Save the views. Now select the views with path all_books. Add the

cover as a field. Select Label as None. Select Format as Image linked to node. Click on Update and

override. Change the Style in Basic settings to Grid. Remove Content: Id from the fields to display.

Move the Cover to the top of the list of fields. Make sure to always edit the page with path to all_books

and whenever provided with choice, choose Update and override. Save the view. In the header and

footer of the page with path as all_books, type in <a href="?q=all_books_compact">Click here for

compact view</a> and save. In the header and footer of the page with path as all_books_compact, type

in <a href="?q=all_books ">Click here for detailed view</a> and save. Make sure to always choose

Update and override.

Select Content management > Content types > Edit Library book > Display fields > Basic. Set the Label

to Inline for all the fields. Click Save. This will make the fields with a single value align with the label and

look better.

Test

Login as any user and see the book list. Try both the views.

Enhancement

Let us add review for books.

Enable the features of the Voting module at the end of the modules list. Enable URL Fill module. Add a

new content type called book_review. Title label can be changed to One-line summary. The body label

can be changed to Comments. Have field_review_book as a node reference to the Library book with

autocomplete text field as the widget. In the URL Fill section, enable it. Select Hide field. Have

field_review_rating as a type Fivestar rating and widget as Stars. Uncheck Allow user to clear value. Set

the default rating as 3. Set the voting target as the node reference to field_review_book. Mark it as

required.

Go to Manage fields of Library book content type. Add a new field field_add_review_link with Add

review as label, type as Computed and widget as Computed. In the Display format, type in $display =

"<a href='?q=node/add/book-review&field_review_book=".$node->nid."'>Click here to add a review

to this book</a>"; Uncheck Store using the … Click on Save field settings.

Page 32: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 32 of 33

Select User management > Permissions. Select authenticated user for create book_review content,

delete own book_review content and edit own book_review content. Select library_stk_mgr for delete

any book_review content and edit any book_review content. Save permissions.

Test

Login as m1. Create a review for the book The Alchemist. Login as m2. Create another review for the

same book.

Enhancement

We need to add the list of reviews to the corresponding books. Enable the features View Reference and

Viewfield in the modules list. Create a view called reviews_for_book. Add a filter to restrict to type

book_review. Add an argument with field_review_book. Add fields title, body and field_review_rating.

Set all labels to None. Rearrange the fields to have title, rating and then body. Save the view. Go to

Manage fields of Library book content type. Add a new field field_book_reviews. Select Views as the

type. Select Always use default value. Click on Default value. Select reviews_for_book – Defaults as the

Reviews and %nid as the Arguments. Select Required and reviews_for_book as the Allowed values.

Save the field settings.

Test

Login as m1. Click on the book The Alchemist. You can see the reviews.

Site look and feel

To change the name of the site, go to Site configuration > Site information from the admin menu bar.

To change the icon, colors and theme, go to Site building > Themes. You try various themes.

To remove the Submitted by … from the book pages, disable it at Site building > Themes > Configure >

Global settings.

Further enhancements You can try these enhancements to hone your Drupal skills:

Add reservation capability

Add delivery request management

Page 33: Drupal 6 Web Application Development Tutorialpracticalphilosophy.in/wp-content/uploads/2012/08/Drupal... · Drupal 6 Web Application Development Tutorial 28-Aug-2012 Page 3 of 33

Drupal 6 Web Application Development Tutorial 28-Aug-2012

Page 33 of 33

Going Further I hope this tutorial gave you an idea of Drupal’s capabilities as a Web Application Development

framework.

As you might have seen, there are tons and tons of options available in whatever you have done in this

tutorial. You can explore the options which you have not used to get familiar with the capabilities. The

starter package that you have used in the tutorial is almost sufficient to develop very powerful web

applications. There are some modules which are packaged but not enabled. You can explore some of

them.

There are tons of contributed modules to do almost everything that you would want to do. If you do not

find a suitable module, you can easily write a module to do what you want to do. If you know some PHP

programming, you can easily write modules. There are a lot of tutorials available on writing Drupal

modules.

If you want to customize the look and feel, you can do by writing some PHP and CSS code. You can even

develop your own themes easily. There are a number of tutorials on theme development for Drupal.

Drupal comes with built in tools to do performance tuning of the website. There is a lot of information

on the internet on Drupal performance tuning. There are also a number of professional consultants who

do Drupal performance tuning.

Welcome to the World of Drupal !!!