cyan yelloW MaGenTa Black panTone 123 c Books for professionals By professionals® The eXperT’s Voice® in open source companion eBook Available NNooww ccoovveerrss Pro Drupal Development, DDrruuppaall 66!! Pro SeconD eDition Pro Dear Reader, D Drupal is a powerful open source content management framework for creating customized web sites. Building on its modular core, over time you can evolve a r basic brochure-style site into a platform for driving cutting-edge services such u Drupal as social networking, mashups, and e-commerce, all within the same consis- tent, integrated, and secure framework. Best of all, with Drupal’s fine-grained permissions and revision support, editing web site content can be delegated to p those who know it best—the users. In Pro Drupal Development, Second Edition, I cover Drupal from the per- a spective of someone knowledgeable in PHP who is looking for a way to quickly understand the system and begin coding sophisticated Drupal applications as l soon as possible. For that reason, I use an approach that is peppered with practi- cal coding examples, big-picture flowcharts, and diagrams to help you visualize D how Drupal works. And I’ve included a chapter on best practices for Drupal development to help you avoid common pitfalls. e I have been using Drupal for over five years and have contributed to the Drupal Development v core as well as to numerous modules. During this time, though Drupal was designed e to be lean and modular, I’ve observed new developers struggling to understand Drupal’s internals. This book should help make the learning curve less daunting and l encourage talented developers to learn, use, and ultimately share in the benefits of o one of the most vibrant and growing open source communities. p John K. VanDyk, PhD m e Learn how to use the content management n THE APRESS ROADMAP framework to create powerful customized web sites t companion eBook Building Online Pro Drupal Development, Communities with Drupal, Second Edition phpBB, and WordPress SeconD eDition SeconD eDition Beginning PHP and PHP 5 Objects, MySQL 5, Third Edition Patterns, and Practice See last page for details on $10 eBook version Drupal 6 SOURCE CODE ONLINE V John K. VanDyk a www.apress.com ISBN 978-1-4302-0989-8 n 54999 D y Foreword by Dries Buytaert, k US $49.99 Drupal founder and project lead Shelve in PHP User level: 9 781430 209898 Intermediate–Advanced this print for content only—size & color not accurate spine = 1.324" 704 page count 09898fmfinal.qxd 7/30/08 12:48 PM Page i Pro Drupal Development Second Edition John K. VanDyk 09898fmfinal.qxd 7/30/08 12:48 PM Page ii Pro Drupal Development,Second Edition Copyright © 2008 by John K.VanDyk All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-0989-8 ISBN-13 (electronic): 978-1-4302-0990-4 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editor: Matt Wade Technical Reviewer:Robert Douglass Editorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, JonathanGennick, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editors: Heather Lang and Damon Larson Associate Production Director: Kari Brooks-Copony Production Editor: Laura Esterman Compositor: Linda Weidemann, Wolf Creek Press Proofreaders: April Eddy and Linda Siefert Indexer:John Collin Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide bySpringer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected],or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected],or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses arealso available for most titles.For more information, reference our SpecialBulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every pre- caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. 09898fmfinal.qxd 7/30/08 12:48 PM Page iii For the Great Architect and to my incredibly patient wife and children 09898fmfinal.qxd 7/30/08 12:48 PM Page iv Contents at a Glance Foreword........................................................................xxv About the Author................................................................xxvii About the Technical Reviewer .....................................................xxix Acknowledgments...............................................................xxxi Introduction....................................................................xxxiii nCHAPTER 1 HowDrupal Works..............................................1 nCHAPTER 2 Writing a Module ..............................................13 nCHAPTER 3 Hooks,Actions,and Triggers...................................35 nCHAPTER 4 The Menu System .............................................59 nCHAPTER 5 Working with Databases.......................................89 nCHAPTER 6 Working with Users...........................................115 nCHAPTER 7 Working with Nodes ..........................................137 nCHAPTER 8 The Theme System ...........................................165 nCHAPTER 9 Working with Blocks..........................................203 nCHAPTER 10 The FormAPI.................................................221 nCHAPTER 11 Manipulating User Input:The Filter System....................275 iv 09898fmfinal.qxd 7/30/08 12:48 PM Page v nCHAPTER 12 Searching and Indexing Content ..............................291 nCHAPTER 13 Working with Files............................................309 nCHAPTER 14 Working with Taxonomy ......................................327 nCHAPTER 15 Caching......................................................349 nCHAPTER 16 Sessions .....................................................365 nCHAPTER 17 Using jQuery..................................................377 nCHAPTER 18 Localization and Translation ..................................407 nCHAPTER 19 XML-RPC.....................................................439 nCHAPTER 20 Writing Secure Code..........................................453 nCHAPTER 21 Development Best Practices ..................................477 nCHAPTER 22 Optimizing Drupal ............................................527 nCHAPTER 23 Installation Profiles...........................................547 nAPPENDIX A Database Table Reference....................................573 nAPPENDIX B Resources....................................................605 nINDEX .......................................................................611 v 09898fmfinal.qxd 7/30/08 12:48 PM Page vi 09898fmfinal.qxd 7/30/08 12:48 PM Page vii Contents Foreword........................................................................xxv About the Author................................................................xxvii About the Technical Reviewer .....................................................xxix Acknowledgments...............................................................xxxi Introduction....................................................................xxxiii nCHAPTER 1 How Drupal Works...........................................1 WhatIs Drupal?..................................................1 Technology Stack ................................................1 Core............................................................2 Administrative Interface...........................................3 Modules.........................................................3 Hooks...........................................................4 Themes.........................................................5 Nodes ..........................................................5 Blocks ..........................................................6 File Layout ......................................................6 Serving a Request................................................8 The Web Server’s Role.......................................8 The BootstrapProcess .......................................9 Processing a Request.......................................10 Theming the Data ..........................................11 Summary.......................................................11 nCHAPTER 2 Writing a Module............................................13 Creating the Files................................................13 Implementing a Hook............................................15 Adding Module-Specific Settings..................................16 Adding the Data Entry Form.......................................19 Storing Data in a Database Table.............................22 Defining Your Own Administration Section..........................27 Presenting a Settings Form to the User.............................29 Validating User-Submitted Settings................................31 vii 09898fmfinal.qxd 7/30/08 12:48 PM Page viii viii nCONTENTS Storing Settings.................................................32 Using Drupal’s variables Table ...............................33 Retrieving Stored Values with variable_get() ...................34 Further Steps...................................................34 Summary.......................................................34 nCHAPTER 3 Hooks, Actions, and Triggers ..............................35 Understanding Events and Triggers................................35 Understanding Actions...........................................37 The Trigger User Interface...................................37 Your First Action............................................39 Assigning the Action........................................41 Changing Which Triggers an Action Supports ..................41 Actions That Support Any Trigger.............................42 Advanced Actions ..........................................42 Using the Context in Actions......................................47 How the Trigger Module Prepares the Context..................47 Establishing the Context.....................................49 Examining the Context...........................................51 HowActions Are Stored..........................................52 The actions Table...........................................52 Action IDs .................................................53 Calling an Action Directly with actions_do().........................53 Defining Your Own Triggers with hook_hook_info()..................54 Adding Triggers to Existing Hooks .................................56 Summary.......................................................58 nCHAPTER 4 The Menu System...........................................59 Callback Mapping ...............................................59 Mapping URLs to Functions..................................59 Page Callback Arguments ...................................65 Menu Nesting...................................................69 Access Control..................................................70 Title Localization and Customization...............................71 Defining a Title Callback.....................................71 Title Arguments ............................................73 Wildcards in Menu Items.........................................75 Wildcards and Parameter Replacement........................77 Building Paths from Wildcards Using to_arg() Functions .........79
Description: