Custom Drupal modules enable users to create new functionalities that are not parts of Drupal’s basic features. Even though there is an abundance of features provided by the default Drupal installation and the abundance of contributed modules, the Drupal 10 custom module is a good way to get down to implementing unique features.
For example is that you may have a client who would like a particular sort of user interaction feature, which is not available in Drupal community. You need a custom Drupal module to solve your queries.
Table of Contents
Instructions for how to create Drupal 10 custom module
Step 1: Installing Development Tools
This is to ensure that your development environment is properly configured before proceeding to create the actual modules. For this tutorial, you’ll require local development server with Drupal 10 up and running on it. This is particularly useful when you want to test your module on live site without it being a problem. Services such as Docker or DDEVs can help facilitate this process.
Step 2: Create the Module’s Info File
All modules within Drupal need to have an info file that supplies metadata. This file is important as it relates the Drupal 10 about the name of the module, type, description as well as other modules that this module needs to work with.
name: Custom Module
type: module
description: 'A Drupal 10 custom module'
core_version_requirement: ^10
package: Custom
dependencies:
- drupal:field
Step 3: Create the Module’s Directory and Files
To do this, go to your Drupal installation in your file explorer and find the modules/custom directory and create a new folder for your module and call it something such as custom_module. Within it, make the info file called custom_module.info.yml
Folder Structure
The folders’ structure is very important when it comes to Drupal recognizing and loading the module correctly. The following is a simple example of how your directory should look like:
drupal_root/
modules/
custom/
custom_module/
custom_module.info.yml
custom_module.module
custom_module.routing.yml
src/
Controller/
CustomModuleController.php
Form/
CustomModuleForm.php
custom_module.info.yml: It provides metadata about your module.
custom_module.module: That’s where you will perform hook functions in the main module file.
custom_module.routing.yml: In case your module provides pages or other routes, this defines them.
src/Controller/CustomModuleController.php: When applying Symfony’s MVC structure, here you will find your own custom controller class.
Step 4: Now let’s implement Routes and Controllers
In Drupal 10 , For creating routes we use .routing.yml file and to serve the logic for the route we crate controllers.
custom_module.routing.yml
custom_module.content:
path: '/custom-page'
defaults:
_controller: '\Drupal\custom_module\Controller\CustomModuleController::content'
_title: 'Custom Page'
requirements:
_permission: 'access content'
src/Controller/CustomModuleController.php
namespace Drupal\custom_module\Controller;
use Drupal\Core\Controller\ControllerBase;
class CustomModuleController extends ControllerBase {
/**
* Returns a simple page.
*
* @return array
* A render array.
*/
public function content() {
return [
'#markup' => $this->t('Welcome to the custom page!'),
];
}
}
Suppose you have a customer who desires an administrative dashboard which is custom-made. Here, this peculiar feature can be made available directly in the backend of Drupal through your own custom page by implementing routes and controller in Drupal 10 custom module .
Step 5: Create a Custom Form
For making a custom form , you have to use Drupal Form API, But before that you have to create route and respective form class.
custom_module.routing.yml
custom_module.form:
path: '/custom-form'
defaults:
_form: '\Drupal\custom_module\Form\CustomModuleForm'
_title: 'Custom Form'
requirements:
_permission: 'access content'
src/Form/CustomModuleForm.php
namespace Drupal\custom_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class CustomModuleForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'custom_module_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
\Drupal::messenger()->addMessage($this->t('Hello, @name!', ['@name' => $form_state->getValue('name')]));
}
}
Just suppose that you want to make a user registration form with some extra fields which are not available in the default Drupal setup. Such an example above shows how to create a form and process its submission by giving you a tailored solution for your project.
Step 6: Lets test the Module
You can enable your module via the Drupal admin interface or Using Drush after implementing it. Go to Extend and find it under “Custom” package and then click on Install. Check whether other users could reach domainofwebsite/custom-page via a browser and your form by visiting domainofwebsite/custom-form.
You May Also Like
- How to Efficiently Delete a Git Branch Locally and Remotely
- Difference Between Server and Serverless Architecture: A Comprehensive Guide
- Understanding Version Control: A Simple Guide
- What Are the New Features in PHP 8?
If you found this content useful, please share it with your team or leave a comment below!