wrox.beginning.drupal.apr.2010
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.com Programmer Forums Join 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 JavaScript ISBN: 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 Development ISBN: 978-0-470-43853-4 Written 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 Development ISBN: 978-0-7645-7966-0 This 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.3 ISBN: 978-0-470-41396-8 As 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 Site ISBN: 978-0-470-570890 This 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 Right ISBN: 978-0-470-41087-5 Drupal 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 Patterns ISBN: 978-0-470-49670-1 This 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 Trainer ISBN: 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 ™ Interact Take an active role online by participating in our P2P forums @ p2p.wrox.com Wrox Online Library Hundreds 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 Community Sign up for our free monthly newsletter at newsletter.wrox.com Browse Ready 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 wrox-partnerwithus@wrox.com 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 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) 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 Je 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: ԴӀᐂ៥ᕜ៥ᛇ䇈䴲ᐌᛳ䇶ԴӀ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-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 . Wrox Programmer to Programmer™ Join the discussion @ p2p .wrox. com Beginning Drupal ® Jacob Redding Redding $39.99 USA $47.99 CAN Drupal is an. 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