Drupal 10 Module Development - Fourth Edition

Book description

Build and customize powerful Drupal modules to extend your website's functionalities with this comprehensive guide

Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Explore the new features and improved capabilities of Drupal 10 core
  • Discover Drupal APIs and elevate your proficiency by leveraging PHP coding
  • Implement efficient data management and data security by creating dedicated modules

Book Description

Embark on a journey of Drupal module development with the latest edition of this must-have guide written by Daniel Sipos – a Drupal community member! This fourth edition is meticulously revised to cover the latest Drupal 10 enhancements that will help you build custom Drupal modules with an understanding of code deprecations, changing architecture, data modeling, multilingual ecosystem, and so on.

You'll begin with understanding the core components of Drupal 10 architecture, discovering its subsystems and unlocking the secrets of creating your first Drupal module. Further, you'll delve into Drupal logging and mailing systems, creating theme hooks, and rendering a layout. As you progress, you'll work with different types of data storage, custom entities, field types, and work with Database APIs for lower-level database queries. You'll learn to reap the power of JavaScript and ensure that your code works seamlessly on multilingual sites. You'll also learn to create custom views, automate tests for your functionalities, and write secure code for your Drupal apps.

By the end of this book, you'll have gained confidence in developing complex modules that can solve even the most complex business problems and might even become a valuable contributor to the Drupal community!

What you will learn

  • Gain insight into the Drupal 10 architecture for developing advanced modules
  • Master different Drupal 10 subsystems and APIs
  • Optimize data management by modeling, storing, manipulating, and processing data efficiently
  • Present data and content cleanly and securely using the theme system
  • Understand helpful functions while dealing with managed and unmanaged files
  • Ensure your Drupal app has business logic integrity with automated testing
  • Implement secure coding in Drupal

Who this book is for

If you are a Drupal developer looking to create custom modules for Drupal sites and cater to business needs, this book is your one-stop solution. Drupal 10 Module Development will be helpful for Drupal site builders and PHP developers with basic object-oriented programming skills, looking to upskill themselves in Drupal module development. A basic working experience with Symfony will be helpful but not mandatory.

Table of contents

  1. Drupal 10 Module Development
    1. Drupal 10 Module Development
  2. Contributors
    1. About the author
    2. About the reviewer
  3. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  4. Chapter 1: Developing for Drupal
    1. Introducing Drupal (for developers)
    2. How did we get to Drupal 10?
    3. Developing for Drupal
    4. Technologies that drive Drupal
      1. PHP
      2. Databases and MySQL
      3. The web server
      4. Drupal architecture
      5. Drupal’s major subsystems
      6. Tools for developing in Drupal
    5. Summary
  5. Chapter 2: Creating Your First Module
    1. Creating a module
      1. Your first hook implementation
      2. Route and controller
      3. Services
    2. Using services in Drupal
      1. Injecting the service into our Controller
      2. Invoked Controllers
    3. The Form API
      1. Altering forms
      2. Custom submit handlers
      3. Rendering forms
      4. Service dependencies
    4. Blocks
      1. Our first block plugin
      2. Block configuration
    5. Working with links
      1. The URL
      2. The link
      3. Which way to link?
    6. Event Dispatcher and redirects
      1. Redirecting from a Controller
      2. Redirecting from a subscriber
      3. Dispatching events
    7. Summary
  6. Chapter 3: Logging and Mailing
    1. Logging
      1. The Drupal logging theory
      2. Our own logger channel
      3. Our own logger
      4. Logging for Hello World
      5. Logging recap
    2. Mail API
      1. The theory behind the Mail API
      2. Implementing hook_mail()
      3. Sending emails
      4. Altering someone else’s emails
      5. Custom mail plugins
      6. Mail API recap
    3. Tokens
      1. The Token API
      2. Using tokens
      3. Defining new tokens
      4. Tokens recap
    4. Summary
  7. Chapter 4: Theming
    1. Business logic versus presentation logic
    2. Twig
    3. Theme hooks
      1. Theme hook suggestions
    4. Render arrays
      1. The structure of a render array
      2. The render pipeline
    5. Assets and libraries
      1. Libraries
    6. Common theme hooks
      1. Lists
      2. Links
      3. Tables
    7. Attributes
    8. Layouts
      1. Defining layouts
      2. Rendering a layout
    9. Theming our Hello World module
    10. Summary
  8. Chapter 5: Menus and Menu Links
    1. The menu system
      1. Menus
      2. Menu links
      3. MenuLink trees
    2. Rendering menus
    3. Working with menu links
      1. Defining menu links
      2. Manipulating menu links
    4. Defining local tasks
    5. Defining local actions
    6. Defining contextual links
    7. Summary
  9. Chapter 6: Data Modeling and Storage
    1. Different types of data storage
    2. The State API
    3. TempStore
      1. Private TempStore
      2. Shared TempStore
      3. Tempstore recap
    4. The UserData API
    5. Configuration API
      1. Introduction
      2. Configuration storage
      3. Configuration recap
    6. Entities
      1. Content versus configuration entity types
      2. Entity type plugins
      3. Fields
      4. Entity types recap
    7. TypedData
      1. Why TypedData?
      2. What is TypedData?
      3. The low-level API
      4. Content entities
      5. TypedData recap
    8. Interacting with the Entity API
      1. Querying entities
      2. Loading entities
      3. Reading entities
      4. Manipulating entities
      5. Creating entities
      6. Rendering content entities
      7. Pseudo-fields
      8. Entity validation
    9. Summary
  10. Chapter 7: Your Own Custom Entity and Plugin Types
    1. Our custom content entity type
      1. Entity updates
      2. Our custom plugin type
      3. Our custom configuration entity type
      4. The Importer plugin
      5. Content entity bundles
    2. Our own Drush command
    3. Summary
  11. Chapter 8: The Database API
    1. The Schema API
    2. Running queries
      1. Select queries
      2. Pagers
      3. Insert queries
      4. Update queries
      5. Delete queries
    3. Transactions
    4. Query alters
    5. Update hooks
    6. Post update hooks
    7. Summary
  12. Chapter 9: Custom Fields
    1. A recap of Field type plugins
    2. Field type
    3. Field widget
    4. Field formatter
    5. Field settings
    6. Using our custom field type as a base field
    7. Summary
  13. Chapter 10: Access Control
    1. Introduction to the Drupal access system
      1. Roles and permissions under the hood
      2. Defining permissions
      3. Checking the user credentials
    2. Route access
      1. Custom route access
      2. Programmatically checking access on routes
      3. Bonus – dynamic route options for access control
      4. CSRF protection on routes
      5. Altering routes
    3. Entity access
      1. Injecting services into Entity handlers
      2. Entity access hooks
      3. Field access
      4. Entity access in routes
      5. Node access grants
    4. Block access
    5. Summary
  14. Chapter 11: Caching
    1. Introduction to caching
    2. Cacheability metadata
      1. Cache tags
      2. Cache contexts
      3. max-age
      4. Using the cache metadata
    3. Placeholders and lazy building
      1. Lazy builders
    4. Using the Cache API
      1. Creating our own cache bin
    5. Summary
  15. Chapter 12: JavaScript and the Ajax API
    1. JavaScript in Drupal
      1. Drupal behaviors
      2. Drupal settings
    2. The Ajax API
      1. Ajax links
      2. Ajax in forms
    3. The States (Form) system
    4. Summary
  16. Chapter 13: Internationalization and Languages
    1. Introduction to the multilingual ecosystem
      1. Language
      2. Content translation
      3. Configuration translation
      4. Interface translation
    2. Internationalization
    3. Content entities and the translation API
    4. Summary
  17. Chapter 14: 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 queues
      3. Processing a queue programmatically
    5. The Lock API
    6. Summary
  18. Chapter 15: Views
    1. Entities in Views
    2. Exposing custom data to Views
      1. 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
  19. Chapter 16: 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
    6. Our own stream wrapper
    7. Working with unmanaged files
    8. Private filesystem
    9. Images
      1. Image toolkits
      2. Image styles
      3. Rendering images
    10. Summary
  20. Chapter 17: Automated Testing
    1. Testing methodologies in Drupal
    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
  21. Chapter 18: Drupal Security
    1. Cross-Site Scripting (XSS)
      1. Sanitization methods in Drupal
      2. Double escaping
    2. SQL Injection
    3. Cross-Site Request Forgery (CSRF)
    4. Summary
  22. Index
    1. Why subscribe?
  23. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Drupal 10 Module Development - Fourth Edition
  • Author(s): Daniel Sipos
  • Release date: April 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781837631803