- Drupal 9 Module Development
- Daniel Sipos Antonio De Marco
- 431字
- 2021-06-11 18:36:02
Chapter 2: Creating Your First Module
Now that we have covered some of the introductory aspects of Drupal module development, it's time to pe 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.
- 直播人才:數(shù)字電商的組織密碼
- 互聯(lián)網(wǎng)+微運(yùn)營(yíng):微運(yùn)營(yíng)管理及實(shí)戰(zhàn)全攻略
- 網(wǎng)店運(yùn)營(yíng)實(shí)務(wù)
- 配色+布局+海報(bào)+移動(dòng)電商 UI設(shè)計(jì)教程
- 開微店,超簡(jiǎn)單:裝修、經(jīng)營(yíng)、推廣一本通
- 實(shí)戰(zhàn)移動(dòng)互聯(lián)網(wǎng)營(yíng)銷:互聯(lián)網(wǎng)+營(yíng)銷的7個(gè)關(guān)鍵要素
- PrestaShop Module Development
- 電商流量數(shù)據(jù)化運(yùn)營(yíng)(數(shù)據(jù)分析與決策技術(shù)叢書)
- 雙11:世上沒有偶然的奇跡
- 微博營(yíng)銷實(shí)戰(zhàn)技巧
- 電子商務(wù)實(shí)驗(yàn)指導(dǎo)
- 直播電商全能一本通
- 網(wǎng)站運(yùn)營(yíng)直通車:網(wǎng)絡(luò)整合營(yíng)銷
- 跨界:打通線上線下,構(gòu)建商業(yè)閉環(huán)
- 疊加體驗(yàn):用互聯(lián)網(wǎng)思維設(shè)計(jì)商業(yè)模式