Wrox Programmer to Programmer™Join the discussion @ p2p.wrox.com
Beginning
Drupal®
Jacob Redding
Redding
$39.99 USA $47.99 CAN
Drupal is an open source framework and content management system used to create web sites that is known for its steep learning curve. This introduction offers you a clear and solid understanding of Drupal so that you can get started programming a Drupal site immediately. Author Jacob Redding walks you through the installation and configuration of a Drupal web site and presents a clear understanding of Drupal’s hook system, theming layer, views, and API. Throughout the book, helpful examples and step-by-step instructions provide you with a strong proficiency in the power and capabilities of Drupal.
Beginning Drupal:
• Addresses ways to manage and moderate comments on your site
• Looks at why Drupal’s node system is the pivotal structure of Drupal
• Explains the basics of the views modules and shows you how to control the output of your content
• Guides you through Drupal’s built-in themes and modifying them to suit your needs
• Examines the power of Drupal’s community and demonstrates how to become engaged with it
• Covers the entire module-development process, including creating custom configuration settings, blocks, and database tables
Jacob Redding is an open source evangelist and technologist, and an active member in the Drupal community. In addition to managing several modules on drupal.org and providing Drupal-related training and consulting, he is also a board member on the Drupal Association.
Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing a structured, tutorial format that will guide you through all the techniques involved.
Web Page Design
Shorten the learning curve and quickly leverage the power of Drupal
wrox.comProgrammer ForumsJoin our Programmer to Programmer forums to ask and answer programming questions about this book, join discussions on the hottest topics in the industry, and connect with fellow programmers from around the world.
Code Downloads Take advantage of free code samples from this book, as well as code samples from hundreds of other books, all ready to use.
Read More Find articles, ebooks, sample chapters and tables of contents for hundreds of books, and more reference resources on programming topics that matter to you.
Drupal
®
Beginning
Related Wrox Books
Beginning HTML, XHTML, CSS, and JavaScriptISBN: 978-0-470-54070-1 This valuable resource makes it easy to learn how to build web sites using the most up-to-date web standards. Whether you’re a beginner eager to start writing your first web page or a more experienced programmer seeking to update your skills, you will find practical introductions to these essential languages, as well as invaluable tips and techniques used by experienced web professionals.
Beginning Joomla! Web Site DevelopmentISBN: 978-0-470-43853-4Written in an approachable and informative writing style, this hands-on book introduces you to Joomla! and gets you started with using this powerful, versatile tool for developing and managing web sites. You’ll gain a clear understanding of Joomla! and learn how to get things done within the Joomla! framework. Using this book, you will be able to harness the power of Joomla! to accomplish the goals you want to achieve with your web site.
Beginning PHP5, Apache, and MySQL Web DevelopmentISBN: 978-0-7645-7966-0This book guides you through the entire process of setting up your own site and walks you through every step, from the installation of PHP, Apache, and MySQL to database management, security, and integration with other technologies. The multi-platform approach addresses installation and usage on both Linux® and Windows®, and two common-themed, reusable web sites are examined. Upon completion of this book, you’ll be able to create well-designed, dynamic web sites using open source tools.
Beginning PHP 5.3ISBN: 978-0-470-41396-8As one of the most popular open source web-programming languages in use today, PHP is an ideal server-side scripting language that connects HTML-based web pages to a backend database for dynamic content. This guide introduces the PHP language and shows you how to write powerful web applications using PHP.
Joomla! Start to Finish: How to Plan, Execute, and Maintain Your Web SiteISBN: 978-0-470-570890This book walks you through the critical steps that must be taken in the planning process prior to establishing a Joomla! site. Joomla! expert Jen Kramer reviews essential questions that need to be asked of a client, discusses technical solutions to a variety of challenges, and explains how a site structure should be organized. Once the groundwork has been laid, you’ll discover how to host and install Joomla!, and upgrade and maintain your Joomla! site.
Leveraging Drupal: Getting Your Site Done RightISBN: 978-0-470-41087-5Drupal allows you to create interactive, media-based, database-driven web sites that become a part of everyday activities and communications. This unique book tackles the challenging task of leveraging Drupal to get a site done right and make that site work for you, based on industry-wide software development best practices.
Professional PHP Design PatternsISBN: 978-0-470-49670-1This book bridges the gap between PHP and older programming languages by applying those tried and tested Design Patterns to native PHP applications. It starts with an introduction to Design Patterns, describes their use and importance, and details where you’ve seen them already. The book continues through a host of Design Patterns with code examples and explanations. Finally, an in-depth case study shows you how to plan your next application using Design Patterns, how to program those patterns in PHP, and how to revise and refactor an existing block of code using Design Patterns from the book.
WordPress 24-Hour TrainerISBN: 978-0-470-55458-6 This unique book-and-video package provides you with beginner-friendly tutorials for building and maintaining a WordPress web site. You’ll discover how to customize WordPress to meet your needs and you’ll see how critical—and simple—it is to expand and update content in order to make your site as effective as possible.
Get more out of wrox.com
Programmer to Programmer™
InteractTake an active role online by participating in our P2P forums @ p2p.wrox.com
Wrox Online LibraryHundreds of our books are available online through Books24x7.com
Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble!
Join the CommunitySign up for our free monthly newsletter at newsletter.wrox.com
BrowseReady for more Wrox? We have books and e-books available on .NET, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more!
Contact Us. We always like to get feedback from our readers. Have a book idea? Need community support? Let us know by e-mailing [email protected]
BEGINNING DRUPAL®
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1 Introducing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2 Installing Drupal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
CHAPTER 3 Your First Drupal Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
CHAPTER 4 Administration — Confi guration, 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, Workfl ow, 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 Prefl ight Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
APPENDIX Exercises and Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
438527ffirs.indd i438527ffirs.indd i 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
438527ffirs.indd ii438527ffirs.indd ii 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
BEGINNING
Drupal®
Jacob Redding
438527ffirs.indd iii438527ffirs.indd iii 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
Beginning Drupal®
Published byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.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) 748-6008, 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 specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or pro-motional 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 pub-lisher 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 ven-dor mentioned in this book.
438527ffirs.indd iv438527ffirs.indd iv 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
This book is fi rst 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.
438527ffirs.indd v438527ffirs.indd v 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
CREDITS
EXECUTIVE EDITOR
Carol Long
PROJECT EDITOR
Ed Connor
TECHNICAL EDITOR
Joshua Brauer
PRODUCTION EDITOR
Kathleen Wisor
COPY EDITOR
Kathi Duggan
EDITORIAL DIRECTOR
Robyn B. Siesky
EDITORIAL MANAGER
Mary Beth Wakefi eld
MARKETING MANAGER
Ashley Zurcher
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Barry Pruett
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Lynsey Stanford
COMPOSITOR
Jeff Lytle, Happenstance Type-O-Rama
PROOFREADER
Carrie Hunter, Word One, New York
INDEXER
Robert Swanson
COVER DESIGNER
Michael E. Trent
COVER IMAGE
© Jim Jurica/istockphoto
438527ffirs.indd vi438527ffirs.indd vi 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
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-fl ung locales such as China and Jordan. An enthusiastic advocate of open-source, he currently spends most of his time providing project man-agement and training on Drupal-based websites.
438527ffirs.indd vii438527ffirs.indd vii 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
ACKNOWLEDGMENTS
DIANA KUAN DESERVES the top spot here. Serving as a part-time editor, proofreader, supporter, and impromptu student she helped set the fl ow 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: 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-be-nice 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.
438527ffirs.indd viii438527ffirs.indd viii 3/11/10 8:19:57 AM3/11/10 8:19:57 AM
CONTENTS
INTRODUCTION xxi
INTRODUCING DRUPAL CHAPTER 1: 1
History of Drupal 2
Why is the Name Drupal? 2
Who is Drupal? 2
The Drupal Community 2
Where Is the Community? 3
Professional Support 4
The Drupal Association 5
Summary 6
INSTALLING DRUPAL CHAPTER 2: 9
Getting Started with Drupal 10
Installing a Single Drupal Website 10
Installing Drupal 13
Installation Profi les 16
Installing Drupal in Diff erent Languages 16
Acquia’s Stack Installer 16
Breaking It Down 19
Don’t Hack Core 19
Core Files 20
Sites Folder 21
What’s in a Site’s Folder? 21
Inheritance and Overrides 22
Summary 24
YOUR FIRST DRUPAL WEBSITE 2CHAPTER 3: 7
Adding Content 27
Diff erences between Articles and Pages 29
Content Summaries 30
Modifying the Defaults 30
Modifying Your Menus 31
Adding Blocks to Your Website 33
Getting in Contact 34
Adding the Contact Form to Your Main Menu 36
Summing Up the Contact Form 37
438527ftoc.indd ix438527ftoc.indd ix 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
x
CONTENTS
Exploring Your Site’s Permissions 38
Creating a Members-Only Site 39
Exploring Roles 39
Wrapping up Users and Permissions 43
Time for a New Look 43
Time for Something Diff erent 44
Cron 45
Manually? No Way! Let the Computer Do It 46
A Need for Speed 46
Summary 47
ADMINISTRATION — CONFIGURATION, MODULES, AND CHAPTER 4: REPORTING 49
Confi guration 50
System — Site Information 51
System — Actions 53
Regional and Language — Settings 53
Regional and Language — Translate 54
Media 54
Media — File System 55
Temporary Directory 56
PHP File Size Limitations 56
Media — Image Toolkit 57
People and Permissions 57
Web Services 57
Web Services — RSS Publishing 58
Web Services — Feed Aggregator 58
Content Authoring — Text Formats 58
Explore the Filtered HTML Text Format 59
Modify Text 59
Add Text 59
Remove Text 59
Interpreting Text 60
Choosing Roles and the Importance of the Order 60
Confi guring the Filters 61
The Order Matters 61
Additional Filters 61
Writing Your Own Filter 62
Search and Metadata - Clean URLs 62
Apache 63
A Need for Speed 63
438527ftoc.indd x438527ftoc.indd x 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xi
CONTENTS
IIS 64
Search and Metadata — URL Aliases 64
Alias Your Way to a Better Search Ranking 65
Maintenance Mode 65
IP Address Blocking 66
Summary 67
ADMINISTRATION — BLOCKS, MENUS, AND THEMES 6CHAPTER 5: 9
Blocks 70
Block Visibility 73
Block Visibility Using PHP 75
Menus 77
Adding a Menu Link Directly on Content 80
A Word On Permissions 80
Menu Settings 80
Related Modules 81
URL Aliases AKA Custom Paths 81
A Word on Permissions 83
Themes 83
Theme Administration 83
Theme Global Settings 84
Theme Settings Summary 85
Installing New Themes 85
Administrative Theme 86
Post Information 87
Summary 87
CONTENT 9CHAPTER 6: 1
Nodes 92
What is a Node? 92
Creating Custom Content Types 93
Submission Form Settings 95
Publishing Options 96
Display Settings 97
Comment Settings 97
Menu Settings 97
Adding an Image Upload Field 98
Multiple Fields for Multiple Images? 99
The Power of Image Field 99
Permissions 104
Revisions 105
438527ftoc.indd xi438527ftoc.indd xi 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xii
CONTENTS
Digging Deeper Into Fields 106
Fields 106
Field Types (Type of Data to Store) 107
Widgets (Form Element) 107
Allowed Values List 109
Content Construction Kit (CCK) 109
Views 110
Administering Nodes 110
Content Moderation 111
Quick and Easy Moderation 111
Nodes in a Nutshell 111
Comments 112
Permissions 113
Comment Administration 113
Comment Administration Page 114
Comment Approval Queue (A.K.A Comment Moderation) 114
Comment Form 116
Allowing Rich Text Comments 116
WYSIWYG 117
Spam 117
RSS Aggregation 118
Displaying the Feed 120
Confi guring the Aggregation Settings 121
Summary 122
USER MANAGEMENT 12CHAPTER 7: 5
Creating User Accounts 126
People 126
Anonymous Users 126
Creating Accounts 126
Approving Accounts in Moderation 128
Setting a Password During Registration 129
Canceling Accounts 129
Setting Up Account E-mails 129
Using OpenID 130
Creating Roles and Permissions 132
Administrator Role 135
Creating Custom User Profi les 135
Profi les in Drupal 7 135
User Profi les vs. User Fields 136
Using Fields for Profi les 136
438527ftoc.indd xii438527ftoc.indd xii 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xiii
CONTENTS
Using the Core Profi le Module 137
Controlling Access to User Profi les 140
Hidden Fields 142
Visibility Options Summarized 143
Tracking User Activity 143
Using Views to Create Custom Tracking Pages and Blocks 144
Custom Administration Pages with Views 150
User Sessions 153
Forcing Sessions to Expire 154
Summary 155
TAXONOMY 15CHAPTER 8: 7
In Simple Terms 157
Terminology 158
Vocabularies 158
Terms 159
Advanced Options 159
Using Terms on Your Content 160
Taxonomy Term Field Settings 162
A Term’s Parent/Child Relationship 164
Using Views to Display Term Pages 165
Display Multiple Terms on a Single Page 167
API 168
Theming Considerations 168
Customizing Each Section 168
Useful Contributed Modules 169
Summary 170
SEARCH, PERFORMANCE, STATISTICS, AND REPORTING 17CHAPTER 9: 3
Search 174
Indexing Your Content 175
Advanced Search Settings 176
Indexing Settings 176
CJK Handling 177
Content Ranking 177
Faceted Search 177
Outsourcing Search 178
If You Don’t Want to Outsource 178
Performance 178
Caching 178
Cache Blocks 179
438527ftoc.indd xiii438527ftoc.indd xiii 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xiv
CONTENTS
Page Cache for Anonymous Users 179
Minimum Cache Lifetime 179
Bandwidth Optimization 180
Clearing Cached Data 180
The Need for Speed 181
Reporting 182
Status Report 183
Recent Log Entries 183
Statistics 184
Access Log 185
Content Views 185
Performance Considerations 185
Summary 186
TRIGGERS, ACTIONS, WORKFLOW, AND RULES 18CHAPTER 10: 9
Triggers and Actions 190
Working with Actions 190
Working with Triggers 191
Next Steps 193
Workfl ow 193
Rules 200
Features of the Rules Module 201
Conditions 203
Scheduler and Rule Sets 206
Input Evaluators 209
Summary 210
VIEWS 21CHAPTER 11: 3
Advanced Help 214
Overview of Views 214
The Frontpage View 215
Explore the Frontpage View 215
Displays 218
Style Settings 220
Creating a Photo Gallery 222
Arguments 225
Exposed Filters 229
Exposed Items in a Block 231
Making Your View Accessible via a Tab 232
Relationships 234
438527ftoc.indd xiv438527ftoc.indd xiv 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xv
CONTENTS
Permissions 235
All-Views Permissions 236
Per-View Permissions 236
Caching (AKA Performance) 236
Caching 236
Block Caching 237
Best Practices 237
Advanced Views 238
Summary 238
INTERNATIONALIZATION 24CHAPTER 12: 1
A Bit of Terminology 241
Localizing 242
Regional Settings 242
Date and Time 243
Localizing Your Site’s Interface 243
Your Site’s Primary Language 249
Language Negotiation 249
Completing Your Site’s Localization 252
Making Translation Easy with the L10N Client 253
Translating Your Content 258
Beyond the Basics 260
Summary 261
THEMING 26CHAPTER 13: 3
Administering Themes 264
Installing New Themes 264
Confi guring Your Themes 265
Custom Theming 266
Template Files 270
Theme Functions 274
Reviewing the Last Five Exercises 279
Sub-Themes 280
Zen Base Theme 282
.info File 282
Removing Theme Settings 282
Style Sheets 283
JavaScript 284
Regions 285
Template Files (.tpl.php) 286
page.tpl.php 286
node.tpl.php 287
438527ftoc.indd xv438527ftoc.indd xv 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xvi
CONTENTS
comment.tpl.php 287
block.tpl.php 287
Template Suggestions 288
Preprocess Functions 288
Adding New Variables 289
Template Suggestions 289
Swapping Theme Functions and Template Files 290
Moving a Theme Function to a Template File 290
Moving a Template File to a Theme Function 290
Theme Registry 291
Theme Engines 294
Summary 296
CONTRIBUTED MODULES 29CHAPTER 14: 9
Contributed and Custom Modules 299
Determining Your Needs 300
Fulfi lling Your Needs 301
Lullabot Podcast 301
Top Drupal Modules 302
Showcases 302
Planet Drupal 303
Finding Modules 303
Search Drupal.org 303
Google 304
Drupalmodules.com 304
IRC and the Drupal Community 305
RSS Feeds 305
Twitter 305
Is It the Right Module? 306
Project Page 306
Google 309
Coder Module 309
Tests 309
Be a Follower 309
Take It Slow 310
Set Up a Development Environment 311
Avoid the Buff et 311
Summary 312
438527ftoc.indd xvi438527ftoc.indd xvi 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xvii
CONTENTS
CUSTOM MODULES 31CHAPTER 15: 5
What Is a Module? 315
Hooks 317
More Information 319
Modules Step by Step 319
Step 1: Choose a Name 319
Step 2: Create the .info File 320
Step 3: Create the .module File 321
Creating a Page 321
Capturing Custom URLs with hook_menu 322
Modifying Existing Modules 324
Patching 325
Creating a Patch 325
The Value of the Patch 327
When to Modify 327
Summary 327
DEVELOPMENT HOOKS 33CHAPTER 16: 1
Hooks: A Module’s Starting Point 332
Community Documentation 332
The Hooks 333
hook_menu 333
Making Your Modules Themeable 339
The t() Function 340
Theme Functions and Template Files 341
Summary 344
MODULE DEVELOPMENT START TO FINISH 34CHAPTER 17: 7
The Module 348
Beginning Your Module 349
Implementing a hook 350
Adding Custom Confi guration Settings 351
Custom Module Settings 352
How Did it Become an Option at Confi guration? 353
How Did the Settings Form Appear? 354
Where Are the Settings Saved? 354
Filtering on Multiple Words 355
A Quick Review of the New PHP Code 356
438527ftoc.indd xvii438527ftoc.indd xvii 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xviii
CONTENTS
Helping Your Users 356
Permission Handling 358
Storing Data in a Custom Table 359
Database Abstraction Layer 360
Drupal Query Functions 361
Displaying the Results on the User’s Profi le 365
Database Summary 367
Creating a Bulletproof Site with SimpleTest 367
Test Driven Development 367
How SimpleTest Performs Its Testing 369
Writing Your Own SimpleTest 369
SimpleTest Assertions 371
Adding the Tests to Your Module 373
Summing Up SimpleTest 375
Summary 375
ADVANCED DRUPAL INSTALLATIONS 37CHAPTER 18: 9
Drush 380
But Wait There’s More! 381
Using CVS to Manage Drupal 382
What is CVS? 382
What is SVN? 382
Installing CVS 383
Installing and Upgrading Drupal using CVS 383
Understanding CVS Tags 385
How Does This Relate to CVS? 385
Branches and Tags 386
Installing Modules from the CVS Repository 386
One Drupal, Many Websites 387
Example 1: Multiple Independent Websites 388
Example 2: Multiple Related Websites 389
Sites.php 392
Settings.php 392
$databases 393
Scalability 394
$db_prefi x 394
$baseurl 395
$ini_set 395
$reverse_proxy 396
438527ftoc.indd xviii438527ftoc.indd xviii 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
xix
CONTENTS
Variable Overrides 396
String Overrides 396
IP Blocking 397
Summary 397
PREFLIGHT CHECKLIST 40CHAPTER 19: 1
Prefl ight Checklist 402
Site Information 402
User Settings 402
Site Maintenance 402
SPAM 403
SEO 403
Search 403
Performance 404
Disaster Recovery 404
SEO 404
SEO Best Practices 405
The Importance of URLs 405
XML Sitemap 405
Submitting Your Sitemap to Search Engines 408
Analyzing Your Site with Google 411
Dashboard 413
Summary 415
APPENDIX: EXERCISES AND ANSWERS 417
INDEX 433
438527ftoc.indd xix438527ftoc.indd xix 3/10/10 9:17:33 AM3/10/10 9:17:33 AM
438527flast.indd xx438527flast.indd xx 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
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 inter-activity and information-sharing. RSS feeds, blogs, and podcasts are just a few of the current stan-dards. 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 mod-ular 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 nonprofi t 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 expe-rience 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 specifi c 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
438527flast.indd xxi438527flast.indd xxi 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
xxii
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 mod-ule 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, "Workfl ow and Actions," and Chapter 11, "Views." A good under-standing 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 appropri-ate. This book walks you through the entire process of creating a Drupal website. It starts with the installation, moves to administration and confi guration, and then covers users and permissions. The book also discusses content (including nodes, users, and blocks), Drupal’s infamous taxonomy sys-tem, 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 fl ow 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 clarifi cation or more information.
Experienced developers or those with previous Drupal experience may fi nd it benefi cial 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 ➤
438527flast.indd xxii438527flast.indd xxii 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
xxiii
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:
What You Will Be Doing in this ExerciseTRY IT OUT
Each Try It Out consists of an exercise you should work through, following the text in the book.
1. They usually consist of a set of steps.
2. Each step has a number.
3. 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.
438527flast.indd xxiii438527flast.indd xxiii 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
xxiv
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 man-ually, 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 fi lename
NOTE Because many books have similar titles, you may fi nd 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
438527flast.indd xxiv438527flast.indd xxiv 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
xxv
INTRODUCTION
this page you can view all errata that has been submitted for this book and posted by Wrox edi-tors. 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/techsup-port.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 sub-sequent 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. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to provide and click Submit.
4. You will receive an e-mail with information describing how to verify your account and com-plete 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 specifi c to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.
438527flast.indd xxv438527flast.indd xxv 3/10/10 9:17:17 AM3/10/10 9:17:17 AM
438527flast.indd xxvi438527flast.indd xxvi 3/10/10 9:17:18 AM3/10/10 9:17:18 AM
Introducing Drupal
WHAT YOU WILL LEARN IN THIS CHAPTER:
History of Drupal ➤
Drupal’s community ➤
How to fi nd 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
1
438527c01.indd 1438527c01.indd 1 3/11/10 12:25:41 PM3/11/10 12:25:41 PM
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 defi nition:
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 authentica-tion. 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 develop-ers and users.
Why is the Name Drupal?
When searching for domain names Dries accidentally misspelled the Dutch word dorp (meaning vil-lage) as drop. Accepting his mistake he registered and started to use the drop.org domain. In 2001 the project offi cially 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
438527c01.indd 2438527c01.indd 2 3/11/10 12:25:41 PM3/11/10 12:25:41 PM