You are currently viewing Creating a Custom Module in Drupal: A Step-by-Step Guide

Creating a Custom Module in Drupal: A Step-by-Step Guide

Drupal, the versatile content management system, allows you to extend its functionality by creating custom modules. In this tutorial, we’ll walk you through the process of creating a custom module called “Custom Addition Module.” This module takes user input, generates a random value, adds them together, and displays the result. Let’s get started!

Folder Structure

First, let’s organize our module’s folder structure:

/modules/custom/custom_addition
├── custom_addition.info.yml
├── custom_addition.routing.yml
└── src
   ├── Form
   │   └── CustomAdditionForm.php
   └── Controller
       └── CustomAdditionController.php

Now, let’s dive into the key files and their content.

custom_addition.info.yml – Module Info File

name: 'Custom Addition Module'
type: module
description: 'A module that takes user input, generates a random value, adds them, and displays the result.'
core_version_requirement: ^9 || ^10
package: Custom
dependencies:
  - drupal:form

custom_addition.routing.yml – Module Routing File

custom_addition.form:
  path: '/custom_addition'
  defaults:
    _title: 'Custom Addition'
    _controller: '\Drupal\custom_addition\Controller\CustomAdditionController::content'
  requirements:
    _permission: 'access content'

CustomAdditionForm.php – Form Class (src/Form/CustomAdditionForm.php)

<?php
namespace Drupal\custom_addition\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Messenger\MessengerInterface;

class CustomAdditionForm extends FormBase {
  protected $messenger;

  public function __construct(MessengerInterface $messenger) {
    $this->messenger = $messenger;
  }

  public static function create(ContainerInterface $container) {
    return new static($container->get('messenger'));
  }

  public function getFormId() {
    return 'custom_addition_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['user_input'] = [
      '#type' => 'number',
      '#title' => $this->t('Enter a number'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
    ];
    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $user_input = $form_state->getValue('user_input');
    $random_value = random_int(1, 100);
    $result = $user_input + $random_value;

    $this->messenger->addMessage($this->t('User input: @input', ['@input' => $user_input]));
    $this->messenger->addMessage($this->t('Random value: @random', ['@random' => $random_value]));
    $this->messenger->addMessage($this->t('Result: @result', ['@result' => $result]));
  }
}

CustomAdditionController.php – Controller Class (src/Controller/CustomAdditionController.php)

<?php
namespace Drupal\custom_addition\Controller;

use Drupal\Core\Controller\ControllerBase;

class CustomAdditionController extends ControllerBase {
  public function content() {
    return \Drupal::formBuilder()->getForm('Drupal\custom_addition\Form\CustomAdditionForm');
  }
}
//some align mistake may be form Try to sovle your selfe

Steps to Run the Module:

  1. Log in to Your Drupal Admin Panel: Access the administrative dashboard of your Drupal website using your admin credentials.
  2. Go to the Extend Page:
    • Click on “Manage” in the top menu.
    • Select “Extend” from the dropdown menu.
  3. Locate Your Custom Module:
    • On the “Extend” page, find your “Custom Addition Module” in the list of available modules.
  4. Enable the Module:
    • Check the checkbox next to your “Custom Addition Module.”
  5. Click the “Install” Button:
    • Scroll to the bottom of the page.
    • Click the “Install” button to enable the module.
  6. Clear the Cache:
    • If prompted, click the “Clear cache” button to ensure the changes take effect.
  7. Access the Form:
  8. Test Your Module:
    • You should now see the form generated by your custom module.
    • Enter a number in the input field and click the “Submit” button to test your module’s functionality.

Congratulations! You’ve successfully created a custom Drupal module to perform a simple addition task. Feel free to explore and expand upon this module for more advanced functionality.

Conclusion

In conclusion, creating a custom module in Drupal is a valuable skill that enhances your website’s capabilities. With our step-by-step guide, you can build and run a custom Drupal module efficiently. Start exploring the world of custom modules today to unlock unique website functionalities.

Happy coding! 🚀

Leave a Reply