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 8 Module Development

Book Description

Learn to create and customize impressive Drupal 8 modules to extend your website's functionalities

About This Book

  • Unleash the power of Drupal 8 programming by creating efficient modules and extensions
  • Explore a plethora of Drupal 8 APIs and get the best out of them using the power of PHP coding
  • Learn to implement efficient data management and data security by creating dedicated modules for it.

Who This Book Is For

The primary target of this book is Drupal developers who want to learn how to write modules and carry out development in Drupal 8. It is also intended for Drupal site builders and PHP developers who have basic Object Oriented Programming skills.

A little bit of Symfony experience is helpful but not mandatory.

What You Will Learn

  • Write a Drupal 8 module with custom functionality and hook into various extension points
  • Master numerous Drupal 8 sub-systems and APIs
  • Model, store, and manipulate data in various ways and for various purposes
  • Display data and content in a presentable and secure way
  • Learn about the theme system and render arrays

In Detail

Drupal is an open source web-based content management system (CMS) that can be used for building anything from simple websites to complex applications. It enables individuals and organizations to build platforms that engage users and deliver the right content at the right time.

Drupal 8 is an exciting new development in the Drupal community. However, the differences from the previous version are substantial and this can put quite some pressure on Drupal 7 developers that need to catch up. This book aims to help such developers in getting up to speed with Drupal 8 module development.

The book first introduces you to the Drupal 8 architecture and its subsystems before diving into creating your first module with basic functionality. Building upon that, you will cover many core APIs and functionalities available to module developers.

You will work with the Drupal logging and mailing systems, learn how to output data using the theme layer and work with menus and links programmatically. Then, you will learn how to work with different kinds of data storages, create custom entities, field types and leverage the Database API for lower level database queries. Moreover, you will learn about the Drupal 8 access system and caching layer as well as the APIs used for data processing (queues and batches).

You will further see how to introduce javascript into your module, work with the various file systems and ensure the code you write works on multilingual sites. Finally, you will learn how to programmatically work with Views, write automated tests for your functionality and also write secure code in general.

By the end of the book, you will have learned how to develop your own custom module from scratch that can help solve a small problem or even provide complex functionality. And who knows, maybe you’ll even contribute it back to the Drupal community.

Style and approach

This book aims to introduce PHP developers to programming concepts specific to Drupal 8 to get them started on writing their own functionality with Drupal 8 and create efficient Drupal 8 modules and extensions.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book 
      3. Errata
      4. Piracy
      5. Questions
  2. Developing for Drupal 8
    1. Introducing Drupal (for developers)
    2. Developing for Drupal 8
      1. Technologies that drive Drupal
        1. PHP
        2. Databases and MySQL
        3. The web server
        4. HTML, CSS, and JavaScript
      2. Drupal architecture
        1. Drupal core, modules, and themes
        2. Hooks, plugins, and events
        3. Services and the dependency injection container
        4. From request to response
      3. Drupal's major subsystems
        1. Routing
        2. Entities
        3. Fields
        4. Menus
        5. Views
        6. Forms
        7. Configuration
        8. Plugins
        9. The theme system
        10. Caching
        11. Other subsystems
      4. Tools for developing in Drupal
        1. Version control
        2. Composer
        3. The API site and coding standards
        4. The developer (Devel) module
        5. Drush (the Drupal shell)
        6. Drupal Console
        7. Developer settings
    3. Summary
  3. Creating Your First Module
    1. Creating a module
      1. Your first hook implementation
      2. Route and controller
        1. The route
          1. Route variables
        2. Namespaces
        3. The Controller
      3. Services
        1. What is a service?
        2. The HelloWorldSalutation service
        3. Tagged services
    2. Using services in Drupal 8
      1. Injecting the service into our Controller
      2. The form
        1. Altering forms
        2. Custom submit handlers
        3. Rendering forms
      3. Service dependencies
    3. Blocks
      1. Our first block plugin
      2. Block configuration
    4. Working with links
      1. The URL
      2. The link
      3. Which way to link?
    5. Event Dispatcher and redirects
      1. Redirecting from a Controller
      2. Redirecting from a subscriber
      3. Dispatching events
    6. Summary
  4. Logging and Mailing
    1. Logging
      1. The Drupal 8 logging theory
      2. Our own logger channel
      3. Our own logger
      4. Logging for Hello World
      5. Logging summary
    2. Mail API
      1. The theory of the Mail API
      2. Implementing hook_mail()
      3. Sending emails
      4. Altering someone else's emails
      5. Custom mail plugins
        1. The mail plugin
        2. Using mail plugins
    3. Tokens
      1. The Token API
      2. Using tokens
      3. Defining new tokens
      4. Token summary
    4. Summary
  5. Theming
    1. Business logic versus presentation logic
    2. Twig
    3. Theme hooks
    4. Theme hook suggestions
    5. Render arrays
      1. The structure of a render array
        1. #type
        2. #theme
        3. #markup
      2. The render pipeline
    6. Assets and libraries
      1. Libraries
        1. Attaching libraries
    7. Common theme hooks
      1. Lists
      2. Links
      3. Tables
    8. Attributes
    9. Theming our Hello World module
    10. Summary
  6. Menus and Menu Links
    1. The menu system
      1. Menus
      2. Menu links
        1. Multiple types of menu links
          1. Local tasks
          2. Local actions
          3. Contextual links
      3. MenuLink trees
      4. Menu link tree manipulators
      5. Menu active trail
    2. Rendering menus
    3. Working with menu links
      1. Defining menu links
      2. Working with menu links
    4. Defining local tasks
    5. Defining local actions
    6. Defining contextual links
    7. Summary
  7. Data Modeling and Storage
    1. Different types of data storage
    2. State API
    3. Tempstore
      1. PrivateTempStore
        1. A note about anonymous users
      2. SharedTempStore
      3. Tempstore conclusion
    4. UserData
    5. Configuration
      1. Introduction
        1. What is configuration used for?
        2. Managing configuration
      2. Different types of configuration
      3. Configuration storage
        1. Schema
        2. Overrides
          1. Global overrides
          2. Module overrides
          3. Language overrides
          4. Priority
      4. Interacting with simple configuration
    6. Entities
      1. Content versus configuration entity types
      2. Entity type plugins
        1. Identifiers
        2. Bundles
        3. Database tables
        4. Entity keys
        5. Links
        6. Entity translation
        7. Entity revisions
        8. Configuration export
        9. Handlers
      3. Fields
        1. Configuration entity fields
        2. Content entity fields
          1. Base fields
          2. Configurable fields
          3. Field storage
      4. Entity types summary
    7. TypedData
      1. Why?
      2. What?
      3. The low-level API
        1. DataType plugins
        2. Data definitions
      4. Content entities
      5. TypedData summary
    8. Interacting with the Entity API
      1. Querying and loading entities
        1. Building queries
      2. Loading entities
      3. Reading entities
      4. Manipulating entities
      5. Creating entities
      6. Rendering content entities
      7. Pseudo-fields
      8. Content entity validation
        1. Validation summary
    9. Summary
  8. Your Own Custom Entity and Plugin Types
    1. Custom content entity type
      1. Custom plugin type
      2. Custom configuration entity type
      3. The Importer plugin
      4. Content entity bundles
    2. Drush command
    3. Summary
  9. The Database API
    1. The Schema API
    2. Running queries
      1. Select queries
        1. Handling the result
        2. More complex select queries
        3. Range queries
      2. Pagers
      3. Insert queries
      4. Update queries
      5. Delete queries
      6. Transactions
      7. Query alters
      8. Update hooks
    3. Summary
  10. Custom Fields
    1. Field type
    2. Field widget
    3. Field formatter
    4. Field settings
    5. Using as a base field
    6. Summary
  11. Access Control
    1. Introduction to the Drupal access system
      1. Roles and permissions under the hood
    2. Defining permissions
    3. Checking the user credentials
    4. Route access
      1. Custom route access
        1. Static approach
        2. Service approach
      2. Programmatically checking access on routes
      3. Bonus - dynamic route options for access control
      4. CSRF protection on routes
      5. Altering routes
    5. Entity access
      1. Injecting services into Entity handlers
      2. Entity access hooks
      3. Field access
      4. Entity access in routes
      5. Node access grants
    6. Block access
    7. Summary
  12. Caching
    1. Introduction
    2. Cacheability metadata
      1. Cache tags
      2. Cache contexts
      3. Max-age
      4. Using the cache metadata
        1. Caching in block plugins
        2. Caching access results
    3. Placeholders and lazy building
      1. Lazy builders
    4. Using the Cache API
      1. Creating our own cache bin
    5. Summary
  13. JavaScript and the Ajax API
    1. JavaScript in Drupal
      1. Drupal behaviors
        1. Our library
        2. The JavaScript
      2. Drupal settings
    2. Ajax API
      1. Ajax links
      2. Ajax in forms
      3. States (Form) system
    3. Summary
  14. Internationalization and Languages
    1. Introduction
      1. Language
      2. Content Translation
      3. Configuration Translation
      4. Interface Translation
    2. Internationalization
    3. Content entities and the Translation API
    4. Summary
  15. Batches, Queues, and Cron
    1. Batch powered update hooks
    2. Batch operations
      1. Creating the batch
      2. Batch operations
    3. Cron
    4. Queues
      1. Introduction to the Queue API
      2. Cron based queue
      3. Processing a queue programmatically
      4. Lock API
    5. Summary
  16. Views
    1. Entities in Views
    2. Exposing custom data to Views
      1. Views data
        1. Views fields
        2. Views relationships
        3. Views sorts and filters
        4. Views arguments
        5. Altering Views data
    3. Custom Views field
      1. Field configuration
    4. Custom Views filter
    5. Custom Views argument
    6. Views theming
    7. Views hooks
    8. Summary
  17. Working with Files and Images
    1. The filesystem
    2. Stream wrappers
    3. Managed versus unmanaged files
    4. Using the File and Image fields
    5. Working with managed files
      1. Attaching managed files to entities
      2. Helpful functions for dealing with managed files
      3. Managed file uploads
        1. Managed file form element
        2. Entity CRUD hooks
        3. Managed file usage service
        4. Processing the CSV file
    6. Our own stream wrapper
    7. Working with unmanaged files
    8. Private file system
    9. Images
      1. Image toolkits
      2. Image styles
      3. Rendering images
    10. Summary
  18. Automated Testing
    1. Testing methodologies in Drupal 8
    2. PHPUnit
    3. Registering tests
    4. Unit tests
      1. Mocked dependencies
    5. Kernel tests
      1. TeamCleaner test
      2. CsvImporter test
    6. Functional tests
      1. Configuration for functional tests
      2. Hello World page test
      3. Hello World form test
    7. Functional JavaScript tests
      1. Time test
      2. CsvImporter test
    8. Summary
  19. Drupal 8 Security
    1. Cross-Site Scripting (XSS)
      1. Sanitization methods in Drupal 8
      2. Double escaping
    2. SQL Injection
    3. Cross-Site Request Forgery (CSRF)
    4. Summary