The Definitive Guide to Drupal 7 Benjamin Melancon, Jacine Luisi, Karoly Negyesi, Greg Anderson, Bojhan Somers, Stephane Corlosquet, Stefan Freudenberg, Michelle Lauer, Ed Carlevale, Florian Loretan, Dani Nordin, Ryan Szrama, Susan Stewart, Jake Strawn, Brian Travis, Dan Hakimzadeh, Amye Scavarda, Albert Albala, Allie Micka, Robert Douglass, Robin Monks, Roy Scholten, Peter Wolanin, Kay VanValkenburgh, Greg Stout, Kasey Qynn Dolin, Mike Gifford, Claudina Sarahe, Sam Boyer, and Forest Mars, with contributions from George Cassie, Mike Ryan, Nathaniel Catchpole, and Dmitri Gaskin Apress
29
Embed
The definitive guide to Drupal 7 : [everything you need to ... Contents ata Glance iv Foreword xxxiv Aboutthe Authors xxxv Aboutthe Technical Reviewer..... xli Acknowledgments xlii
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
The Definitive Guide
to Drupal 7
Benjamin Melancon, Jacine Luisi, Karoly Negyesi,Greg Anderson, Bojhan Somers, StephaneCorlosquet, Stefan Freudenberg, Michelle Lauer, Ed
Carlevale, Florian Loretan, Dani Nordin, RyanSzrama, Susan Stewart, Jake Strawn, Brian Travis,Dan Hakimzadeh, Amye Scavarda, Albert Albala, Allie
Micka, Robert Douglass, Robin Monks, Roy Scholten,Peter Wolanin, Kay VanValkenburgh, Greg Stout,Kasey Qynn Dolin, Mike Gifford, Claudina Sarahe,Sam Boyer, and Forest Mars, with contributions fromGeorge Cassie, Mike Ryan, Nathaniel Catchpole, and
Dmitri Gaskin
Apress
Contents
Contents at a Glance iv
Foreword xxxiv
About the Authors xxxv
About the Technical Reviewer..... xli
Acknowledgments xlii
Preface: Why Drupal.. xliii
What's New in Drupal 7? xlvii
How to Use This Book liii
How Drupal Works Ivii
Part I: Getting Started 1
Chapter 1: Building a Drupal 7 Site 3
Planning: Setting Parameters and Knowing Where You're Going 4
Discovery: Why Should This Site Be Built? 4
Information Architecture: Exactly What Will You Build? 5
Design 7
Implementation 8
Installing Drupal 9
Drupal's Administration Menu 12
Appearance: Changing a Core Theme's Color Scheme 13
Extending Functionality with Modules 14
Allowing People to Register and Log In with OpenID 15
Creating Content Types and Adding Content 16
vi
m CONTENTS
Creating Content 19
Blocks: Creating a Mission Statement 21
Taxonomy: Categorizing Content .....24
Users, Roles, and Permissions 27
Time for a Celebratory Beverage 29
Chapter 2: Essential Tools: Drush and Git 31
A Beginner's Guide to Installing Drush 32
Git: Development Grease 38
Why Git? 39
Installing Git 39
Working with Git 41
Other Useful Git Commands 45
Database Backup Tools 45
Summary 46
Part II: Site Building Foundations 47
i Chapter 3: Building Dynamic Pages Using Views 49
What Are Views? 49
Examples of Views Usage 50
Download, Enable, and Configure Permissions for the Views Module(s) 50
The Views Administration Page 53
Advanced Help Module 53
Action Links 54
Change Which Available Views Are Listed 54
Available Views 55
Deconstructing a View 57
Display Types 58
Views Configuration Detail 59
Overriding: A Views Concept 64
Understanding What Type of Content Will Be Output: Views Filters 64
Advanced Filter Criteria Groups: Combining Sorts with Logical Operators 66
vii
i CONTENTS
Understanding the Order in Which Content Will Be Output: Views Sort Criteria 67
Understanding What Pieces of Content Will Be Output: Views Format Settings 67
Creating a Basic View 68
The Goal 68
Systematic Approach 69
Set Up the Basics for Your Views 69
Define the Administrative Information 71
Define the Title 71
Define What Type of Content You Want to Display 71
Define What Elements of the Content You Want to Display 72
Define Format Settings 72
Define the Order in Which You Want Your Content to Display 72
Define the Number of Results 72
Add a Menu 73
Define Advanced Settings 73
Preview Your Work 73
Dynamically Editing Your View 73
Admire Your View 74
Add More Features 74
Extend a View 76
Handling the Use Case of Zero Results 76
One Page, Multiple Displays to Highlight First Result 77
Using Tabs for Unique Displays 78
Cloning and Making Administrative Tables Using Exposed Filters 80
Advanced Views Implementations 82
Contextual Filters 82
Relationships 83
Other Modules 84
Exporting to Code 85
Additional Resources 86
Chapter 4: There's a Module for That 87
Modules for Drupal Are Constantly Evolving 87
viii
» CONTENTS
Fewer Modules Is Better 88
How to Find and Choose a Module 88
What to Do When Something's Wrong with a Module 91
Modules in Core 91
Where to Store Contributed Modules 94
Site Building Essentials 94
Views 95
Pathauto 95
Token (Dependency) 96
Additional Field Types 96
WYSIWYG 97
Webform 98
AntiSpam or Mollom 98
Other Modules That May Prove Useful 99
Administrative Interface and Content Entry 99
Content Display 103
Menus and Navigation 104
Community Building and Social Networking 104
Paths, Search and 404 Errors 106
Miscellany 107
The Beauty of It All 108
« Chapter 5: Creating Community Web Sites with Organic Groups 109
Installing and Configuring Organic Groups 110
Group Content Types 111
Creating Groups 114
Using Views with Organic Modules 115
Creating Group Content 117
Getting Started with Panels 119
Members, Roles, and Permissions .-122
Summary 124
ix
CONTENTS
Chapter 6: Security in Drupal 125
Setting Up a Secure Drupal Site 125
Use Strong Passwords 125
Reserve User 1 for Administration Purposes Only 126
Be Cautious When Assigning Permissions 127
Keep Text Formats Tight and Secure 127
Avoid Using the PHP Filter Module 128
Security Process 128
Choosing Modules and Themes: How Secure Are Contributed Projects? 129
Keep Your Code Base Current 132
Writing Secure Code 133
Dealing with Security Issues 135
Summary 136
Chapter 7: Updating Drupal 137
Why Update 137
Preparation 138
Manual Update 139
Follow the Steps in UPGRADE.txt 140
Drush Update 142
Diff Update 143
Contributed Modules 145
Drupal's Automated Module Installer 146
Updating Modules with Drush 146
Summary 147
Chapter 8: Extending Your Site 149
Showcasing Authors with Profile Pages 149
Giving Authors a Headshot Image 151
Linking from Profiles to Web Sites 151
Authors' Other Homes on the Internet 153
A Non-displaying Data Field: Approximate Pages 155
x
i CONTENTS
Connecting Author Profiles to Authors' User Accounts 155
Giving Authors Permission to Create Profiles 156
Listing the Authors 157
Building the Authors Headshot View 158
Building an Author Biographies View Page, Reachable As a Tab on the Authors View 162
Fine-tuning Content Display 164
Using View Modes to Display the Same Content in Different Ways 166
Modifying Teaser Display and Setting Trim Length 166
Making the Table of Contents with Book Module 168
Setting Permissions for Organizing and Writing Chapters 170
Adding Metadata to the Chapter Content Type with Fields 172
Setting How the Chapter Content Type Displays Its Fields 173
Using Menu Block to Display a Better Table of Contents 176
Adding the Table of Contents to the Main Menu 178
Unking Chapters to Their Authors 178
Adding a Resource Content Type That References Chapters 179
Reusing Chapter's Image Field 179
Allowing People to Attach Generic Files to Content 180
Connecting Content Types with a Node Reference 181
Managing Resource Content Type Display 181
Showing Content That References the Post Being Viewed 182
Giving Faces to the People Posting on Your Site 183
Adding a Text Format That Allows Images 184
Bonus: Making It Easy to Insert Images into Posts 186
Limiting Access to the Suggestion Status Field 187
Autogenerating Human-readable URLs with Pathauto 190
Summary 191
Part III: Making Your Life Easier 193
Chapter 9: Drupal Community: Getting Help and Getting Involved 195
How to Get the Most from Your Participation 196
XI
a CONTENTS
Where to Find the Community 196
Reading, Listening, and Watching 196
IRC 199
The Issue Queues 201
Summary 202
Chapter 10: Planning and Managing a Drupal Project 203
The Role of Limitations 203
Putting Down Your Concept on Paper 204
1. Discovery 205
2. Information Architecture 206
3. Design 206
4. Development and Implementation 207
5. Content 207
6. Deployment/Launch 207
7. Maintenance 207
Project Management Methodologies and Drupal 207
Taking the Lifecycle into Account on Paper 209
What's a Project Plan? 209
Estimating Completion Dates 211
Risks 212
Minimum Viable Project/Product 212
Keeping Track of Commitments 212
Project Manager Tasks Beyond Development 213
Kickoff Meetings 213
Discovery Meetings 214
Information Architecture/Design Meetings 214
Development Meetings 215
Checkins 215
Milestone Closing Meetings 215
Launch Meetings 216
Post-Project Debriefs 216
Other Tasks for Project Managers 216
Xll
3 CONTENTS
Creating User Stories 216
Implementing Tasks and Task Workflow 217
Tasks That Make up Milestones 217
Bad Days 218
Further Resources 219
Chapter 11: Documenting for End Users and the Production Team 221
What Makes Good Documentation? 221
Getting Clients into Content Entry Early 222
Creating End-User Documentation Post-Launch 222
The Anatomy of Good Client Documentation 223
Documenting for the Development Team 225
Documenting for the Community 226
The More You Know ...226
Chapter 12: Development Environment 227
Starting with Quickstart 228
Enhancing Your Existing Dev Environment 229
Hosting Your Site Locally 230
Accessing the Command Line 230
Working with Rendered HTML, CSS, and JavaScript 231
Browser and Device Compatibility Testing 232
Working with PHP Files 233
The Most Basic Development Environment 233
Select Hosting Service 234
Uncompressing Archives 238
Server-Side Tasks and Tools 239
Summary 241
^Chapter 13: Putting a Site Online and Deploying New Features 243
Putting Your Site Online 243
1. Export the Database 244
CONTENTS
2. Transfer to Server 245
3. Create a Database on the Server and Import Your Database 245
4. Set the Database Settings in settings.php 246
5. Point Incoming Traffic for Your Domain(s) to Your Site on the Server 247
Before You Go Any Further, Back Up 249
Staging and Deployment 251
Approach 252
The Workflow 252
Bringing Content from Production to Development (and Stage/QA) 253
Bring Code Changes from Development to Stage, then Live 254
How to Put "Everything In Code" 256
Development Workflow Recap 260
Summary 261
Chapter 14: Developing from a Human Mindset 263
Use Revision Control 263
Backup 263
Experiment Freely 264
Contribute 265
Part IV: Front-End Development 267
Chapter 15: Theming 269
The Core Themes Directory 269
Core Themes 270
Theme Engines 273
Theme Administration 274
Enabling and Setting a Default Theme 274
Administration Theme 275
Global Theme Settings 275
Installing a New Theme 277
Defining Theme Metadata (.info Files) 278
Required Properties 279
xiv
II CONTENTS
Additional Properties 279
Working with Regions 282
Default Regions 284
Hidden Regions 286
Module-Specific Regions 287
Regions and Your Theme 288
Using Regions vs. Hard-coding Variables in Template Files 288
Layout Strategies 289
Template Files 293
Common Core Templates 295
Overriding Template Files 295
Global Template Variables 296
Theme Functions .-..301
How Theme Functions Are Created 301
Calling Theme Functions 302
Overriding Theme Functions 302
Theme Hooks and Theme Hook Suggestions 304
What Is a Theme Hook? 304
Theme Hook Suggestions 305
Suggestions and Template Files 305
Suggestions and Theme Functions 308
Summary 310
Chapter 16: Advanced Theming 311
Finding Available Variables in the Theme Layer 311
Using the Theme Developer Module 312
Preprocess and Process Functions 313
Implementing Preprocess and Process Hooks 315
Finding the Contents of $variables 317
Preprocess Functions in Action 318
Using the Render API 321
What Is a Render Array? 321
Identifying Render Elements •322
xv
CONTENTS
Manipulating the Output of Render Elements 322
Notable Render Arrays in Core Templates 326
Introducing renderO, hideO, and showO 327
Theming Forms 329
How Form Markup Is Generated 329
First Steps for Theming Forms 332
Theming Forms with Theme Functions 334
Theming Forms with Template Files 336
Modifying Forms Using Alter Hooks 339
Managing CSS Files 341
Aggregation and Compression 341
Patterns and Naming Conventions 342
Adding, Removing, and Replacing CSS Files 343
Working with Base and Subthemes 348
Creating a Subtheme 348
Inheritance and How It Works 349
Finding a Good Base Theme 349
Tips for Creating Your Own Base Themes 351
Sustainability and Best Practices 351
Start With a Good Base 351
Override Template Files with Purpose 353
Leverage Default CSS Classes 353
Do My Changes Belong in a Module? 354
Summary 354
Chapter 17: jQuery 355
Implementing jQuery and JavaScript 355
Including JavaScript 355
JavaScript Libraries 359
Overriding JavaScript 362
Drupal Behaviors 364
Degrading JavaScript/jQuery Nicely 365
jQuery Ul 366
xvi
'« CONTENTS
jQuery Ul in Drupal Core 366
Further jQuery Resources 379
Summary 379
Part V: Back-End Development 381
Chapter 18: Introduction to Module Development 383
A Very Simple Module 383
Two Files in a Folder 383
Where to Put a Custom Module 386
Create a Repository for Your Module 388
The .info File 388
The .module File 392
Hooks 393
Technical Skills 398
PHP Basics 398
Terminology 399
Operators and Conditional Statements 399
Control Structures 402
Drupal Coding Standards 404
Development Tip #1: When Something Isn't Working, Clear Caches 407
Development Tip #2: When Anything's Missing, Check Permissions 407
Development Tip #3: Set Your Site to Show All Errors 408
Summary 408
Chapter 19: Using Drupal's APIs in a Module 409
Altering Forms 409
Localization with to and format_pluralO 412
Finding a Drupal Function That Does What You Need 414
Investigating What the Function Gives You 416
Creating a Page with hookjnenuO 421
Choosing a Path for an Administration Page 422
Defining a Page with a Normal Menu Item 422
xvii
CONTENTS
Defining a Tab with a Local Task Menu Item 425
Declaring Menu Items for X-ray Module 425
Using Existing Permissions in Your Module 426
Finding Permissions' System Names in the Database 428
Finding Permissions' System Names in Code 430
A Second Local Task to Complement the Default Local Task 431