Drupal 9 Module Development
eBook - ePub

Drupal 9 Module Development

Get up and running with building powerful Drupal modules and applications, 3rd Edition

Daniel Sipos

Share book
  1. 626 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Drupal 9 Module Development

Get up and running with building powerful Drupal modules and applications, 3rd Edition

Daniel Sipos

Book details
Book preview
Table of contents
Citations

About This Book

Build and extend flexible Drupal sites and applications with this up-to-date, expert guide to Drupal 9 module development

Key Features

  • Explore the essential Drupal 9 APIs for module development
  • Learn how to implement data modeling, caching, architecture, and much more in your Drupal applications
  • Discover what's new in the latest Drupal core releases

Book Description

With its latest release, Drupal 9, the popular open source CMS platform has been updated with new functionalities for building complex Drupal apps with ease. This third edition of the Drupal Module Development guide covers these new Drupal features, helping you to stay on top of code deprecations and the changing architecture with every release.

The book starts by introducing you to the Drupal 9 architecture and its subsystems before showing you how to create your first module with basic functionality. You'll explore the Drupal logging and mailing systems, learn how to output data using the theme layer, and work with menus and links programmatically. Once you've understood the different kinds of data storage, this Drupal guide will demonstrate how to create custom entities and field types and leverage the Database API for lower-level database queries. You'll also learn how to introduce JavaScript into your module, work with various file systems, and ensure that your code works on multilingual sites. Finally, you'll work with Views, create automated tests for your functionality, and write secure code.

By the end of the book, you'll have learned how to develop custom modules that can provide solutions to complex business problems, and who knows, maybe you'll even contribute to the Drupal community!

What you will learn

  • Develop custom Drupal 9 modules for your applications
  • Master different Drupal 9 subsystems and APIs
  • Model, store, manipulate, and process data for effective data management
  • Display data and content in a clean and secure way using the theme system
  • Test your business logic to prevent regression
  • Stay ahead of the curve and write PHP code by implementing best practices

Who this book is for

If you are a Drupal developer looking to learn Drupal 9 to write modules for your sites, this book is for you. Drupal site builders and PHP developers with basic object-oriented programming skills will also find this book helpful. Although not necessary, some Symfony experience will help with understanding concepts easily.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Drupal 9 Module Development an online PDF/ePUB?
Yes, you can access Drupal 9 Module Development by Daniel Sipos in PDF and/or ePUB format, as well as other popular books in Computer Science & Computer Science General. We have over one million books available in our catalogue for you to explore.

Information

Year
2020
ISBN
9781800200548
Edition
3

Chapter 1: Developing for Drupal 9

Drupal is a web-based Content Management System (CMS). While it is useful out of the box, it is designed with developers in mind. The purpose of this book is to explain how Drupal can be extended in many ways and for many purposes. To this end, the version we will use will be the latest one at the time of writing this book—Drupal 9.
In this book, we will cover a wide range of development topics. We'll discuss how to create a Drupal 9 module, and as we go through the chapters, we'll cover many concepts and tips that will help you build what you need. The goal is not only to explain how things work but also to go through some examples in order to demonstrate them. Since no book can contain everything, I hope that after reading this book, you'll be able to expand on your knowledge on your own using the resources I reference and by looking into the Drupal core code itself. As helpful as such a book can be for learning any kind of software development, if you really want to progress, you will need to apply the knowledge you learned and explore the source code yourself. Only by doing this will you be able to understand complex systems with many dependencies and layers.
This chapter introduces the terminology, tools, and processes for developing modules in Drupal 9. While subsequent chapters focus on code, this chapter focuses on concepts. We'll talk about the architecture of Drupal and how you can hook into Drupal at strategic places to extend it to accomplish new tasks.
The following are the major topics we will be covering in this chapter:
  • An introduction to Drupal development
  • How did we get to Drupal 9?
  • Drupal 9 architecture
  • The major subsystems of Drupal
  • Tools for developing in Drupal
By the end of this chapter, you will understand the architectural aspects of Drupal and be ready to start writing code.

Introducing Drupal (for developers)

Out of the box, Drupal traditionally has all the standard functions of a web-based content management system:
  • Visitors can view published information on the site, navigate through menus, view listings, and individual pages, and so on.
  • Users can create accounts and leave comments.
  • Administrators can manage the site configuration and control the permissions of users.
  • Editors can create, preview, and then publish content when it is ready.
  • Content can be syndicated to RSS, where feed readers can pick up new articles as they are published.
  • With several built-in themes, even the look and feel of the site can be easily changed.
However, Drupal 8 improved on these and introduced some more powerful capabilities. For example, advanced multilingual support, content moderation, layout building, REST API, and many other features are now available out of the box. And yes, I did mean Drupal 8 because all this started with that version and is continuing with the current Drupal 9.

How did we get to Drupal 9?

Drupal 9 does not represent a great milestone in the traditional sense. It does, but not one comparable in scale to the release of Drupal 8.0 when the entire world cheered and the stock markets rallied. Rather, Drupal 9 represents proof that some decisions were made wisely when the time came to change the release approach Drupal was accustomed to.
Before Drupal 8, every few years a major version of Drupal was released. And with these releases came the joy of getting all the new features and leaving the old behind. But what also came was the pain of upgrading to these new releases. No more, said Drupal 8, which has been steadily introducing new features and functionality with each minor release until now. To the point that we have reached the end of the Drupal 8 release cycle and have gone into Drupal 9 in almost the same way as we've been going from one minor release to another, say, from 8.5 to 8.6. But then what is the difference?
In semantic versioning, minor releases mean that new features can be added as long as backward compatibility is ensured on all existing APIs. This means that even if in 8.5 you realize a public API is stupid and want to change it, you must ensure that in 8.6 it remains functioning. In a deprecated state, sure, but still compatible. Most of the time, these APIs simply delegate to the new, shiny ones. But as time goes on, the codebase gets full of this deprecated code that should not be used anymore. Enter major releases, such as Drupal 9.
Major releases allow the removal of all the deprecated code and instruct developers and users of the software to ensure that they are compatible with all the new APIs and that they ditch the old ones. So, this is exactly what Drupal 9 is doing: removing all the deprecated code from 8.9 and calling it Drupal 9. The two will be identical in many respects.
Why now, though? Why not in 4 years? Or 5? Apart from the growing codebase full of useless code and the increasing difficulty of managing these deprecations, Drupal is also facing the challenge of maintaining its dependencies. Ever since it "got off the island" and started relying on other open source libraries, it also got dependent on their life cycles. Here, the most notable is Symfony. Drupal 8 has been using Symfony 3, whose end of life is set for November 2021, and therefore the end of life of Drupal 8 needs to coincide with that. Upgrading to Symfony 4 would mean breaking backward compatibility, so a new major version of Drupal is also needed. Besides, there are plenty of libraries that stand to be updated as well, such as Twig to version 2. Which is also great.
Where does this leave our book? Everything you will learn in this book will be compatible with Drupal 9. Many of the things will also be compatible with Drupal 8, especially later versions of it, and with slight adjustments. For this reason, going forward, I will refrain from mentioning specific versions of Drupal because it makes less and less sense to do so. Except, of course, when it becomes germane to my point to be specific about the version.

Developing for Drupal

As fantastic as these features are, they will certainly not satisfy the needs of all users. To that end, Drupal's capabilities can be easily extended with modules, themes, and installation profiles. Take a look at Drupal's main website, http://drupal.org, and you will find thousands of modules that provide new features and thousands of themes that transform the look and feel of the application or website.
The flexible way Drupal can be extended and transformed through the module and theme mechanisms has led many to claim that Drupal isn't just a CMS, but a Content Management Framework (CMF), capable of being re-tooled to specific needs and functional requirements.
Establishing whether Drupal is rightly called a CMS or CMF is beyond our purpose here, but it is certain that Drupal's most tremendous asset is its extensibility. Want to use a directory server for authentication? There's a Drupal module for that. Want to export data to Comma-Separated Value (CSV) files? There are several modules for that (depending on what data you want to export). Interested in Facebook support, integration with Twitter, or adding a Share This button? Yup, there are modules for those too—all of which are available on Drupal.org and provided by developers like you.
Want to integrate Drupal with that custom tool you wrote to solve your special business needs? There may not be a module for that, but with a little bit of code, you can write your own. In fact, that is the subject of this book—providing you with the knowledge and tools to achieve your own goals.
In summary, the purpose of this book is to get you ramped up (as quickly as possible) for Drupal 9 module development. As we move chapter by chapter, we will cover the APIs and tools that you will use to build custom Drupal sites, and we won't stick to theory. Most chapters provide working, practically oriented example code designed to show you how to implement the concepts we will be talking about. We will follow Drupal coding conventions and utilize Drupal design patterns in an effort to illustrate the correct way to write code within the Drupal development context.
While I certainly can't write the exact code to meet your needs, my hope is that the code mentioned in these chapters can serve as a foundation for your bigger and better applications.
So let's get started with a few preliminary matters to better understand Drupal.

Technologies that drive Drupal

Drupal has gone through a series of different best practices for when it comes to how it should be installed. But the reality is that they are simply tailored to different needs. The most common, and the most recommended by this author, is the Composer-based approach with the Drupal community-promoted project here: https://www.drupal.org/docs/develop/using-composer/starting-a-site-using-drupal-composer-project-templates.
You can read more about how to install Drupal there, so I will not go into details here. Instead, let's talk a bit about the technologies that power (or are needed by) Drupal 9.

PHP

Drupal is written in the PHP programming language. PHP is a widely supported, multiplatform, and web-centric scripting language. Since Drupal is written in PHP, this book will largely feature code written in PHP, albeit with Drupal standard practices being kept in mind.
It is very important to note that the minimum version of PHP required for Drupal 9 to run (and install via Composer) is 7.3. Therefore, PHP 7.1 is no longer supported as it reached its end of life in December 2019.

Databases and MySQL

Drupal uses the powerful PHP Data Objects (PDO) library that is standard in PHP 7. This library is an abstraction layer that allows developers to support numerous databases, including MySQL, PostgreSQL, SQLite, and MariaDB.
The minimum database versions for Drupal 9 are as follows:
  • MySQL 5.7.8/MariaDB 10.3.7/Percona Server 5.7.8 or higher with PDO and an InnoDB-compatible primary storage engine
  • PostgreSQL 10 or higher
  • SQLite 3.26 or higher

The web server

Apache has long been the predominant web server, but it is by no means the only server. While Drupal was originally written with Apache in mind, many other web servers (including IIS, Lighttpd, and Nginx) can run Drupal.
We do not explicitly cover the web server layer anywhere in this book, primarily because development rarely requires working at that low level. However, Drupal expects a fair amount of processing from the web server layer, including the handling of URL rewriting.
The two most common web servers you'll typically run Drupal on are Apache and Nginx, with the following minimum version requirements for Drupal 9:
  • Apache 2.4.7 or higher
  • Nginx 1.1 or higher

HTML, CSS, and JavaScript

The de facto web data format is HTML styled with Cascading Style Sheets (CSS). Client-side interactive components are scripted with JavaScript. As Drupal developers, we will encounter all three of these technologies in this book. Although you don't need to be a JavaScript ninja to understand the code here, you will get the most from this book if you are comfortable with these three technologies.

Drupal architecture

In the previous section, we introduced the technologies that drive Drupal. However, how do they all fit together? How is the code organized? In this section, let me give you a quick overview of this architecture.

Drupal core, modules, and themes

From an architectural standpoint, we can break up Drupal into three pieces: its core, modules and themes.
When we discuss Drupal core, we can interpret it in two ways. A more restrictive interpretation sees it as the functionality covered by all the code it ships with, excluding modules and themes. The more widespread interpretation sees it as the total codebase it ships with (out of the box).
Although the most widespread interpretation is the latter (not least because it differentiates all the functionalities its standard installation contains versus all others provided by contributed modules and themes), it i...

Table of contents