Beginning Drupal free download ebook
Join the discussion @ p2p.wrox.com
Wrox Programmer to Programmer™
Drupal
Beginning
®
Drupal
Jacob Redding
www.it-ebooks.info
Programmer to Programmer™
Get more out of
wrox.com
Interact
Join the Community
Take an active role online by participating in our
P2P forums @ p2p.wrox.com
Sign up for our free monthly newsletter at
newsletter.wrox.com
Wrox Online Library
Browse
Hundreds of our books are available online
through Books24x7.com
Ready for more Wrox? We have books and
e-books available on .NET, SQL Server, Java,
XML, Visual Basic, C#/ C++, and much more!
Wrox Blox
Download short informational pieces and code
to keep you up to date and out of trouble!
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com
www.it-ebooks.info
BEGINNING DRUPAL®
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1
Introducing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Installing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
CHAPTER 3
Your First Drupal Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
CHAPTER 4
Administration — Configuration, Modules, and Reporting . . . . . . . . . . . 49
CHAPTER 5
Administration — Blocks, Menus, and Themes . . . . . . . . . . . . . . . . . . . . 69
CHAPTER 6
Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
CHAPTER 7
User Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
CHAPTER 8
Taxonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
CHAPTER 9
Search, Performance, Statistics, and Reporting . . . . . . . . . . . . . . . . . . . 173
CHAPTER 10
Triggers, Actions, Workflow, and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 189
CHAPTER 11
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
CHAPTER 12
Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
CHAPTER 13
Theming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
CHAPTER 14
Contributed Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 15
Custom Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
CHAPTER 16
Development Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
CHAPTER 17
Module Development Start to Finish . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
CHAPTER 18
Advanced Drupal Installations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
CHAPTER 19
Preflight Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
APPENDIX
Exercises and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
www.it-ebooks.info
www.it-ebooks.info
BEGINNING
Drupal®
Jacob Redding
www.it-ebooks.info
Beginning Drupal®
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-43852-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 7486008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available
in electronic books.
Library of Congress Control Number: 2009930281
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other
countries, and may not be used without written permission. Drupal is a registered trademark of Dries Buytaert. All other
trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
www.it-ebooks.info
This book is first and foremost dedicated to the
Drupal community, a melting pot of some of the
most talented and intelligent people I have had the
pleasure of working with. Equally important is Diana
Kuan. She stuck by me while I wrote this book, pulled
incredible hours with the Drupal Association, traveled
to countless Drupal events and went crazy trying to
keep up with the intense development of Drupal 7.
Thank you Diana.
www.it-ebooks.info
CREDITS
EXECUTIVE EDITOR
Carol Long
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
PROJECT EDITOR
Ed Connor
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Barry Pruett
TECHNICAL EDITOR
Joshua Brauer
ASSOCIATE PUBLISHER
Jim Minatel
PRODUCTION EDITOR
Kathleen Wisor
PROJECT COORDINATOR, COVER
Lynsey Stanford
COPY EDITOR
Kathi Duggan
COMPOSITOR
Jeff Lytle, Happenstance Type-O-Rama
EDITORIAL DIRECTOR
Robyn B. Siesky
PROOFREADER
Carrie Hunter, Word One, New York
EDITORIAL MANAGER
Mary Beth Wakefield
INDEXER
Robert Swanson
MARKETING MANAGER
Ashley Zurcher
COVER DESIGNER
Michael E. Trent
PRODUCTION MANAGER
Tim Tate
COVER IMAGE
© Jim Jurica/istockphoto
www.it-ebooks.info
ABOUT THE AUTHOR
JACOB REDDING got his start in Drupal in 2005 after 10 years in the IT
industry building custom software with everything from Microsoft to Linux
and UNIX technologies. He has since become heavily involved in the Drupal
association and Drupal communities around the world, speaking and leading
classes in not only New York, San Francisco, Boston, and Washington DC,
but also far-flung locales such as China and Jordan. An enthusiastic advocate
of open-source, he currently spends most of his time providing project management and training on Drupal-based websites.
www.it-ebooks.info
ACKNOWLEDGMENTS
DIANA KUAN DESERVES the top spot here. Serving as a part-time editor, proofreader, supporter,
and impromptu student she helped set the flow of the book, proofread nearly every page, and tested
many of the exercises. Without her assistance and support this book would have taken yet another
year, an outcome that would have surely sent the publisher’s henchmen out to get me.
Jason Chinn, Mr. Magicspark, helped me through the rough spots of theming including building the
nice Drupal chocolate theme used in Chapter 13. Without him the theming chapter would have been
a rambling mess.
My students: To those in China: ԴӀᐂ៥ᕜ៥ᛇ䇈䴲ᐌᛳ䇶ԴӀDŽ ៥Ꮰᳯ៥Ӏৃҹᴹ㾕䴶DŽ My
students in Jordan caught me during a critical time of this book and unknowingly tested several of
the exercises in the book, thank you for secretly helping to create this book. I can’t leave out those
in my home country of the United States, thank you for listening to me ramble about in class. I hope
that I have been a useful part of your Drupal journeys.
Joshua Brauer: My technical editor. Although we have never met and I’m not entirely sure how we were
paired together, the odd in-book synergy somehow worked. I was able to see through his trying-to-benice commentary that brow beat me into thoroughly revising the chapters into something useable.
Carol Long and Ed Connor at Wiley/Wrox: Oh boy. These two had no idea what they signed up for.
The book started on very early versions of Drupal 7 and was revised too many times for them to
count. Their patience and tolerance of my “I’ll get it to you soon-ish” (heavy emphasis on –ish) and
“by the way Drupal 7 changed so I have to rewrite the entire chapter” is much appreciated.
The entire Drupal community: You gals and guys are amazing. From small meetups in Beijing,
large DrupalCamps in Los Angeles, collaborative working sessions in New York and the awesome
DrupalChix group, you (the Drupal community) are amazing individuals to know and to collaborate
with. With the book fi nally complete I hope to see you all in IRC more often.
www.it-ebooks.info
CONTENTS
INTRODUCTION
xxi
CHAPTER 1: INTRODUCING DRUPAL
History of Drupal
1
2
Why is the Name Drupal?
Who is Drupal?
2
2
The Drupal Community
Where Is the Community?
Professional Support
The Drupal Association
Summary
2
3
4
5
6
CHAPTER 2: INSTALLING DRUPAL
Getting Started with Drupal
Installing a Single Drupal Website
Installing Drupal
Installation Profiles
Installing Drupal in Different Languages
Acquia’s Stack Installer
9
10
10
13
16
16
16
Breaking It Down
Don’t Hack Core
Core Files
Sites Folder
What’s in a Site’s Folder?
Inheritance and Overrides
19
19
20
21
21
22
Summary
24
CHAPTER 3: YOUR FIRST DRUPAL WEBSITE
Adding Content
27
27
Differences between Articles and Pages
Content Summaries
Modifying the Defaults
Modifying Your Menus
Adding Blocks to Your Website
Getting in Contact
Adding the Contact Form to Your Main Menu
Summing Up the Contact Form
www.it-ebooks.info
29
30
30
31
33
34
36
37
CONTENTS
Exploring Your Site’s Permissions
Creating a Members-Only Site
Exploring Roles
Wrapping up Users and Permissions
Time for a New Look
38
39
39
43
43
Time for Something Different
Cron
44
45
Manually? No Way! Let the Computer Do It
A Need for Speed
Summary
46
46
47
CHAPTER 4: ADMINISTRATION — CONFIGURATION, MODULES, AND
REPORTING
Configuration
System — Site Information
System — Actions
Regional and Language — Settings
Regional and Language — Translate
Media
Media — File System
Temporary Directory
PHP File Size Limitations
Media — Image Toolkit
People and Permissions
Web Services
Web Services — RSS Publishing
Web Services — Feed Aggregator
Content Authoring — Text Formats
Explore the Filtered HTML Text Format
Modify Text
Add Text
Remove Text
Interpreting Text
Choosing Roles and the Importance of the Order
Configuring the Filters
The Order Matters
Additional Filters
Writing Your Own Filter
Search and Metadata - Clean URLs
Apache
A Need for Speed
49
50
51
53
53
54
54
55
56
56
57
57
57
58
58
58
59
59
59
59
60
60
61
61
61
62
62
63
63
x
www.it-ebooks.info
CONTENTS
IIS
64
Search and Metadata — URL Aliases
Alias Your Way to a Better Search Ranking
Maintenance Mode
IP Address Blocking
Summary
64
65
65
66
67
CHAPTER 5: ADMINISTRATION — BLOCKS, MENUS, AND THEMES
Blocks
69
70
Block Visibility
Block Visibility Using PHP
73
75
Menus
77
Adding a Menu Link Directly on Content
A Word On Permissions
Menu Settings
Related Modules
URL Aliases AKA Custom Paths
A Word on Permissions
80
80
80
81
81
83
Themes
83
Theme Administration
Theme Global Settings
Theme Settings Summary
Installing New Themes
Administrative Theme
Post Information
83
84
85
85
86
87
Summary
87
CHAPTER 6: CONTENT
91
Nodes
92
What is a Node?
92
Creating Custom Content Types
Submission Form Settings
Publishing Options
Display Settings
Comment Settings
Menu Settings
93
95
96
97
97
97
Adding an Image Upload Field
Multiple Fields for Multiple Images?
The Power of Image Field
Permissions
98
99
99
104
Revisions
105
xi
www.it-ebooks.info
CONTENTS
Digging Deeper Into Fields
Fields
Field Types (Type of Data to Store)
Widgets (Form Element)
Allowed Values List
Content Construction Kit (CCK)
Views
Administering Nodes
Content Moderation
Quick and Easy Moderation
Nodes in a Nutshell
Comments
Permissions
106
106
107
107
109
109
110
110
111
111
111
112
113
Comment Administration
Comment Administration Page
Comment Approval Queue (A.K.A Comment Moderation)
Comment Form
Allowing Rich Text Comments
WYSIWYG
Spam
RSS Aggregation
113
114
114
116
116
117
117
118
Displaying the Feed
Configuring the Aggregation Settings
Summary
120
121
122
CHAPTER 7: USER MANAGEMENT
Creating User Accounts
125
126
People
Anonymous Users
126
126
Creating Accounts
126
Approving Accounts in Moderation
Setting a Password During Registration
Canceling Accounts
Setting Up Account E-mails
Using OpenID
Creating Roles and Permissions
Administrator Role
128
129
129
129
130
132
135
Creating Custom User Profiles
135
Profiles in Drupal 7
User Profiles vs. User Fields
Using Fields for Profiles
135
136
136
xii
www.it-ebooks.info
CONTENTS
Using the Core Profile Module
Controlling Access to User Profiles
Hidden Fields
Visibility Options Summarized
Tracking User Activity
137
140
142
143
143
Using Views to Create Custom Tracking Pages and Blocks
Custom Administration Pages with Views
User Sessions
Forcing Sessions to Expire
144
150
153
154
Summary
155
CHAPTER 8: TAXONOMY
157
In Simple Terms
Terminology
Vocabularies
Terms
157
158
158
159
Advanced Options
159
Using Terms on Your Content
Taxonomy Term Field Settings
A Term’s Parent/Child Relationship
Using Views to Display Term Pages
Display Multiple Terms on a Single Page
API
Theming Considerations
160
162
164
165
167
168
168
Customizing Each Section
168
Useful Contributed Modules
Summary
169
170
CHAPTER 9: SEARCH, PERFORMANCE, STATISTICS, AND REPORTING 17 3
Search
174
Indexing Your Content
Advanced Search Settings
Indexing Settings
CJK Handling
Content Ranking
Faceted Search
Outsourcing Search
If You Don’t Want to Outsource
Performance
175
176
176
177
177
177
178
178
178
Caching
Cache Blocks
178
179
xiii
www.it-ebooks.info
CONTENTS
Page Cache for Anonymous Users
Minimum Cache Lifetime
Bandwidth Optimization
Clearing Cached Data
The Need for Speed
Reporting
179
179
180
180
181
182
Status Report
Recent Log Entries
183
183
Statistics
184
Access Log
Content Views
Performance Considerations
Summary
185
185
185
186
CHAPTER 10: TRIGGERS, ACTIONS, WORKFLOW, AND RULES
Triggers and Actions
189
190
Working with Actions
Working with Triggers
Next Steps
190
191
193
Workflow
Rules
Features of the Rules Module
Conditions
Scheduler and Rule Sets
Input Evaluators
Summary
CHAPTER 11: VIEWS
193
200
201
203
206
209
210
213
Advanced Help
Overview of Views
The Frontpage View
214
214
215
Explore the Frontpage View
Displays
Style Settings
Creating a Photo Gallery
Arguments
Exposed Filters
215
218
220
222
225
229
Exposed Items in a Block
Making Your View Accessible via a Tab
Relationships
231
232
234
xiv
www.it-ebooks.info
CONTENTS
Permissions
235
All-Views Permissions
Per-View Permissions
236
236
Caching (AKA Performance)
236
Caching
Block Caching
Best Practices
236
237
237
Advanced Views
Summary
238
238
CHAPTER 12: INTERNATIONALIZATION
A Bit of Terminology
Localizing
241
241
242
Regional Settings
Date and Time
Localizing Your Site’s Interface
Your Site’s Primary Language
Language Negotiation
Completing Your Site’s Localization
Making Translation Easy with the L10N Client
Translating Your Content
Beyond the Basics
Summary
242
243
243
249
249
252
253
258
260
261
CHAPTER 13: THEMING
263
Administering Themes
264
Installing New Themes
Configuring Your Themes
264
265
Custom Theming
266
Template Files
Theme Functions
Reviewing the Last Five Exercises
Sub-Themes
270
274
279
280
Zen Base Theme
282
.info File
282
Removing Theme Settings
Style Sheets
JavaScript
Regions
282
283
284
285
Template Files (.tpl.php)
286
page.tpl.php
node.tpl.php
286
287
xv
www.it-ebooks.info
CONTENTS
comment.tpl.php
block.tpl.php
Template Suggestions
287
287
288
Preprocess Functions
288
Adding New Variables
Template Suggestions
289
289
Swapping Theme Functions and Template Files
Moving a Theme Function to a Template File
Moving a Template File to a Theme Function
Theme Registry
Theme Engines
Summary
290
290
290
291
294
296
CHAPTER 14: CONTRIBUTED MODULES
Contributed and Custom Modules
Determining Your Needs
Fulfilling Your Needs
Lullabot Podcast
Top Drupal Modules
Showcases
Planet Drupal
299
299
300
301
301
302
302
303
Finding Modules
303
Search Drupal.org
Google
Drupalmodules.com
IRC and the Drupal Community
RSS Feeds
Twitter
Is It the Right Module?
303
304
304
305
305
305
306
Project Page
Google
Coder Module
Tests
306
309
309
309
Be a Follower
309
Take It Slow
Set Up a Development Environment
Avoid the Buffet
Summary
310
311
311
312
xvi
www.it-ebooks.info
CONTENTS
CHAPTER 15: CUSTOM MODULES
What Is a Module?
Hooks
315
315
317
More Information
319
Modules Step by Step
319
Step 1: Choose a Name
Step 2: Create the .info File
Step 3: Create the .module File
Creating a Page
319
320
321
321
Capturing Custom URLs with hook_menu
Modifying Existing Modules
322
324
Patching
Creating a Patch
The Value of the Patch
When to Modify
325
325
327
327
Summary
327
CHAPTER 16: DEVELOPMENT HOOKS
331
Hooks: A Module’s Starting Point
Community Documentation
The Hooks
332
332
333
hook_menu
333
Making Your Modules Themeable
The t() Function
Theme Functions and Template Files
Summary
339
340
341
344
CHAPTER 17: MODULE DEVELOPMENT START TO FINISH
The Module
347
348
Beginning Your Module
Implementing a hook
349
350
Adding Custom Configuration Settings
Custom Module Settings
How Did it Become an Option at Configuration?
How Did the Settings Form Appear?
Where Are the Settings Saved?
Filtering on Multiple Words
A Quick Review of the New PHP Code
351
352
353
354
354
355
356
xvii
www.it-ebooks.info
CONTENTS
Helping Your Users
Permission Handling
Storing Data in a Custom Table
356
358
359
Database Abstraction Layer
360
Drupal Query Functions
Displaying the Results on the User’s Profile
Database Summary
Creating a Bulletproof Site with SimpleTest
Test Driven Development
How SimpleTest Performs Its Testing
Writing Your Own SimpleTest
SimpleTest Assertions
Adding the Tests to Your Module
Summing Up SimpleTest
Summary
361
365
367
367
367
369
369
371
373
375
375
CHAPTER 18: ADVANCED DRUPAL INSTALLATIONS
Drush
379
380
But Wait There’s More!
381
Using CVS to Manage Drupal
What is CVS?
What is SVN?
Installing CVS
Installing and Upgrading Drupal using CVS
Understanding CVS Tags
How Does This Relate to CVS?
Branches and Tags
Installing Modules from the CVS Repository
One Drupal, Many Websites
Example 1: Multiple Independent Websites
Example 2: Multiple Related Websites
Sites.php
Settings.php
382
382
382
383
383
385
385
386
386
387
388
389
392
392
$databases
Scalability
$db_prefix
$baseurl
$ini_set
$reverse_proxy
393
394
394
395
395
396
xviii
www.it-ebooks.info
CONTENTS
Variable Overrides
String Overrides
IP Blocking
396
396
397
Summary
397
CHAPTER 19: PREFLIGHT CHECKLIST
40 1
Preflight Checklist
402
Site Information
User Settings
Site Maintenance
SPAM
SEO
Search
Performance
Disaster Recovery
402
402
402
403
403
403
404
404
SEO
404
SEO Best Practices
The Importance of URLs
XML Sitemap
Submitting Your Sitemap to Search Engines
Analyzing Your Site with Google
Dashboard
Summary
405
405
405
408
411
413
415
APPENDIX: EXERCISES AND ANSWERS
417
INDEX
433
xix
www.it-ebooks.info
www.it-ebooks.info
INTRODUCTION
OVER THE PAST FEW YEARS, the Web has undergone dramatic changes. What originally started as a
handful of static web pages with a few e-commerce sites in the mix has now turned into a frenzy of
collaboration and information sharing. New websites are expected to meet a certain level of interactivity and information-sharing. RSS feeds, blogs, and podcasts are just a few of the current standards. Photo uploads with auto-resizing and cropping, video and audio embedding, and even web
APIs are quickly becoming the new standard-website functions.
Years ago, when I started creating dynamic websites, I began as most developers did, by writing my
own custom content management system (CMS). My experience was not unlike that of others who
followed the same path. The system was light and fast, and did everything that I, and my clients,
asked — until a new feature emerged on the Web, and my software had to play catch-up. Soon I was
buried under a pile of feature requests and bug fi xes, which left me with no time to innovate. My quest
for a better solution led me through a maze of software packages, until a friend told me about Drupal.
According to him Drupal has two really great things going for it. The fi rst is that it’s built on a modular structure, so it can quickly adapt to custom and unique scenarios. The second is that it is built
around a community that is growing every day. When a new feature emerges on the Web, chances
are someone has already gotten it to work within Drupal. The community factor is what sold me.
Now that I have been working with Drupal for a few years, I am in awe at how well the community
has organized itself and, as a result, kept itself and the Drupal project on the cutting edge of web
technology. Drupal has moved from being a piece of software that modularized the latest Internet
technologies, to a platform that creates the latest technologies. The community made this happen.
You are now a part of this awesome community. Welcome.
Who This Book Is For
This book was written for people who want a robust website and aren’t afraid to get their hands dirty.
Maybe you’re the leader of a nonprofit who wants to organize your constituency through a
community-driven website. You have a bit of prior experience in creating HTML pages by hand
or with another CMS. You’re now ready to dig in and create your community’s website, and
Drupal is your tool of choice, but you want to be on the fast track.
Perhaps you’re an established developer and possess the skills to create your own custom CMS
or have experience with other software packages. You’re looking for defi nitions and examples on
Drupal’s terminology and structure so that you can take advantage of Drupal’s fast development
cycle that you hear so much about.
To get the most out of this book, you should already possess or have a strong desire to obtain experience in web development with HTML, JavaScript, or PHP. I did not set out to write a book that
guides you step-by-step through building a specific website such as one for a restaurant or bakery.
Instead, I set out to write a book that will help you understand how you can use Drupal to take the
www.it-ebooks.info
INTRODUCTION
vision in your head and apply it to the Web. For example, when you understand that you need a
new form of content, fl ip to Chapter 6, "Content," which discusses all aspects of content in depth,
including creating, administering, moderating, and controlling that content.
Experienced developers who are looking to propel themselves on the fast track to custom module development might fi nd it useful to skim Chapters 2 through 4, and then skip forward to the
later chapters on module development. I would suggest that you spend a bit of time on Chapter 6,
"Content," and Chapter 10, "Workflow and Actions," and Chapter 11, "Views." A good understanding of these chapters may save you countless development hours.
What This Book Covers
This book focuses on Drupal 7, but the changes since Drupal 6 are highlighted where appropriate. This book walks you through the entire process of creating a Drupal website. It starts with the
installation, moves to administration and configuration, and then covers users and permissions. The
book also discusses content (including nodes, users, and blocks), Drupal’s infamous taxonomy system, tips on how to make your site not look like a Drupal site, and instructions on expanding your
site with contributed and/or custom modules.
How This Book Is Structured
I often train others on how to use Drupal, so this book is structured similar to a training manual.
The chapters are ordered in a manner that builds upon the topics of the previous chapter. For
example, the chapter on Views is after the chapters on users, content, and taxonomy, because a
view works with all of these items. I believe this order will help you understand the overall flow and
structure of Drupal. Each chapter, however, is a thorough examination of its topic.
I don’t expect you to read the book cover to cover before building your website. You should read
this book alongside your development and use it as an aid or reference.
If you are new to Drupal, I recommend that you read at least the fi rst parts of each chapter, but you
can move to the next chapter if the topic becomes too complex or just plain boring. You can then
revisit each chapter when you need clarification or more information.
Experienced developers or those with previous Drupal experience may fi nd it beneficial to skip to
the chapters that matter most to them. Each chapter is designed to be independent, although the
examples in one chapter may build upon the examples in the previous chapter. If you fi nd this to be
the case, simply fl ip back one chapter and follow the last example to catch up.
What You Need to Use This Book
The book covers Drupal version 7, which has the following requirements:
➤
Webserver: Apache 1.3 or 2.x or IIS 6+
➤
Database server: MySQL 5+ or Postgres 8.3+
➤
PHP 5.2 or higher
xxii
www.it-ebooks.info
INTRODUCTION
The XAMPP project for Microsoft Windows, Linux, and Mac OS X as well as the MAMP project
for Mac OS X contain all of these requirements in a single downloadable package.
Please visit http://drupal.org/requirements for up-to-date system requirements.
CONVENTIONS
To help you get the most from the text and keep track of what’s happening, I’ve used a number of
conventions throughout the book.
Exercises that you can try out for yourself generally appear in a box like this:
TRY IT OUT
What You Will Be Doing in this Exercise
Each Try It Out consists of an exercise you should work through, following the text in the book.
1.
2.
3.
They usually consist of a set of steps.
Each step has a number.
Follow the steps using your copy of the database.
How It Works
After each Try It Out, the code you’ve typed will be explained in detail.
WARNING Boxes like this one hold important, not-to-be forgotten information
that is directly relevant to the surrounding text.
NOTE Notes, tips, hints, tricks, and asides to the current discussion are off set
and placed in italics like this.
This book also uses the following styles to set apart non-standard text:
➤
New and important terms are italicized.
➤
A combination of two or more keyboard strokes is presented like this: Ctrl+A.
➤
File names, URLs, and code within the text are shown in a monofont, like so: persistence.
properties.
xxiii
www.it-ebooks.info
INTRODUCTION
➤
Separate lines of code are formatted in the following ways:
Monofont type with no highlighting is used for most code examples.
Bold is used to emphasize code that’s particularly important in the present
context.
SOURCE CODE
As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code fi les that accompany the book. All the source code used in this book
is available for download at http://www.wrox.com. When at the site, simply locate the book’s title
(use the Search box or one of the title lists) and click the Download Code link on the book’s detail
page to obtain all the source code for the book. Code that is included on the Web site is highlighted
by the following icon:
Listings include the fi lename in the title. If it is just a code snippet, you’ll fi nd the fi lename in a code
note such as this:
code snippet filename
NOTE Because many books have similar titles, you may find it easiest to search
by ISBN. This book’s ISBN is 978-0-470-43852-7.
Once you download the code, just decompress it with your favorite compression tool. Alternately,
you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/
download.aspx to see the code available for this book and all other Wrox books.
ERRATA
We make every effort to ensure that there are no errors in the text or in the code. However, no one
is perfect, and mistakes do occur. If you fi nd an error in one of our books, like a spelling mistake or
faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save
another reader hours of frustration and at the same time you will be helping us provide even higher
quality information.
To fi nd the errata page for this book, go to http://www.wrox.com and locate the title using the
Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On
xxiv
www.it-ebooks.info
INTRODUCTION
this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/
misc-pages/booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the
information and, if appropriate, post a message to the book’s errata page and fi x the problem in subsequent editions of the book.
P2P.WROX.COM
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based
system for you to post messages relating to Wrox books and related technologies and interact with
other readers and technology users. The forums offer a subscription feature to e-mail you topics
of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other
industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will fi nd a number of different forums that will help you not only as
you read this book, but also as you develop your own applications. To join the forums, just follow
these steps:
1.
2.
3.
4.
Go to p2p.wrox.com and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join as well as any optional information you wish to
provide and click Submit.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
NOTE You can read messages in the forums without joining P2P but in order to
post your own messages, you must join.
When you join, you can post new messages and respond to messages other users post. You can read
messages at any time on the Web. If you would like to have new messages from a particular forum
e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to
questions about how the forum software works as well as many common questions specific to P2P
and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
xxv
www.it-ebooks.info
www.it-ebooks.info
1
Introducing Drupal
WHAT YOU WILL LEARN IN THIS CHAPTER:
➤
History of Drupal
➤
Drupal’s community
➤
How to find and participate in the community, including: IRC,
Meetups, Camps, and Drupal conferences
➤
Professional Drupal support
➤
The Drupal Association
What is Drupal? If this is your fi rst time using Drupal, you’re probably trying to answer this
yourself, and you may have heard confl icting answers. Often Drupal is described as a content
management system, whereas other times, it’s called a content management framework. So is
it a system or a framework? Here’s a simple defi nition:
Drupal is an open source software application that manages the content of
and builds websites and web applications. It can be used to create a web
blog, e-commerce store, photo gallery, or social networking website.
That seems straightforward, right? In fact, it sounds a lot like Joomla!, Wordpress, Zope,
and many other content management systems (CMSs). So why do people use the term content
management platform or content management framework?
The use of the terms platform and framework is an attempt to describe Drupal as a base or a
foundation onto which you build your desired websites or web applications. Out of the box,
Drupal can be used to instantly create a website with users, articles, blogs, comments, and a
forum. With a few modules from http://drupal.org it can be transformed into an e-commerce
site, a group photo gallery, and more. Drupal is also built around a modular core and a strong
www.it-ebooks.info
2
❘
CHAPTER 1 INTRODUCING DRUPAL
application programming interface (API) so you can quickly and easily extend Drupal to build the site
of your dreams. This leads to the following, more technical definition:
Drupal is a modular framework written in the PHP scripting language that
contains a CMS, a module system, and an API for rapid development of websites
and web applications.
An example I’ve used often is Lego systems. You can purchase the race car Lego set and build
the provided example race car. You could also build a luxury car with the same Lego set. If you
purchase the rocket ship Lego set, you could combine the two and build a luxury rocket race car.
After all, it’s only a set of blocks that you put together using your imagination. Drupal is built on
the same idea — it’s a set of modules that you mix and match to build your perfect website or web
application.
HISTORY OF DRUPAL
In 2000, Dries Buytaert, a student at the University of Antwerp, needed a method to communicate
with his classmates and friends. He built a small web application so that he and his friends could
leave notes for each other. After Dries graduated he moved the software over to drop.org and used
it to experiment with new web technologies such as syndication, rating, and distributed authentication. Dries also made the software freely available and licensed it under the GPL. It didn’t take long
before the software’s unique modular structure gained notice and attracted a community of developers and users.
Why is the Name Drupal?
When searching for domain names Dries accidentally misspelled the Dutch word dorp (meaning village) as drop. Accepting his mistake he registered and started to use the drop.org domain. In 2001
the project officially adopted the name Drupal. The Dutch word for drop is druppel, which changed
to Drupal (pronounced “droo-puhl”) for easier pronunciation.
Who is Drupal?
As of this book’s writing, thousands of developers have contributed to the Drupal project. Many
have contributed by adding modules and themes, providing support on drupal.org and the Internet
Relay Chat (IRC) channels, and participating at meetups, DrupalCamps, and conferences around
the world. Drupal has been recorded as having a developer base in almost every country in the
world. It truly is an international open source project.
THE DRUPAL COMMUNITY
Drupal is not just software — it’s a community! The Drupal community is one of the largest and
most supportive communities in the open source world. In fact, the community is so strong that
it should be a major deciding factor when you’re choosing whether or not to use Drupal. Imagine
building a site that requires a certain piece of functionality. You found a module that does 90
www.it-ebooks.info
Where Is the Community?
percent of the work but isn’t quite right. You can decide to code this functionality on your own or
you could work with the current module owner to modify and build in the functionality you need.
If you code it yourself, you will have to maintain it in perpetuity, but if you work with the current
module owner and the community, you are sharing maintenance with the community. The latter
result is a more sustainable, longer lasting website and web application.
There is no formal agreement with the community. The agreement is much more tacit. A user providing support one day may expect to receive support another day. Likewise, other module developers will maintain their module if you maintain yours, so that everyone’s site can grow and expand.
You do not have to be a developer to participate in the community. There are groups of designers,
users, administrators, and others at all levels working together to build great sites. The worst thing
you can do when starting out with Drupal is to ignore the community.
WHERE IS THE COMMUNITY?
The Drupal community meets online and, better yet, in the real world. Here are a few places where
you can fi nd them:
➤
Forums on Drupal.org (http://drupal.org/forum)
Installation, configuration, migration, translation, upgrading, and module development are
just some of the topics covered in the forums. Forums are a great place to start when first
learning about Drupal and its community.
➤
IRC channels
Because it functions in real time, IRC can provide the fastest support, communication, and
collaboration. IRC can be accessed using IRC software such as Mirc (Windows), Colloquy
(Mac OS X), or xChat (Linux). The server is irc.freenode.net and the top three channels
are (note that many more exist):
➤
#drupal-support — Provides general support for installation, configuration, and
site administration. When starting out with Drupal this is the channel to use.
➤
#drupal-themes — Provides support for the creation and modification of Drupal
themes (most often on custom themes).
➤
#drupal — Hosts high-level development discussions on Drupal core and modules.
Once you have used Drupal for a while and are developing modules, use this channel
to collaborate with other developers.
You can fi nd more channels and the rules to IRC etiquette at http://drupal.org/irc.
➤
Drupal Groups at http://groups.drupal.org
The Drupal Groups website is the Grand Central station of the Drupal community. Likeminded users and developers meet and collaborate on common topics and projects on this
website. Regional groups from New York City, Paris, Wisconsin, Peru, and Texas schedule
regular meetups. Topic-based groups such as High Performance and Drupal in Education
are great for fi nding others who are in similar situations as you. At the time of this writing,
there were over 360 active groups. Find a local group and get involved!
www.it-ebooks.info
❘3
4
❘
CHAPTER 1 INTRODUCING DRUPAL
➤
Regional and language-based websites
There are several regional websites that enable users to communicate in their local language
or connect within a geographic region. Examples include drupalchina.org, drupaltaiwan.org, drupalitalia.org, and ladrupal.org. Language-based communities can be
found at http://drupal.org/language-specific-communities, and geographic communities can be found at http://groups.drupal.org/groups.
➤
Camps and conferences
Drupalcon brings together developers, designers, users, and other Drupal professionals from
all over the world. Currently Drupalcon is held once a year in Europe and North America
but is quickly expanding to other parts of the world. Check http://drupalcon.org for the
latest event schedule.
Drupalcon may be the biggest Drupal-based conference, but it is not the only one.
DrupalCamps, meetups, and other community-organized events happen daily around the
world. Check http://groups.drupal.org/events for the most recent events.
PROFESSIONAL SUPPORT
The Drupal community forums and IRC channels are filled with wonderful volunteers working day
and night to help each other build and troubleshoot their websites. Although working with the community is the best way to learn Drupal quickly, you may or may not have your questions answered
or problems fi xed in a timely manner; these channels can require patience. Fortunately the companies of the Drupal community are there to help you.
Drupal is a software application that runs on a server platform, meaning a host. For this reason support packages are divided into three general categories: Drupal-only, server-only, Drupal + server.
Many hosting companies provide server-only support and do not support the Drupal software,
whereas Drupal professionals may provide Drupal-only support. When purchasing support it is best
to ask what is and what is not supported to fi nd the right combination for your project and budget.
You can fi nd a list of hosting companies that specialize in Drupal at http://drupal.org/hosting
and a list of companies providing support, consulting, and development at http://drupal.org/
services. To help get you started here a few of the more popular options:
➤
Acquia Drupal
Created by the founder of Drupal, Dries Buytaert, this company provides a distribution of
Drupal that bundles some of the most popular contributed modules and a custom Acquia
module to connect your website to Acquia’s automated monitoring and support network.
Because Acquia’s distribution is built on top of Drupal, it is 100 percent compatible with all
of Drupal’s modules, themes, translations and other items, including every exercise in this
book. In fact, you can use Acquia’s Drupal distribution with this book.
Acquia Drupal can be found at http://acquia.com.
➤
Hosted Drupal solutions
A hosted Drupal solution is different from a regular host. A hosted Drupal solution is a host
that installs, configures, and fi ne-tunes Drupal for you. Often these companies will continually monitor your installation for updates to Drupal and its modules. Although Drupal is
www.it-ebooks.info
The Drupal Association
❘5
compatible with many hosts the difference is the specialty in Drupal, which you’ll appreciate
when your site goes down in the middle of the night. Many other Drupal companies will
only host sites they build or help you build. But a hosted Drupal solution takes your site,
your code, and your system and makes sure it’s rock-solid.
Check out the following hosted Drupal solutions:
➤
Hot Drupal (http://hotdrupal.com)
➤
Workhabit (http://workhabit.com)
➤
Acquia Drupal (http://acquia.com)
For high availability, high traffic websites check out Acquia, Four Kitchen’s Pressflow Drupal
distribution http://pressflow.org, Pantheon, an EC2/Cloud computing distribution
(http://getpantheon.com), or Firehose at http://stationindustries.com.
You can browse many more hosted Drupal solutions or Drupal-compatible web hosts at
http://drupal.org/hosting.
➤
Drupal professionals
The Drupal community is teeming with dedicated professionals that are ready to help you
build your website, provide a bit of custom development, or give you advice. Companies
exist for customers with nearly any budget, from the hobbyist to the Fortune 500 enterprise
customer. If you choose to hire a professional be sure to inquire about their launch and ongoing support options.
You can browse a list of Drupal professionals at http://drupal.org/services.
Confused? Don’t be. The community is there to help you. If you have questions simply log into IRC
channel #drupal-support, ask your question in the Drupal forums at http://drupal.org/forums, or
pick up your phone and talk to one of the many Drupal professionals listed at http://drupal.org/
services.
Of course you don’t need to hire a professional to build, support, or even host your website. Keep
reading to become your own Drupal professional.
THE DRUPAL ASSOCIATION
Standing to the side of the community and directed to support and protect your rights as a community member is the Drupal Association. Originally created to purchase hardware to support
the drupal.org infrastructure, the Association quickly became a defender of the Drupal trademark
from an erroneous claim against it. This all community- and volunteer-run nonprofit works to
ensure that Drupal continues to be free and available for everyone to enjoy. A few of the activities
that the Association performs are:
➤
Purchasing infrastructure (servers, etc.) that the drupal.org websites use
➤
Protecting the Drupal trademark
➤
Defending Drupal against GPL infringements
➤
Working with the community to organize the annual Drupalcon conference
www.it-ebooks.info
6
❘
CHAPTER 1 INTRODUCING DRUPAL
➤
Redesigning and creating functional upgrades to the drupal.org website
➤
And many other supporting activities!
The Association does not write or maintain the Drupal software or control the content on any
of the drupal.org websites. The Drupal project is solely owned and run by the community. As
a community-run organization all users are able and encouraged to participate in the Drupal
Association to help Drupal continue as one of the greatest open source projects!
The Drupal Association can be found at http://association.drupal.org.
SUMMARY
It should be clear that Drupal is much more than great software; it is also a great community. The
Drupal software, built upon a modular architecture, allows you to build great websites with relative
ease. And the community of users, designers, and developers, as well as great Drupal companies, are
all there to help you support your website both now and in the future. You need no special skills or
invitation to join the Drupal community, just a bit of time to participate in it. Drupal is more than
software. Drupal is you!
Welcome to the community!
In the next chapter you’ll explore Drupal’s installation process and begin your Drupal journey.
EXERCISES
1.
What website can you use to connect with other Drupal users, developers, and professionals with
similar interests or located near you?
2.
Where can you communicate with other Drupal users or developers in real time over the Internet?
3.
What nonprofit organization helps to protect the Drupal community’s code from GPL copyright
infringement, protects the Drupal trademark, and supports the http://drupal.org infrastructure?
4.
Drupal is a great CMS, but developers also love it because of its great ____? (three letter
acronym)
Answers to the Exercises can be found in the Appendix.
www.it-ebooks.info
Summary
⊲ WHAT YOU LEARNED IN THIS CHAPTER
➤
Drupal is not only a CMS but also a framework and an API for building great websites and
applications.
➤
Drupal’s modular structure allows you to plug in the pieces you need to build the website of
your dreams.
➤
Drupal is pronounced “droo-puhl” and comes from the Dutch word for drop.
➤
Drupal is not just software. It is a living and thriving community of developers, designers,
and users.
➤
The Drupal community meets online in the drupal.org forums and IRC channels as well as
in-person at locations around the world. Find out more at http://groups.drupal.org.
➤
From the hobbyist to the Fortune 500, companies exist at all levels to give you professional
service.
➤
You are now part of the Drupal community!
www.it-ebooks.info
❘7
www.it-ebooks.info
2
Installing Drupal
WHAT YOU WILL LEARN IN THIS CHAPTER:
➤
Installing a Drupal website
➤
Installing Drupal using the Acquia Distribution
➤
Exploring the files, folders and structure of Drupal
➤
Reviewing the sites folder
➤
Exploring inheritance and overrides
➤
Overriding Drupal’s default theme with one of your own
Now that you have decided to use Drupal the next step is to download and install it. In fact
you might have already installed one or more Drupal websites. Even if you have already
installed Drupal, a quick read through this chapter will help to clarify the process and help
you better understand Drupal.
This chapter reviews the installation proce
Wrox Programmer to Programmer™
Drupal
Beginning
®
Drupal
Jacob Redding
www.it-ebooks.info
Programmer to Programmer™
Get more out of
wrox.com
Interact
Join the Community
Take an active role online by participating in our
P2P forums @ p2p.wrox.com
Sign up for our free monthly newsletter at
newsletter.wrox.com
Wrox Online Library
Browse
Hundreds of our books are available online
through Books24x7.com
Ready for more Wrox? We have books and
e-books available on .NET, SQL Server, Java,
XML, Visual Basic, C#/ C++, and much more!
Wrox Blox
Download short informational pieces and code
to keep you up to date and out of trouble!
Contact Us.
We always like to get feedback from our readers. Have a book idea?
Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com
www.it-ebooks.info
BEGINNING DRUPAL®
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1
Introducing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2
Installing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
CHAPTER 3
Your First Drupal Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
CHAPTER 4
Administration — Configuration, Modules, and Reporting . . . . . . . . . . . 49
CHAPTER 5
Administration — Blocks, Menus, and Themes . . . . . . . . . . . . . . . . . . . . 69
CHAPTER 6
Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
CHAPTER 7
User Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
CHAPTER 8
Taxonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
CHAPTER 9
Search, Performance, Statistics, and Reporting . . . . . . . . . . . . . . . . . . . 173
CHAPTER 10
Triggers, Actions, Workflow, and Rules . . . . . . . . . . . . . . . . . . . . . . . . . . 189
CHAPTER 11
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
CHAPTER 12
Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
CHAPTER 13
Theming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
CHAPTER 14
Contributed Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 15
Custom Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
CHAPTER 16
Development Hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
CHAPTER 17
Module Development Start to Finish . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
CHAPTER 18
Advanced Drupal Installations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
CHAPTER 19
Preflight Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
APPENDIX
Exercises and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
www.it-ebooks.info
www.it-ebooks.info
BEGINNING
Drupal®
Jacob Redding
www.it-ebooks.info
Beginning Drupal®
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2010 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-43852-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 7486008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available
in electronic books.
Library of Congress Control Number: 2009930281
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other
countries, and may not be used without written permission. Drupal is a registered trademark of Dries Buytaert. All other
trademarks are the property of their respective owners. Wiley Publishing, Inc. is not associated with any product or vendor mentioned in this book.
www.it-ebooks.info
This book is first and foremost dedicated to the
Drupal community, a melting pot of some of the
most talented and intelligent people I have had the
pleasure of working with. Equally important is Diana
Kuan. She stuck by me while I wrote this book, pulled
incredible hours with the Drupal Association, traveled
to countless Drupal events and went crazy trying to
keep up with the intense development of Drupal 7.
Thank you Diana.
www.it-ebooks.info
CREDITS
EXECUTIVE EDITOR
Carol Long
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
PROJECT EDITOR
Ed Connor
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Barry Pruett
TECHNICAL EDITOR
Joshua Brauer
ASSOCIATE PUBLISHER
Jim Minatel
PRODUCTION EDITOR
Kathleen Wisor
PROJECT COORDINATOR, COVER
Lynsey Stanford
COPY EDITOR
Kathi Duggan
COMPOSITOR
Jeff Lytle, Happenstance Type-O-Rama
EDITORIAL DIRECTOR
Robyn B. Siesky
PROOFREADER
Carrie Hunter, Word One, New York
EDITORIAL MANAGER
Mary Beth Wakefield
INDEXER
Robert Swanson
MARKETING MANAGER
Ashley Zurcher
COVER DESIGNER
Michael E. Trent
PRODUCTION MANAGER
Tim Tate
COVER IMAGE
© Jim Jurica/istockphoto
www.it-ebooks.info
ABOUT THE AUTHOR
JACOB REDDING got his start in Drupal in 2005 after 10 years in the IT
industry building custom software with everything from Microsoft to Linux
and UNIX technologies. He has since become heavily involved in the Drupal
association and Drupal communities around the world, speaking and leading
classes in not only New York, San Francisco, Boston, and Washington DC,
but also far-flung locales such as China and Jordan. An enthusiastic advocate
of open-source, he currently spends most of his time providing project management and training on Drupal-based websites.
www.it-ebooks.info
ACKNOWLEDGMENTS
DIANA KUAN DESERVES the top spot here. Serving as a part-time editor, proofreader, supporter,
and impromptu student she helped set the flow of the book, proofread nearly every page, and tested
many of the exercises. Without her assistance and support this book would have taken yet another
year, an outcome that would have surely sent the publisher’s henchmen out to get me.
Jason Chinn, Mr. Magicspark, helped me through the rough spots of theming including building the
nice Drupal chocolate theme used in Chapter 13. Without him the theming chapter would have been
a rambling mess.
My students: To those in China: ԴӀᐂ៥ᕜ៥ᛇ䇈䴲ᐌᛳ䇶ԴӀDŽ ៥Ꮰᳯ៥Ӏৃҹᴹ㾕䴶DŽ My
students in Jordan caught me during a critical time of this book and unknowingly tested several of
the exercises in the book, thank you for secretly helping to create this book. I can’t leave out those
in my home country of the United States, thank you for listening to me ramble about in class. I hope
that I have been a useful part of your Drupal journeys.
Joshua Brauer: My technical editor. Although we have never met and I’m not entirely sure how we were
paired together, the odd in-book synergy somehow worked. I was able to see through his trying-to-benice commentary that brow beat me into thoroughly revising the chapters into something useable.
Carol Long and Ed Connor at Wiley/Wrox: Oh boy. These two had no idea what they signed up for.
The book started on very early versions of Drupal 7 and was revised too many times for them to
count. Their patience and tolerance of my “I’ll get it to you soon-ish” (heavy emphasis on –ish) and
“by the way Drupal 7 changed so I have to rewrite the entire chapter” is much appreciated.
The entire Drupal community: You gals and guys are amazing. From small meetups in Beijing,
large DrupalCamps in Los Angeles, collaborative working sessions in New York and the awesome
DrupalChix group, you (the Drupal community) are amazing individuals to know and to collaborate
with. With the book fi nally complete I hope to see you all in IRC more often.
www.it-ebooks.info
CONTENTS
INTRODUCTION
xxi
CHAPTER 1: INTRODUCING DRUPAL
History of Drupal
1
2
Why is the Name Drupal?
Who is Drupal?
2
2
The Drupal Community
Where Is the Community?
Professional Support
The Drupal Association
Summary
2
3
4
5
6
CHAPTER 2: INSTALLING DRUPAL
Getting Started with Drupal
Installing a Single Drupal Website
Installing Drupal
Installation Profiles
Installing Drupal in Different Languages
Acquia’s Stack Installer
9
10
10
13
16
16
16
Breaking It Down
Don’t Hack Core
Core Files
Sites Folder
What’s in a Site’s Folder?
Inheritance and Overrides
19
19
20
21
21
22
Summary
24
CHAPTER 3: YOUR FIRST DRUPAL WEBSITE
Adding Content
27
27
Differences between Articles and Pages
Content Summaries
Modifying the Defaults
Modifying Your Menus
Adding Blocks to Your Website
Getting in Contact
Adding the Contact Form to Your Main Menu
Summing Up the Contact Form
www.it-ebooks.info
29
30
30
31
33
34
36
37
CONTENTS
Exploring Your Site’s Permissions
Creating a Members-Only Site
Exploring Roles
Wrapping up Users and Permissions
Time for a New Look
38
39
39
43
43
Time for Something Different
Cron
44
45
Manually? No Way! Let the Computer Do It
A Need for Speed
Summary
46
46
47
CHAPTER 4: ADMINISTRATION — CONFIGURATION, MODULES, AND
REPORTING
Configuration
System — Site Information
System — Actions
Regional and Language — Settings
Regional and Language — Translate
Media
Media — File System
Temporary Directory
PHP File Size Limitations
Media — Image Toolkit
People and Permissions
Web Services
Web Services — RSS Publishing
Web Services — Feed Aggregator
Content Authoring — Text Formats
Explore the Filtered HTML Text Format
Modify Text
Add Text
Remove Text
Interpreting Text
Choosing Roles and the Importance of the Order
Configuring the Filters
The Order Matters
Additional Filters
Writing Your Own Filter
Search and Metadata - Clean URLs
Apache
A Need for Speed
49
50
51
53
53
54
54
55
56
56
57
57
57
58
58
58
59
59
59
59
60
60
61
61
61
62
62
63
63
x
www.it-ebooks.info
CONTENTS
IIS
64
Search and Metadata — URL Aliases
Alias Your Way to a Better Search Ranking
Maintenance Mode
IP Address Blocking
Summary
64
65
65
66
67
CHAPTER 5: ADMINISTRATION — BLOCKS, MENUS, AND THEMES
Blocks
69
70
Block Visibility
Block Visibility Using PHP
73
75
Menus
77
Adding a Menu Link Directly on Content
A Word On Permissions
Menu Settings
Related Modules
URL Aliases AKA Custom Paths
A Word on Permissions
80
80
80
81
81
83
Themes
83
Theme Administration
Theme Global Settings
Theme Settings Summary
Installing New Themes
Administrative Theme
Post Information
83
84
85
85
86
87
Summary
87
CHAPTER 6: CONTENT
91
Nodes
92
What is a Node?
92
Creating Custom Content Types
Submission Form Settings
Publishing Options
Display Settings
Comment Settings
Menu Settings
93
95
96
97
97
97
Adding an Image Upload Field
Multiple Fields for Multiple Images?
The Power of Image Field
Permissions
98
99
99
104
Revisions
105
xi
www.it-ebooks.info
CONTENTS
Digging Deeper Into Fields
Fields
Field Types (Type of Data to Store)
Widgets (Form Element)
Allowed Values List
Content Construction Kit (CCK)
Views
Administering Nodes
Content Moderation
Quick and Easy Moderation
Nodes in a Nutshell
Comments
Permissions
106
106
107
107
109
109
110
110
111
111
111
112
113
Comment Administration
Comment Administration Page
Comment Approval Queue (A.K.A Comment Moderation)
Comment Form
Allowing Rich Text Comments
WYSIWYG
Spam
RSS Aggregation
113
114
114
116
116
117
117
118
Displaying the Feed
Configuring the Aggregation Settings
Summary
120
121
122
CHAPTER 7: USER MANAGEMENT
Creating User Accounts
125
126
People
Anonymous Users
126
126
Creating Accounts
126
Approving Accounts in Moderation
Setting a Password During Registration
Canceling Accounts
Setting Up Account E-mails
Using OpenID
Creating Roles and Permissions
Administrator Role
128
129
129
129
130
132
135
Creating Custom User Profiles
135
Profiles in Drupal 7
User Profiles vs. User Fields
Using Fields for Profiles
135
136
136
xii
www.it-ebooks.info
CONTENTS
Using the Core Profile Module
Controlling Access to User Profiles
Hidden Fields
Visibility Options Summarized
Tracking User Activity
137
140
142
143
143
Using Views to Create Custom Tracking Pages and Blocks
Custom Administration Pages with Views
User Sessions
Forcing Sessions to Expire
144
150
153
154
Summary
155
CHAPTER 8: TAXONOMY
157
In Simple Terms
Terminology
Vocabularies
Terms
157
158
158
159
Advanced Options
159
Using Terms on Your Content
Taxonomy Term Field Settings
A Term’s Parent/Child Relationship
Using Views to Display Term Pages
Display Multiple Terms on a Single Page
API
Theming Considerations
160
162
164
165
167
168
168
Customizing Each Section
168
Useful Contributed Modules
Summary
169
170
CHAPTER 9: SEARCH, PERFORMANCE, STATISTICS, AND REPORTING 17 3
Search
174
Indexing Your Content
Advanced Search Settings
Indexing Settings
CJK Handling
Content Ranking
Faceted Search
Outsourcing Search
If You Don’t Want to Outsource
Performance
175
176
176
177
177
177
178
178
178
Caching
Cache Blocks
178
179
xiii
www.it-ebooks.info
CONTENTS
Page Cache for Anonymous Users
Minimum Cache Lifetime
Bandwidth Optimization
Clearing Cached Data
The Need for Speed
Reporting
179
179
180
180
181
182
Status Report
Recent Log Entries
183
183
Statistics
184
Access Log
Content Views
Performance Considerations
Summary
185
185
185
186
CHAPTER 10: TRIGGERS, ACTIONS, WORKFLOW, AND RULES
Triggers and Actions
189
190
Working with Actions
Working with Triggers
Next Steps
190
191
193
Workflow
Rules
Features of the Rules Module
Conditions
Scheduler and Rule Sets
Input Evaluators
Summary
CHAPTER 11: VIEWS
193
200
201
203
206
209
210
213
Advanced Help
Overview of Views
The Frontpage View
214
214
215
Explore the Frontpage View
Displays
Style Settings
Creating a Photo Gallery
Arguments
Exposed Filters
215
218
220
222
225
229
Exposed Items in a Block
Making Your View Accessible via a Tab
Relationships
231
232
234
xiv
www.it-ebooks.info
CONTENTS
Permissions
235
All-Views Permissions
Per-View Permissions
236
236
Caching (AKA Performance)
236
Caching
Block Caching
Best Practices
236
237
237
Advanced Views
Summary
238
238
CHAPTER 12: INTERNATIONALIZATION
A Bit of Terminology
Localizing
241
241
242
Regional Settings
Date and Time
Localizing Your Site’s Interface
Your Site’s Primary Language
Language Negotiation
Completing Your Site’s Localization
Making Translation Easy with the L10N Client
Translating Your Content
Beyond the Basics
Summary
242
243
243
249
249
252
253
258
260
261
CHAPTER 13: THEMING
263
Administering Themes
264
Installing New Themes
Configuring Your Themes
264
265
Custom Theming
266
Template Files
Theme Functions
Reviewing the Last Five Exercises
Sub-Themes
270
274
279
280
Zen Base Theme
282
.info File
282
Removing Theme Settings
Style Sheets
JavaScript
Regions
282
283
284
285
Template Files (.tpl.php)
286
page.tpl.php
node.tpl.php
286
287
xv
www.it-ebooks.info
CONTENTS
comment.tpl.php
block.tpl.php
Template Suggestions
287
287
288
Preprocess Functions
288
Adding New Variables
Template Suggestions
289
289
Swapping Theme Functions and Template Files
Moving a Theme Function to a Template File
Moving a Template File to a Theme Function
Theme Registry
Theme Engines
Summary
290
290
290
291
294
296
CHAPTER 14: CONTRIBUTED MODULES
Contributed and Custom Modules
Determining Your Needs
Fulfilling Your Needs
Lullabot Podcast
Top Drupal Modules
Showcases
Planet Drupal
299
299
300
301
301
302
302
303
Finding Modules
303
Search Drupal.org
Drupalmodules.com
IRC and the Drupal Community
RSS Feeds
Is It the Right Module?
303
304
304
305
305
305
306
Project Page
Coder Module
Tests
306
309
309
309
Be a Follower
309
Take It Slow
Set Up a Development Environment
Avoid the Buffet
Summary
310
311
311
312
xvi
www.it-ebooks.info
CONTENTS
CHAPTER 15: CUSTOM MODULES
What Is a Module?
Hooks
315
315
317
More Information
319
Modules Step by Step
319
Step 1: Choose a Name
Step 2: Create the .info File
Step 3: Create the .module File
Creating a Page
319
320
321
321
Capturing Custom URLs with hook_menu
Modifying Existing Modules
322
324
Patching
Creating a Patch
The Value of the Patch
When to Modify
325
325
327
327
Summary
327
CHAPTER 16: DEVELOPMENT HOOKS
331
Hooks: A Module’s Starting Point
Community Documentation
The Hooks
332
332
333
hook_menu
333
Making Your Modules Themeable
The t() Function
Theme Functions and Template Files
Summary
339
340
341
344
CHAPTER 17: MODULE DEVELOPMENT START TO FINISH
The Module
347
348
Beginning Your Module
Implementing a hook
349
350
Adding Custom Configuration Settings
Custom Module Settings
How Did it Become an Option at Configuration?
How Did the Settings Form Appear?
Where Are the Settings Saved?
Filtering on Multiple Words
A Quick Review of the New PHP Code
351
352
353
354
354
355
356
xvii
www.it-ebooks.info
CONTENTS
Helping Your Users
Permission Handling
Storing Data in a Custom Table
356
358
359
Database Abstraction Layer
360
Drupal Query Functions
Displaying the Results on the User’s Profile
Database Summary
Creating a Bulletproof Site with SimpleTest
Test Driven Development
How SimpleTest Performs Its Testing
Writing Your Own SimpleTest
SimpleTest Assertions
Adding the Tests to Your Module
Summing Up SimpleTest
Summary
361
365
367
367
367
369
369
371
373
375
375
CHAPTER 18: ADVANCED DRUPAL INSTALLATIONS
Drush
379
380
But Wait There’s More!
381
Using CVS to Manage Drupal
What is CVS?
What is SVN?
Installing CVS
Installing and Upgrading Drupal using CVS
Understanding CVS Tags
How Does This Relate to CVS?
Branches and Tags
Installing Modules from the CVS Repository
One Drupal, Many Websites
Example 1: Multiple Independent Websites
Example 2: Multiple Related Websites
Sites.php
Settings.php
382
382
382
383
383
385
385
386
386
387
388
389
392
392
$databases
Scalability
$db_prefix
$baseurl
$ini_set
$reverse_proxy
393
394
394
395
395
396
xviii
www.it-ebooks.info
CONTENTS
Variable Overrides
String Overrides
IP Blocking
396
396
397
Summary
397
CHAPTER 19: PREFLIGHT CHECKLIST
40 1
Preflight Checklist
402
Site Information
User Settings
Site Maintenance
SPAM
SEO
Search
Performance
Disaster Recovery
402
402
402
403
403
403
404
404
SEO
404
SEO Best Practices
The Importance of URLs
XML Sitemap
Submitting Your Sitemap to Search Engines
Analyzing Your Site with Google
Dashboard
Summary
405
405
405
408
411
413
415
APPENDIX: EXERCISES AND ANSWERS
417
INDEX
433
xix
www.it-ebooks.info
www.it-ebooks.info
INTRODUCTION
OVER THE PAST FEW YEARS, the Web has undergone dramatic changes. What originally started as a
handful of static web pages with a few e-commerce sites in the mix has now turned into a frenzy of
collaboration and information sharing. New websites are expected to meet a certain level of interactivity and information-sharing. RSS feeds, blogs, and podcasts are just a few of the current standards. Photo uploads with auto-resizing and cropping, video and audio embedding, and even web
APIs are quickly becoming the new standard-website functions.
Years ago, when I started creating dynamic websites, I began as most developers did, by writing my
own custom content management system (CMS). My experience was not unlike that of others who
followed the same path. The system was light and fast, and did everything that I, and my clients,
asked — until a new feature emerged on the Web, and my software had to play catch-up. Soon I was
buried under a pile of feature requests and bug fi xes, which left me with no time to innovate. My quest
for a better solution led me through a maze of software packages, until a friend told me about Drupal.
According to him Drupal has two really great things going for it. The fi rst is that it’s built on a modular structure, so it can quickly adapt to custom and unique scenarios. The second is that it is built
around a community that is growing every day. When a new feature emerges on the Web, chances
are someone has already gotten it to work within Drupal. The community factor is what sold me.
Now that I have been working with Drupal for a few years, I am in awe at how well the community
has organized itself and, as a result, kept itself and the Drupal project on the cutting edge of web
technology. Drupal has moved from being a piece of software that modularized the latest Internet
technologies, to a platform that creates the latest technologies. The community made this happen.
You are now a part of this awesome community. Welcome.
Who This Book Is For
This book was written for people who want a robust website and aren’t afraid to get their hands dirty.
Maybe you’re the leader of a nonprofit who wants to organize your constituency through a
community-driven website. You have a bit of prior experience in creating HTML pages by hand
or with another CMS. You’re now ready to dig in and create your community’s website, and
Drupal is your tool of choice, but you want to be on the fast track.
Perhaps you’re an established developer and possess the skills to create your own custom CMS
or have experience with other software packages. You’re looking for defi nitions and examples on
Drupal’s terminology and structure so that you can take advantage of Drupal’s fast development
cycle that you hear so much about.
To get the most out of this book, you should already possess or have a strong desire to obtain experience in web development with HTML, JavaScript, or PHP. I did not set out to write a book that
guides you step-by-step through building a specific website such as one for a restaurant or bakery.
Instead, I set out to write a book that will help you understand how you can use Drupal to take the
www.it-ebooks.info
INTRODUCTION
vision in your head and apply it to the Web. For example, when you understand that you need a
new form of content, fl ip to Chapter 6, "Content," which discusses all aspects of content in depth,
including creating, administering, moderating, and controlling that content.
Experienced developers who are looking to propel themselves on the fast track to custom module development might fi nd it useful to skim Chapters 2 through 4, and then skip forward to the
later chapters on module development. I would suggest that you spend a bit of time on Chapter 6,
"Content," and Chapter 10, "Workflow and Actions," and Chapter 11, "Views." A good understanding of these chapters may save you countless development hours.
What This Book Covers
This book focuses on Drupal 7, but the changes since Drupal 6 are highlighted where appropriate. This book walks you through the entire process of creating a Drupal website. It starts with the
installation, moves to administration and configuration, and then covers users and permissions. The
book also discusses content (including nodes, users, and blocks), Drupal’s infamous taxonomy system, tips on how to make your site not look like a Drupal site, and instructions on expanding your
site with contributed and/or custom modules.
How This Book Is Structured
I often train others on how to use Drupal, so this book is structured similar to a training manual.
The chapters are ordered in a manner that builds upon the topics of the previous chapter. For
example, the chapter on Views is after the chapters on users, content, and taxonomy, because a
view works with all of these items. I believe this order will help you understand the overall flow and
structure of Drupal. Each chapter, however, is a thorough examination of its topic.
I don’t expect you to read the book cover to cover before building your website. You should read
this book alongside your development and use it as an aid or reference.
If you are new to Drupal, I recommend that you read at least the fi rst parts of each chapter, but you
can move to the next chapter if the topic becomes too complex or just plain boring. You can then
revisit each chapter when you need clarification or more information.
Experienced developers or those with previous Drupal experience may fi nd it beneficial to skip to
the chapters that matter most to them. Each chapter is designed to be independent, although the
examples in one chapter may build upon the examples in the previous chapter. If you fi nd this to be
the case, simply fl ip back one chapter and follow the last example to catch up.
What You Need to Use This Book
The book covers Drupal version 7, which has the following requirements:
➤
Webserver: Apache 1.3 or 2.x or IIS 6+
➤
Database server: MySQL 5+ or Postgres 8.3+
➤
PHP 5.2 or higher
xxii
www.it-ebooks.info
INTRODUCTION
The XAMPP project for Microsoft Windows, Linux, and Mac OS X as well as the MAMP project
for Mac OS X contain all of these requirements in a single downloadable package.
Please visit http://drupal.org/requirements for up-to-date system requirements.
CONVENTIONS
To help you get the most from the text and keep track of what’s happening, I’ve used a number of
conventions throughout the book.
Exercises that you can try out for yourself generally appear in a box like this:
TRY IT OUT
What You Will Be Doing in this Exercise
Each Try It Out consists of an exercise you should work through, following the text in the book.
1.
2.
3.
They usually consist of a set of steps.
Each step has a number.
Follow the steps using your copy of the database.
How It Works
After each Try It Out, the code you’ve typed will be explained in detail.
WARNING Boxes like this one hold important, not-to-be forgotten information
that is directly relevant to the surrounding text.
NOTE Notes, tips, hints, tricks, and asides to the current discussion are off set
and placed in italics like this.
This book also uses the following styles to set apart non-standard text:
➤
New and important terms are italicized.
➤
A combination of two or more keyboard strokes is presented like this: Ctrl+A.
➤
File names, URLs, and code within the text are shown in a monofont, like so: persistence.
properties.
xxiii
www.it-ebooks.info
INTRODUCTION
➤
Separate lines of code are formatted in the following ways:
Monofont type with no highlighting is used for most code examples.
Bold is used to emphasize code that’s particularly important in the present
context.
SOURCE CODE
As you work through the examples in this book, you may choose either to type in all the code manually, or to use the source code fi les that accompany the book. All the source code used in this book
is available for download at http://www.wrox.com. When at the site, simply locate the book’s title
(use the Search box or one of the title lists) and click the Download Code link on the book’s detail
page to obtain all the source code for the book. Code that is included on the Web site is highlighted
by the following icon:
Listings include the fi lename in the title. If it is just a code snippet, you’ll fi nd the fi lename in a code
note such as this:
code snippet filename
NOTE Because many books have similar titles, you may find it easiest to search
by ISBN. This book’s ISBN is 978-0-470-43852-7.
Once you download the code, just decompress it with your favorite compression tool. Alternately,
you can go to the main Wrox code download page at http://www.wrox.com/dynamic/books/
download.aspx to see the code available for this book and all other Wrox books.
ERRATA
We make every effort to ensure that there are no errors in the text or in the code. However, no one
is perfect, and mistakes do occur. If you fi nd an error in one of our books, like a spelling mistake or
faulty piece of code, we would be very grateful for your feedback. By sending in errata you may save
another reader hours of frustration and at the same time you will be helping us provide even higher
quality information.
To fi nd the errata page for this book, go to http://www.wrox.com and locate the title using the
Search box or one of the title lists. Then, on the book details page, click the Book Errata link. On
xxiv
www.it-ebooks.info
INTRODUCTION
this page you can view all errata that has been submitted for this book and posted by Wrox editors. A complete book list including links to each book’s errata is also available at www.wrox.com/
misc-pages/booklist.shtml.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the
information and, if appropriate, post a message to the book’s errata page and fi x the problem in subsequent editions of the book.
P2P.WROX.COM
For author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based
system for you to post messages relating to Wrox books and related technologies and interact with
other readers and technology users. The forums offer a subscription feature to e-mail you topics
of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other
industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com you will fi nd a number of different forums that will help you not only as
you read this book, but also as you develop your own applications. To join the forums, just follow
these steps:
1.
2.
3.
4.
Go to p2p.wrox.com and click the Register link.
Read the terms of use and click Agree.
Complete the required information to join as well as any optional information you wish to
provide and click Submit.
You will receive an e-mail with information describing how to verify your account and complete the joining process.
NOTE You can read messages in the forums without joining P2P but in order to
post your own messages, you must join.
When you join, you can post new messages and respond to messages other users post. You can read
messages at any time on the Web. If you would like to have new messages from a particular forum
e-mailed to you, click the Subscribe to this Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to
questions about how the forum software works as well as many common questions specific to P2P
and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
xxv
www.it-ebooks.info
www.it-ebooks.info
1
Introducing Drupal
WHAT YOU WILL LEARN IN THIS CHAPTER:
➤
History of Drupal
➤
Drupal’s community
➤
How to find and participate in the community, including: IRC,
Meetups, Camps, and Drupal conferences
➤
Professional Drupal support
➤
The Drupal Association
What is Drupal? If this is your fi rst time using Drupal, you’re probably trying to answer this
yourself, and you may have heard confl icting answers. Often Drupal is described as a content
management system, whereas other times, it’s called a content management framework. So is
it a system or a framework? Here’s a simple defi nition:
Drupal is an open source software application that manages the content of
and builds websites and web applications. It can be used to create a web
blog, e-commerce store, photo gallery, or social networking website.
That seems straightforward, right? In fact, it sounds a lot like Joomla!, Wordpress, Zope,
and many other content management systems (CMSs). So why do people use the term content
management platform or content management framework?
The use of the terms platform and framework is an attempt to describe Drupal as a base or a
foundation onto which you build your desired websites or web applications. Out of the box,
Drupal can be used to instantly create a website with users, articles, blogs, comments, and a
forum. With a few modules from http://drupal.org it can be transformed into an e-commerce
site, a group photo gallery, and more. Drupal is also built around a modular core and a strong
www.it-ebooks.info
2
❘
CHAPTER 1 INTRODUCING DRUPAL
application programming interface (API) so you can quickly and easily extend Drupal to build the site
of your dreams. This leads to the following, more technical definition:
Drupal is a modular framework written in the PHP scripting language that
contains a CMS, a module system, and an API for rapid development of websites
and web applications.
An example I’ve used often is Lego systems. You can purchase the race car Lego set and build
the provided example race car. You could also build a luxury car with the same Lego set. If you
purchase the rocket ship Lego set, you could combine the two and build a luxury rocket race car.
After all, it’s only a set of blocks that you put together using your imagination. Drupal is built on
the same idea — it’s a set of modules that you mix and match to build your perfect website or web
application.
HISTORY OF DRUPAL
In 2000, Dries Buytaert, a student at the University of Antwerp, needed a method to communicate
with his classmates and friends. He built a small web application so that he and his friends could
leave notes for each other. After Dries graduated he moved the software over to drop.org and used
it to experiment with new web technologies such as syndication, rating, and distributed authentication. Dries also made the software freely available and licensed it under the GPL. It didn’t take long
before the software’s unique modular structure gained notice and attracted a community of developers and users.
Why is the Name Drupal?
When searching for domain names Dries accidentally misspelled the Dutch word dorp (meaning village) as drop. Accepting his mistake he registered and started to use the drop.org domain. In 2001
the project officially adopted the name Drupal. The Dutch word for drop is druppel, which changed
to Drupal (pronounced “droo-puhl”) for easier pronunciation.
Who is Drupal?
As of this book’s writing, thousands of developers have contributed to the Drupal project. Many
have contributed by adding modules and themes, providing support on drupal.org and the Internet
Relay Chat (IRC) channels, and participating at meetups, DrupalCamps, and conferences around
the world. Drupal has been recorded as having a developer base in almost every country in the
world. It truly is an international open source project.
THE DRUPAL COMMUNITY
Drupal is not just software — it’s a community! The Drupal community is one of the largest and
most supportive communities in the open source world. In fact, the community is so strong that
it should be a major deciding factor when you’re choosing whether or not to use Drupal. Imagine
building a site that requires a certain piece of functionality. You found a module that does 90
www.it-ebooks.info
Where Is the Community?
percent of the work but isn’t quite right. You can decide to code this functionality on your own or
you could work with the current module owner to modify and build in the functionality you need.
If you code it yourself, you will have to maintain it in perpetuity, but if you work with the current
module owner and the community, you are sharing maintenance with the community. The latter
result is a more sustainable, longer lasting website and web application.
There is no formal agreement with the community. The agreement is much more tacit. A user providing support one day may expect to receive support another day. Likewise, other module developers will maintain their module if you maintain yours, so that everyone’s site can grow and expand.
You do not have to be a developer to participate in the community. There are groups of designers,
users, administrators, and others at all levels working together to build great sites. The worst thing
you can do when starting out with Drupal is to ignore the community.
WHERE IS THE COMMUNITY?
The Drupal community meets online and, better yet, in the real world. Here are a few places where
you can fi nd them:
➤
Forums on Drupal.org (http://drupal.org/forum)
Installation, configuration, migration, translation, upgrading, and module development are
just some of the topics covered in the forums. Forums are a great place to start when first
learning about Drupal and its community.
➤
IRC channels
Because it functions in real time, IRC can provide the fastest support, communication, and
collaboration. IRC can be accessed using IRC software such as Mirc (Windows), Colloquy
(Mac OS X), or xChat (Linux). The server is irc.freenode.net and the top three channels
are (note that many more exist):
➤
#drupal-support — Provides general support for installation, configuration, and
site administration. When starting out with Drupal this is the channel to use.
➤
#drupal-themes — Provides support for the creation and modification of Drupal
themes (most often on custom themes).
➤
#drupal — Hosts high-level development discussions on Drupal core and modules.
Once you have used Drupal for a while and are developing modules, use this channel
to collaborate with other developers.
You can fi nd more channels and the rules to IRC etiquette at http://drupal.org/irc.
➤
Drupal Groups at http://groups.drupal.org
The Drupal Groups website is the Grand Central station of the Drupal community. Likeminded users and developers meet and collaborate on common topics and projects on this
website. Regional groups from New York City, Paris, Wisconsin, Peru, and Texas schedule
regular meetups. Topic-based groups such as High Performance and Drupal in Education
are great for fi nding others who are in similar situations as you. At the time of this writing,
there were over 360 active groups. Find a local group and get involved!
www.it-ebooks.info
❘3
4
❘
CHAPTER 1 INTRODUCING DRUPAL
➤
Regional and language-based websites
There are several regional websites that enable users to communicate in their local language
or connect within a geographic region. Examples include drupalchina.org, drupaltaiwan.org, drupalitalia.org, and ladrupal.org. Language-based communities can be
found at http://drupal.org/language-specific-communities, and geographic communities can be found at http://groups.drupal.org/groups.
➤
Camps and conferences
Drupalcon brings together developers, designers, users, and other Drupal professionals from
all over the world. Currently Drupalcon is held once a year in Europe and North America
but is quickly expanding to other parts of the world. Check http://drupalcon.org for the
latest event schedule.
Drupalcon may be the biggest Drupal-based conference, but it is not the only one.
DrupalCamps, meetups, and other community-organized events happen daily around the
world. Check http://groups.drupal.org/events for the most recent events.
PROFESSIONAL SUPPORT
The Drupal community forums and IRC channels are filled with wonderful volunteers working day
and night to help each other build and troubleshoot their websites. Although working with the community is the best way to learn Drupal quickly, you may or may not have your questions answered
or problems fi xed in a timely manner; these channels can require patience. Fortunately the companies of the Drupal community are there to help you.
Drupal is a software application that runs on a server platform, meaning a host. For this reason support packages are divided into three general categories: Drupal-only, server-only, Drupal + server.
Many hosting companies provide server-only support and do not support the Drupal software,
whereas Drupal professionals may provide Drupal-only support. When purchasing support it is best
to ask what is and what is not supported to fi nd the right combination for your project and budget.
You can fi nd a list of hosting companies that specialize in Drupal at http://drupal.org/hosting
and a list of companies providing support, consulting, and development at http://drupal.org/
services. To help get you started here a few of the more popular options:
➤
Acquia Drupal
Created by the founder of Drupal, Dries Buytaert, this company provides a distribution of
Drupal that bundles some of the most popular contributed modules and a custom Acquia
module to connect your website to Acquia’s automated monitoring and support network.
Because Acquia’s distribution is built on top of Drupal, it is 100 percent compatible with all
of Drupal’s modules, themes, translations and other items, including every exercise in this
book. In fact, you can use Acquia’s Drupal distribution with this book.
Acquia Drupal can be found at http://acquia.com.
➤
Hosted Drupal solutions
A hosted Drupal solution is different from a regular host. A hosted Drupal solution is a host
that installs, configures, and fi ne-tunes Drupal for you. Often these companies will continually monitor your installation for updates to Drupal and its modules. Although Drupal is
www.it-ebooks.info
The Drupal Association
❘5
compatible with many hosts the difference is the specialty in Drupal, which you’ll appreciate
when your site goes down in the middle of the night. Many other Drupal companies will
only host sites they build or help you build. But a hosted Drupal solution takes your site,
your code, and your system and makes sure it’s rock-solid.
Check out the following hosted Drupal solutions:
➤
Hot Drupal (http://hotdrupal.com)
➤
Workhabit (http://workhabit.com)
➤
Acquia Drupal (http://acquia.com)
For high availability, high traffic websites check out Acquia, Four Kitchen’s Pressflow Drupal
distribution http://pressflow.org, Pantheon, an EC2/Cloud computing distribution
(http://getpantheon.com), or Firehose at http://stationindustries.com.
You can browse many more hosted Drupal solutions or Drupal-compatible web hosts at
http://drupal.org/hosting.
➤
Drupal professionals
The Drupal community is teeming with dedicated professionals that are ready to help you
build your website, provide a bit of custom development, or give you advice. Companies
exist for customers with nearly any budget, from the hobbyist to the Fortune 500 enterprise
customer. If you choose to hire a professional be sure to inquire about their launch and ongoing support options.
You can browse a list of Drupal professionals at http://drupal.org/services.
Confused? Don’t be. The community is there to help you. If you have questions simply log into IRC
channel #drupal-support, ask your question in the Drupal forums at http://drupal.org/forums, or
pick up your phone and talk to one of the many Drupal professionals listed at http://drupal.org/
services.
Of course you don’t need to hire a professional to build, support, or even host your website. Keep
reading to become your own Drupal professional.
THE DRUPAL ASSOCIATION
Standing to the side of the community and directed to support and protect your rights as a community member is the Drupal Association. Originally created to purchase hardware to support
the drupal.org infrastructure, the Association quickly became a defender of the Drupal trademark
from an erroneous claim against it. This all community- and volunteer-run nonprofit works to
ensure that Drupal continues to be free and available for everyone to enjoy. A few of the activities
that the Association performs are:
➤
Purchasing infrastructure (servers, etc.) that the drupal.org websites use
➤
Protecting the Drupal trademark
➤
Defending Drupal against GPL infringements
➤
Working with the community to organize the annual Drupalcon conference
www.it-ebooks.info
6
❘
CHAPTER 1 INTRODUCING DRUPAL
➤
Redesigning and creating functional upgrades to the drupal.org website
➤
And many other supporting activities!
The Association does not write or maintain the Drupal software or control the content on any
of the drupal.org websites. The Drupal project is solely owned and run by the community. As
a community-run organization all users are able and encouraged to participate in the Drupal
Association to help Drupal continue as one of the greatest open source projects!
The Drupal Association can be found at http://association.drupal.org.
SUMMARY
It should be clear that Drupal is much more than great software; it is also a great community. The
Drupal software, built upon a modular architecture, allows you to build great websites with relative
ease. And the community of users, designers, and developers, as well as great Drupal companies, are
all there to help you support your website both now and in the future. You need no special skills or
invitation to join the Drupal community, just a bit of time to participate in it. Drupal is more than
software. Drupal is you!
Welcome to the community!
In the next chapter you’ll explore Drupal’s installation process and begin your Drupal journey.
EXERCISES
1.
What website can you use to connect with other Drupal users, developers, and professionals with
similar interests or located near you?
2.
Where can you communicate with other Drupal users or developers in real time over the Internet?
3.
What nonprofit organization helps to protect the Drupal community’s code from GPL copyright
infringement, protects the Drupal trademark, and supports the http://drupal.org infrastructure?
4.
Drupal is a great CMS, but developers also love it because of its great ____? (three letter
acronym)
Answers to the Exercises can be found in the Appendix.
www.it-ebooks.info
Summary
⊲ WHAT YOU LEARNED IN THIS CHAPTER
➤
Drupal is not only a CMS but also a framework and an API for building great websites and
applications.
➤
Drupal’s modular structure allows you to plug in the pieces you need to build the website of
your dreams.
➤
Drupal is pronounced “droo-puhl” and comes from the Dutch word for drop.
➤
Drupal is not just software. It is a living and thriving community of developers, designers,
and users.
➤
The Drupal community meets online in the drupal.org forums and IRC channels as well as
in-person at locations around the world. Find out more at http://groups.drupal.org.
➤
From the hobbyist to the Fortune 500, companies exist at all levels to give you professional
service.
➤
You are now part of the Drupal community!
www.it-ebooks.info
❘7
www.it-ebooks.info
2
Installing Drupal
WHAT YOU WILL LEARN IN THIS CHAPTER:
➤
Installing a Drupal website
➤
Installing Drupal using the Acquia Distribution
➤
Exploring the files, folders and structure of Drupal
➤
Reviewing the sites folder
➤
Exploring inheritance and overrides
➤
Overriding Drupal’s default theme with one of your own
Now that you have decided to use Drupal the next step is to download and install it. In fact
you might have already installed one or more Drupal websites. Even if you have already
installed Drupal, a quick read through this chapter will help to clarify the process and help
you better understand Drupal.
This chapter reviews the installation proce