- Drupal 8 Module Development
- Daniel Sipos
- 519字
- 2021-07-02 15:45:13
Creating a module
Creating a simple Drupal 8 module is not difficult. You only need one file to get it recognized by the core installation and to be able to enable it. In this state, it won't do much, but it will be installable. Let's first take a look at how to do this, and then we will progressively add meat to it in order to achieve the goals set out at the beginning of the chapter.
Custom Drupal 8 modules typically belong inside the /custom directory of the /modules folder found inside the root Drupal installation. You would put contributed modules inside a /contrib directory instead, in order to have a clear distinction. This is a standard practice, so that is where we will place our custom module, called Hello World.
We will start by creating a folder called hello_world. This will also be the module's machine name used in many other places. Inside, we will need to create an info file that describes our module. This file is named hello_world.info.yml. This naming structure is important--first, the module name, then info and followed by the .yml extension. You will hear about this file being often referred to as the module's info file (due to it having had the .info extension in the past before Drupal 8 used YAML).
Inside this file, we will need to add some minimal info that describes our module. We will go with something like this:
name: Hello World
description: Hello World module
type: module
core: 8.x
package: Custom
Some of this is self-explanatory, but let's see what these lines mean. The first two represent the human-readable name and description of the module. The type key means that this is a module info file rather than a theme. In Drupal 8, this has become mandatory. The core key specifies that this module works with the version 8 of Drupal, and it won't be installable on previous or future versions. Finally, we will place this in a generic Custom package so that it gets categorized in this group on the modules administration screen.
That is pretty much it. The module can now be enabled either through the UI at /admin/modules or via Drush using the drush en hello_world command.
Before we move on, let's see what other options you can add (and probably will need to add at some point or another) into the info file:
Module dependencies: If your module depends on one or more other modules, you can specify this in its info file like so:
dependencies:
- drupal:views
- ctools:ctools
The dependencies should be named in the project:module format, where project is the project name as it appears in the URL of the project on Drupal.org and module is the machine name of the module. You can even include version restrictions, for example, ctools:ctools (>=8.x-3.x).
Configuration: If your module has a general configuration form that centralizes the configuration options of the module, you can specify the route of that form in the info file. Doing so will add a link to that form on the admin/modules UI page where modules are being installed.
- Learning Python Web Penetration Testing
- arc42 by Example
- Developing Middleware in Java EE 8
- 看透JavaScript:原理、方法與實(shí)踐
- 快速念咒:MySQL入門指南與進(jìn)階實(shí)戰(zhàn)
- Oracle 18c 必須掌握的新特性:管理與實(shí)戰(zhàn)
- Android系統(tǒng)原理及開發(fā)要點(diǎn)詳解
- 動(dòng)手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- Access 2010中文版項(xiàng)目教程
- 深度學(xué)習(xí)原理與PyTorch實(shí)戰(zhàn)(第2版)
- 深入解析Java編譯器:源碼剖析與實(shí)例詳解
- Selenium WebDriver Practical Guide
- Joomla!Search Engine Optimization
- 趣學(xué)數(shù)據(jù)結(jié)構(gòu)
- Mathematica Data Visualization