Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Drupal 9 Module Development

You're reading from   Drupal 9 Module Development Get up and running with building powerful Drupal modules and applications

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781800204621
Length 626 pages
Edition 3rd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Daniel Sipos Daniel Sipos
Author Profile Icon Daniel Sipos
Daniel Sipos
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Developing for Drupal 9 2. Chapter 2: Creating Your First Module FREE CHAPTER 3. Chapter 3: Logging and Mailing 4. Chapter 4: Theming 5. Chapter 5: Menus and Menu Links 6. Chapter 6: Data Modeling and Storage 7. Chapter 7: Your Own Custom Entity and Plugin Types 8. Chapter 8: The Database API 9. Chapter 9: Custom Fields 10. Chapter 10: Access Control 11. Chapter 11: Caching 12. Chapter 12: JavaScript and the Ajax API 13. Chapter 13: Internationalization and Languages 14. Chapter 14: Batches, Queues, and Cron 15. Chapter 15: Views 16. Chapter 16: Working with Files and Images 17. Chapter 17: Automated Testing 18. Chapter 18: Drupal Security 19. Other Books You May Enjoy

Chapter 2: Creating Your First Module

Now that we have covered some of the introductory aspects of Drupal module development, it's time to dive right into the meat of what we are doing here—module creation.

Here are some of the important topics that we will cover in this chapter:

  • Creating a new Drupal 9 module – the files that are necessary to get started
  • Creating a route and controller
  • Creating and using a service
  • Creating a form
  • Creating a custom block
  • Working with links
  • Using the Event Dispatcher

Concretely, in this chapter, we will create a new custom module called Hello World. In this module, we will define a route that maps to a controller and that outputs this age-old programming message. So, this will be our first win.

Next, we will define a service that our Controller will use to pimp out our message. After all, we don't want the same message presented to the user all day long. This simple example, however, will illustrate what services are and how to interact with the service container in order to make use of them.

Then, we will create a form where an administrator will be able to override the message shown on our page. It will be stored in configuration, and we will alter our service to make use of that configuration. The key takeaway here will be the use of the Form API. However, we will also discuss how to store some basic configuration values and add dependencies to our existing services.

Finally, we want to become a bit more flexible. Why should users only be greeted on a specific page? We will create a custom block that can be placed anywhere on the site and will display the same message. Here, we will see how block plugins are defined and how they can expose their own configuration forms to be more flexible.

Although not strictly related to our Hello World example, we will also look at how to work with links programmatically in Drupal. This is a very common task that any Drupal developer needs to do very often. Moreover, we will also look at using the Event Dispatcher component and, more importantly, subscribing to events. We'll illustrate this with a fairly common example of when you'd need to do this—performing redirects from incoming requests.

By the end of this chapter, you should have the foundational knowledge necessary to build your own module from scratch. Moreover, you should be able to understand and implement some of the most commonly used techniques in Drupal module development.

You have been reading a chapter from
Drupal 9 Module Development - Third Edition
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781800204621
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 €18.99/month. Cancel anytime