Reader small image

You're reading from  Drupal 10 Masterclass

Product typeBook
Published inDec 2023
PublisherPackt
ISBN-139781837633104
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Adam Bergstein
Adam Bergstein
author image
Adam Bergstein

Adam Bergstein is a product engineering leader and an architect. He has been a long-time Drupal community member, a routine speaker at Drupal community events around the globe, and provided keynotes for several events. He has maintained and contributed to many Drupal projects, including Password Policy, Taxonomy Menu, and more. Adam is the lead of Simplytest, a free service, and a project that offers Drupal community members testing sandboxes. He has also worked for both agencies building Drupal applications and Drupal service providers building Drupal-related products. He has led the Drupal Community Governance Task Force and is serving a term as a community board member of the Drupal Association.
Read more about Adam Bergstein

Right arrow

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 2023Publisher: PacktISBN-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.
undefined
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

Author (1)

author image
Adam Bergstein

Adam Bergstein is a product engineering leader and an architect. He has been a long-time Drupal community member, a routine speaker at Drupal community events around the globe, and provided keynotes for several events. He has maintained and contributed to many Drupal projects, including Password Policy, Taxonomy Menu, and more. Adam is the lead of Simplytest, a free service, and a project that offers Drupal community members testing sandboxes. He has also worked for both agencies building Drupal applications and Drupal service providers building Drupal-related products. He has led the Drupal Community Governance Task Force and is serving a term as a community board member of the Drupal Association.
Read more about Adam Bergstein