Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Drupal 10 Masterclass

You're reading from  Drupal 10 Masterclass

Product type Book
Published in Dec 2023
Publisher Packt
ISBN-13 9781837633104
Pages 310 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Adam Bergstein Adam Bergstein
Profile icon Adam Bergstein

Table of Contents (31) Chapters

Preface 1. Part 1:Foundational Concepts
2. Chapter 1: What is Drupal? 3. Chapter 2: Drupal Core, Modules, and Themes 4. Chapter 3: Infrastructure and Overview of Technical Architecture 5. Chapter 4: Drupal Community 6. Chapter 5: What’s New in Drupal 10 7. Part 2:Setting up - Installing and Maintaining
8. Chapter 6: Bootstrapping, Installing, and Configuring a New Drupal Project 9. Chapter 7: Maintaining Drupal 10. Part 3:Building - Features and Configuration
11. Chapter 8: Content Structures and Multilingual 12. Chapter 9: Users, Roles, and Permissions 13. Chapter 10: Drupal Views and Display Modes 14. Chapter 11: Files, Images, and Media 15. Chapter 12: Search 16. Chapter 13: Contact Forms 17. Part 4:Using - Content Management
18. Chapter 14: Basic Content Authoring Experience 19. Chapter 15: Visual Content Management 20. Chapter 16: Content Workflows 21. Part 5:Advanced Topics
22. Chapter 17: Git, Drush, Composer, and DevOps 23. Chapter 18: Module Development 24. Chapter 19: Theme Development 25. Chapter 20: Delivering Drupal Content through APIs 26. Chapter 21: Migrating Content into Drupal 27. Chapter 22: Multisite Management 28. Index 29. Other Books You May Enjoy Appendix A - Drupal Terminology

Module Development

The 80/20 rule in Drupal relies heavily on having a robust development framework to address functional gaps found in the 20%. While some of the 20% effort applies to visual theming, enterprise Drupal applications often require customization. Drupal modules offer this capability. Modules can integrate with third-party services, add custom business logic, define unique data structures, or develop workflows when something happens within the Drupal application. Modules help developers create custom modules for Drupal applications or contribute to modules found in drupal.org for core and contributed use cases. Learning how to code can be complex, but this chapter offers an overview to help with the basics of developing a Drupal module. A comprehensive guide on developing Drupal modules is a far greater subject and one that is covered in depth by other books.

In this chapter, we’re going to cover the following main topics:

  • Concepts
  • Design patterns...

Concepts

Drupal offers a robust development framework in core that allows for the creation of modules. All types of modules leverage the same framework. A module can be found in core, be contributed to drupal.org, or be a custom module that exists only for a specific use case found in a Drupal application. Use of the framework promotes consistency and helps readily learn across modules.

Modules are set up as projects. Drupal core offers the ability to manage projects per application. This handles the installation, enabling, and disabling of projects. Some projects are intended for specific use cases only, such as debugging on a local system or performing testing on a non-production environment. This allows the same module to exist in a code base but to be managed within a specific application or environment of that application.

As a reminder from earlier chapters, module development should observe the 80/20 rule. Developing Drupal applications starts with site building. Normally...

Common patterns

As mentioned, all Drupal modules follow specific conventions and patterns that bring uniformity across core, contributed, and custom modules. While not exhaustive, there are some common patterns used within modules.

PHP patterns

Object-oriented PHP makes use of PSR standards (https://www.php-fig.org/psr/). PSR-4, which deprecated PSR-0, is a method of autoloading files. This is how Drupal modules, Symfony, and other PHP applications can make use of specific filenames and paths to load files. Files loaded in specific paths help register the code of specific patterns. For instance, a new service class can be placed in a src/Service directory to register a service.

Namespaces are PHP conventions used to organize classes, which are leveraged by Drupal modules. Every auto-loaded file makes use of this pattern, as demonstrated by a namespace for a MyService class:

namespace Drupal\my_module\Service;

Every class in a module uses the same base namespace to keep...

Module definitions

One of the most direct ways to understand these concepts is to see actual code. For example, the following fake hotel_feed module demonstrates various Drupal module capabilities based on a public, free API of hotel information (https://rapidapi.com/apidojo/api/hotels4).

Configuration

The following files demonstrate the standard configuration found in a Drupal module:

  • composer.json: Maintains the module’s composer definitions:
    {
      "name": "drupal/hotel_feed",
      "description": "A sample module that processes and renders hotel information from a feed.",
      "type": "drupal-module",
      "require": {
      }
    }
  • hotel_feed.info.yml: Maintains the module’s high-level metadata:
    name: Hotel Feed
    type: module
    description: A sample module that processes and renders hotel information from a feed.
    core: 10.x
    package: Custom
  • hotel_feed...

Summary

Developing modules in Drupal is an important way to solve the 80/20 rule and to contribute back to the community. Drupal has a robust framework that allows for modules to extend out of the box Drupal and observe standard conventions that all modules follow. Modules often make use of patterns in both Symfony and Drupal. Modules have defined best practices across configuration, PHP code, and more through constructs such as the composer.json file, automated tests, and more.

The next chapter explores theme development in Drupal, important for the visual styling of a Drupal application.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Drupal 10 Masterclass
Published in: Dec 2023 Publisher: Packt ISBN-13: 9781837633104
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}