Drupal 8 Module Development - Second Edition

Book description

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

Key Features

  • 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.
  • Stay up to date with the changes introduced in the new Drupal 8 releases

Book Description

Drupal 8 comes with a release cycle that allows for new functionality to be added at a much faster pace. However, this also means code deprecations and changing architecture that you need to stay on top of. This book updates the first edition and includes the new functionality introduced in versions up to, and including 8.7.

The book will first introduce you to the Drupal 8 architecture and its subsystems before diving into creating your first module with basic functionality. 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.

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, you will have learned how to develop your own custom module that can provide complex business solutions. And who knows, maybe you'll even contribute it back to the Drupal community.

What you will learn

  • Develop Drupal 8 modules that do all the things you want
  • Master numerous Drupal 8 sub-systems and APIs in the process
  • Model, store, manipulate and process data to serve your purposes
  • Display data and content in a clean and secure way using the Drupal 8 theme system
  • Test your business logic to prevent regressions
  • Stay ahead of the curve and write code following the current best practices

Who this book is for

The primary target of this book is Drupal developers who want to learn how to write modules and develop 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.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Drupal 8 Module Development Second Edition
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Foreword
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Developing for Drupal 8
    1. Introducing Drupal (for developers)
    2. Developing for Drupal 8
    3. Technologies that drive Drupal
      1. PHP
      2. Databases and MySQL
      3. The web server
        1. HTML, CSS, and JavaScript
      4. 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
      5. 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
      6. 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
    4. Summary
  8. 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. Invoked Controllers
    3. The form
      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
  9. 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
  10. 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. Layouts
      1. Defining layouts
      2. Rendering a layout
    10. Theming our Hello World module
    11. Summary
  11. 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
        1. Menu link tree manipulators
        2. 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
  12. Data Modeling and Storage
    1. Different types of data storage
    2. State API
    3. TempStore
      1. Private TempStore
      2. Shared TempStore
      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 TypedData?
      2. What is TypedData?
      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 entities
        1. Building queries
      2. Loading entities
      3. Reading entities
      4. Manipulating entities
      5. Creating entities
      6. Rendering content entities
      7. Pseudo-fields
      8. Entity validation
        1. Content entities
        2. Configuration entities
        3. Validation summary
    9. Summary
  13. 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
  14. 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
  15. 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
  16. 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
  17. Caching
    1. Introduction to Caching
    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
  18. JavaScript and the Ajax API
    1. JavaScript in Drupal
      1. Drupal behaviors
        1. Our library
        2. The JavaScript
      2. Drupal settings
    2. The Ajax API
      1. Ajax links
      2. Ajax in forms
      3. States (Form) system
    3. Summary
  19. 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
  20. 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
  21. 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
  22. 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 filesystem
    9. Images
      1. Image toolkits
      2. Image styles
      3. Rendering images
    10. Summary
  23. 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
  24. 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
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Drupal 8 Module Development - Second Edition
  • Author(s): Daniel Sipos
  • Release date: March 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789612363