官术网_书友最值得收藏!

Creating Your First Module

Now that we have covered some of the introductory aspects of Drupal 8 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 8 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 the age-old programming message. So, this will be our first win.

Next, we will define a service that our Controller will use to pimp up 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 8. It's a very common task any Drupal 8 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 8 module development.

主站蜘蛛池模板: 淳安县| 公主岭市| 九龙坡区| 梧州市| 仙桃市| 利津县| 乳山市| 庆阳市| 贡嘎县| 新田县| 德兴市| 黄陵县| 桐柏县| 伊金霍洛旗| 景洪市| 瑞安市| 徐汇区| 宜丰县| 舒兰市| 开封市| 鹤山市| 孟州市| 苍溪县| 马边| 遂川县| 邓州市| 恩施市| 揭阳市| 信宜市| 芦溪县| 耒阳市| 海晏县| 灌阳县| 莆田市| 冕宁县| 甘南县| 巩义市| 峨边| 浠水县| 仙居县| 公安县|