O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drupal 7 Module Development

Book Description

For PHP developers, this is the most practical book available if you want to start coding within Drupal. Every chapter has working code samples, allowing you to establish a great foundation for building your own tools.

  • Specifically written for Drupal 7 development

  • Write your own Drupal modules, themes, and libraries

  • Discover the powerful new tools introduced in Drupal 7

  • Learn the programming secrets of six experienced Drupal developers

  • Get practical with this book's project-based format

  • In Detail

    Drupal is an award-winning open-source Content Management System. It's a modular system, with an elegant hook-based architecture, and great code. Modules are plugins for Drupal that extend, build or enhance Drupal core functionality.

    In Drupal 7 Module development book, six professional Drupal developers use a practical, example-based approach to introduce PHP developers to the powerful new Drupal 7 tools, APIs, and strategies for writing custom Drupal code.

    These tools not only make management and maintenance of websites much easier, but they are also great fun to play around with and amazingly easy to use.

    If you're eager to learn about these new APIs and start creating modules for Drupal 7, this is your book. Walk through the development of complete Drupal modules with this primer for PHP programmers.

    From basic modules and themes to sophisticated Drupal extensions, learn how to use Drupal's API and hook system to build powerful custom Drupal websites. With an emphasis on practical programming, this book takes a project-based approach, providing working examples in every chapter Specifically written for Drupal 7, this book will get you coding modules as quickly as possible, and help you add the features that will give your work that professional gloss!

    This book will walk you through the development of complete Drupal modules and show you how to add various features to meet your requirements.

    The Drupal content management system, written in the popular PHP language, has skyrocketed in popularity.

    Developers for this system are in high demand. This book prepares PHP developers for Drupal development, explaining architecture, exploring APIs, and emphasizing practical approaches.

    In each chapter, readers will learn new aspects of the system while creating fully-functioning modules, themes, and libraries. Learn how to “hook into” any part of the Drupal process, creating custom content types, extending existing capabilities, and integrating with external services and applications.

    Learn the ins and outs of writing custom modules, themes, installation profiles and libraries for the Drupal PHP content management system.

    Table of Contents

    1. Drupal 7 Module Development
      1. Copyright
      2. Credits
      3. Foreword
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. Who this book is for
        3. Conventions
        4. Reader feedback
        5. Customer support
          1. Errata
          2. Piracy
          3. Questions
      8. 1. Developing for Drupal 7
        1. Introducing Drupal (for developers)
          1. Technologies that drive Drupal
            1. PHP
            2. Databases and MySQL
            3. HTML, CSS, and JavaScript
            4. Other technologies
            5. The web server
            6. The Operating System
        2. Drupal architecture
          1. Drupal core libraries
          2. Drupal hooks
          3. Drupal core modules
          4. The database
          5. The theme system
        3. Drupal's major subsystems
          1. Themes
          2. Menus
          3. Nodes
          4. Files
          5. Users
          7. Fields and entities
          8. Forms API
          9. Installation Profiles
          10. Simple test
          11. Blocks
          12. Other subsystems
        4. Tools for developing Drupal code
          1. Version control with Git and CVS
            1. The book's code and Git
          2. The API site and coding standards
          3. Developer-oriented modules
            1. The developer module
            2. Drush (the Drupal shell)
            3. Coder
        5. Summary
      9. 2. Creating Your First Module
        1. Our goal: a module with a block
        2. Creating a new module
          1. Module names
          2. Where does our module go?
          3. Creating the module directory
          4. Writing the .info file
          5. Creating a module file
            1. Source code standards
            2. Doxygen-style doc blocks
            3. The help hook
            4. The t() function and translations
        3. Working with the Block API
          1. The block info hook
          2. The block view hook
          3. The first module in action
        4. Writing automated tests
          1. Creating a test
            1. Starting out
            2. Writing a test case
            3. The basic pattern
            4. The getInfo() method
            5. Setting up the test case
            6. Writing a test method
        5. Summary
      10. 3. Drupal's Theme Layer
        1. Business logic versus presentation logic
        2. Data granularity
        3. Theme engines
        4. Two ways to theme
          1. Theme functions
            1. Preprocess functions
            2. Theme overrides
          2. Template files
            1. The preprocess zoo
              1. "template_" preprocess functions
              2. Multi-hook preprocess functions
              3. Process functions
              4. Order of preprocess execution
        5. Render elements
          1. Render properties
          2. hook_element_info
          3. hook_page_alter()
        6. The power of theme()
          1. Theme hook suggestions
        7. Theme registry
          1. Variable default values
          2. hook_theme
          3. hook_theme_registry_alter
        8. What else?
        9. Summary
      11. 4. Theming a Module
        1. Reusing a default theme implementation
          1. Drupal blocks revisited
          2. Theming a Drupal block
          3. Render element and a theme hook suggestion
          4. Creating a pre_render function
          5. Attaching CSS to render arrays
          6. RTL languages
        2. Steps to build a default theme implementation
          1. hook_theme() implementations
          2. Variables versus render element
          3. Preprocess functions
          4. Template files
        3. Summary
      12. 5. Building an Admin Interface
        1. The User Warn module
        2. Starting our module
        3. The Drupal menu system
          1. Defining a page callback with hook_menu
          2. Using wildcards in menu paths
        4. Form API
          1. Using drupal_get_form()
          2. Building a form callback function
          3. Managing persistent data
          4. Form submission process
          5. A shortcut for system settings
          6. A shortcut for confirmation forms
        5. Sending mail with drupal_mail() and hook_mail()
          1. Calling drupal_mail()
          2. Implementing hook_mail()
        6. The token system
          1. What are tokens?
          2. Implementing tokens in your text
        7. Summary
      13. 6. Working with Content
        1. Why create your own entities
        2. The goal
        3. Bundles
        4. The Schema API
        5. Declaring our entity
          1. The entity declaration
          2. The entity controller
        6. Entity management
          1. Managing artwork types
          2. Adding artworks
            1. Adding new artwork
            2. Validation callback
            3. Submit callback
            4. Saving your artwork
            5. Handling revisions
          3. Viewing artworks
          4. Editing an artwork
          5. Deleting an artwork
        7. Summary
      14. 7. Creating New Fields
        1. Our goal: a "dimensions" field
        2. How Field API works
        3. Creating our new field type
          1. Declaring the field
          2. Defining the field structure
          3. Defining empty
          4. Field settings
          5. Field validation
        4. Exposing fields to the Form API with widgets
          1. Declaring a widget
          2. Simple widget forms
          3. Complex widgets
        5. Using formatters to display our field
          1. Declaring a formatter
          2. Single-value formatters
          3. Complex formatters
        6. Managing non-Field fields
        7. Finding entities and fields
        8. Summary
      15. 8. Drupal Permissions and Security
        1. Using user_access() to assert permissions
        2. Checking the proper user account
        3. Using hook_permission()
          1. Defining your module's permissions
          2. Writing hook_permission()
        4. Declaring your own access functions
        5. Responding when access is denied
        6. Enabling permissions programmatically
        7. Defining roles programmatically
        8. Securing forms in Drupal
          1. The Forms API
          2. Disabling form elements
          3. Passing secure data via forms
          4. Running access checks on forms
        9. Handling AJAX callbacks securely
          1. Using AJAX in forms
          2. Using AJAX in other contexts
        10. Summary
      16. 9. Node Access
        1. Node Access compared to user_access() and other permission checks
          1. How Drupal grants node permissions
          2. The node_access() function
            1. The access whitelist
            2. Caching the result for performance
            3. Invoking hook_node_access()
            4. Access to a user's own nodes
            5. Invoking the node access API
          3. hook_node_access() compared to {node_access}
        2. Using hook_node_access()
          1. A sample access control module
          2. A second access control module
          3. View operations and access control modules
        3. When to write a node access module
          1. The {node_access} table and its role
          2. {node_access} table schema explained
          3. Defining your module's access rules
        4. Creating the role access module
          1. Using hook_node_access_records()
          2. Using hook_node_grants()
          3. Security considerations
          4. Rebuilding the {node_access} table
        5. Modifying the behavior of other modules
          1. Using hook_node_grants_alter()
          2. Using hook_node_access_records_alter()
        6. Testing and debugging your module
          1. Using Devel Node Access
            1. Using hook_node_access_explain()
            2. Using the Devel Node Access by user block
        7. Summary
      17. 10. JavaScript in Drupal
        1. JavaScript inside Drupal
        2. Adding JavaScript
          1. Adding JavaScript and CSS files to .info files
          2. Using drupal_add_js()
            1. Adding JavaScript files
            2. Adding CSS files
            3. Passing variables from PHP to JavaScript
            4. Adding inline JavaScript
            5. Adding inline CSS
          3. Using the Library API
            1. Defining a library with hook_library
            2. Altering information in hook_library
          4. Using renderable arrays
        3. Altering JavaScript
          1. Altering CSS
        4. Drupal specific JavaScript
          1. Themeable presentation
          2. Translatable strings
          3. Behaviors
        5. AJAX helpers
          1. Adding AJAX to forms
          2. AJAX automatically applied
          3. AJAX commands
            1. ajax_command_after
            2. ajax_command_alert
            3. ajax_command_append
            4. ajax_command_before
            5. ajax_command_changed
            6. ajax_command_css
            7. ajax_command_data
            8. ajax_command_html
            9. ajax_command_prepend
            10. ajax_command_remove
            11. ajax_command_replace
            12. ajax_command_restripe
            13. ajax_command_settings
        6. Summary
      18. 11. Working with Files and Images
        1. The Twitpic and watermark modules
        2. Files in Drupal
        3. File API
        4. Stream wrappers
          1. Creating a stream wrapper
        5. Images in Drupal
          1. Image API
          2. Image Styles
          3. Creating image effects
          4. Creating image styles from a module
        6. Summary
      19. 12. Installation Profiles
        1. Introducing installation profiles
        2. Drupal distributions
          1. Setting up a distribution
          2. Standard and minimal profiles
          3. Creating a profile directory
          4. Profile modules and themes
        3. Creating profiles
          1. Enabling modules
          2. The install task system
          3. Choosing an install task or using hook_install
          4. Anatomy of an install task
          5. Creating a task
          6. Altering tasks
          7. Configuring blocks
          8. Variable settings
          9. Text filters
          10. Code placement
        4. Running the installer from the command line
        5. Summary
      20. A. Database Access
        1. Basic queries
        2. Result objects
        3. Dynamic queries
        4. Insert queries
        5. Update queries
        6. Delete queries
        7. Merge queries
        8. Advanced subjects
          1. Transactions
          2. Slave servers
        9. Summary
      21. B. Security
        1. Thinking securely
        2. Filtering versus escaping
          1. Filtering
          2. Escaping HTML
          3. SQL injection
        3. Node access control
        4. Handling insecure code
        5. Staying up to date
        6. Summary