You are previewing Programmer's Guide to Drupal, 2nd Edition.
O'Reilly logo
Programmer's Guide to Drupal, 2nd Edition

Book Description

If you’re a web programmer, your experiences have taught you certain lessons—and only some of them apply well to Drupal. Drupal has its own set of programming principles that require a different approach, and many programmers make mistakes when relying on skills they’ve used for other projects. This book will show you which programming techniques you can use—and which you should avoid—when building web applications with this popular content management framework.

Updated to cover both Drupal 7 and Drupal 8, the guidelines in this book demonstrate which programming practices conform to the "Drupal way" and which don’t. The book also serves as an excellent guide for Drupal 7 programmers looking to make the transition to Drupal 8.

Table of Contents

  1. Preface
    1. Intended Audience
    2. How to Use This Book
    3. Drupal Versions
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  2. 1. Overview of Drupal
    1. What Is Drupal?
      1. Drupal Core
      2. Drupal Add-Ons: Modules, Themes, Distributions, and Translations
    2. The Evolution of Drupal
    3. How Drupal Handles HTTP Requests
      1. HTTP Request Handling in Drupal 7
      2. HTTP Request Handling in Drupal 8
    4. The Drupal Cache
      1. Drupal 7 Cache API
      2. Drupal 8 Cache API
    5. Automatic Class Loading in Drupal
  3. 2. Drupal Programming Principles
    1. Principle: Drupal Is Alterable
      1. Setting Up a Module or Theme
      2. The Basics of Module and Theme Hook Programming
      3. Making Your Module Output Themeable
      4. The Basics of Drupal 8 Plugin Programming
      5. Drupal 8 Services and Dependency Injection
      6. Interacting with the Drupal 8 Event System
    2. Principle: Drupal Separates Content, Configuration, and State Data
      1. Information Storage in Drupal 7
      2. Information Separation in Drupal 8
      3. Configuration API in Drupal 8
      4. State API in Drupal 8
    3. Principle: Drupal Is International
      1. Internationalizing User Interface Text
      2. Internationalizing User-Entered Text in Drupal 7
      3. Internationalizing User-Entered Text in Drupal 8
    4. Principle: Drupal Is Accessible and Usable
    5. Principle: Drupal Is Database-Independent
      1. Setting Up Database Tables: Schema API and hook_update_N()
      2. Querying the Database with the Database API
    6. Principle: Drupal Is Secure; User Input Is Insecure
      1. Cleansing and Checking User-Provided Input
      2. Checking Drupal Permissions
    7. Principle: Drupal Code Is Tested and Documented
  4. 3. Common Drupal Programming Mistakes
    1. Mistake: Programming Too Much
      1. Avoiding Custom Programming with Fielded Data
      2. Defining Theme Regions for Block Placement
    2. Mistake: Overexecuting Code
      1. Mistake: Executing Code on Every Page Load
      2. Mistake: Using an Overly General Hook
    3. Mistake: Saving PHP Code in the Database
    4. Mistake: Working Alone
      1. Participating in Groups and IRC
      2. Reporting Issues and Contributing Code to the Drupal Community
      3. Contributing to the Drupal Community in Other Ways
  5. 4. Drupal Programming Examples
    1. Registering for URLs and Displaying Content
      1. Registering for a URL in Drupal 7
      2. Altering a URL Registration in Drupal 7
      3. Registering for a URL in Drupal 8
      4. Providing Administrative Links
      5. Altering Routes and Providing Dynamic Routes in Drupal 8
      6. Registering a Block in Drupal 7
      7. Registering a Block in Drupal 8
      8. Creating Render Arrays for Page and Block Output
      9. Render Arrays in Drupal 8
      10. Generating Paged Output
    2. Using the Drupal Form API
      1. Form Arrays, Form State Arrays, and Form State Objects
      2. Basic Form Generation and Processing in Drupal 7
      3. Basic Form Generation and Processing in Drupal 8
      4. Creating Confirmation Forms
      5. Adding Autocomplete to Forms
      6. Altering Forms
    3. Programming with Ajax in Drupal
      1. Setting Up a Form for Ajax
      2. Wrapper-Based Ajax Callback Functions
      3. Command-Based Ajax Callback Functions in Drupal 7
      4. Command-Based Ajax Callback Functions in Drupal 8
    4. Programming with Entities and Fields
      1. Terminology of Entities and Fields
      2. Defining an Entity Type in Drupal 7
      3. Defining a Content Entity Type in Drupal 8
      4. Defining a Configuration Entity Type in Drupal 8
      5. Querying and Loading Entities in Drupal 8
      6. Defining a Field Type
      7. Programming with Field Widgets
      8. Programming with Field Formatters
    5. Creating Views Module Add-Ons
      1. Views Programming Terminology and Output Construction
      2. Setting Up Your Module for Views in Drupal 7
      3. Providing a New Views Data Source
      4. Adding Handlers to Views
      5. Adding Fields and Relationships to an Existing Views Data Source
      6. Providing a Style or Row Plugin to Views
      7. Providing Default Views
    6. Creating Rules Module Add-Ons in Drupal 7
      1. Providing Custom Actions to Rules
      2. Providing Default Reaction Rules and Components
    7. Programming with CTools in Drupal 7
      1. Implementing CTools Plugins for Panels
      2. Providing Default CTools Exportables
  6. 5. Programming Tools and Tips
    1. Where to Find More Information
      1. Drupal Site Building and General Drupal Information
      2. Drupal Programming Reference and Background
      3. PHP Resources
      4. Database Resources
      5. Other Web Technology Resources
    2. Drupal Development Tools
    3. Discovering Drupal API Functions and Classes
    4. Other Programming Tips and Suggestions
  7. Index