Create custom module in Drupal 10

Steps To create a Drupal 10 Module:

drupal10
  1. Create a directory for your module in the modules/custom directory of your Drupal installation. For example, if you want to create a module named "mymodule", create a directory called mymodule in modules/custom.

  2. Create a mymodule.info.yml file in your module directory. This file will contain metadata about your module, such as the name, description, version, and dependencies. Here is an example of what your mymodule.info.yml file might look like:

    name: My Module

    type: module

    description: My custom module for Drupal 10.

    package: Custom

    version: 1.0

    core: 10.x

    dependencies:

      - drupal:views



  3. Create a mymodule.module file in your module directory. This file will contain the PHP code for your module. In this file, you can define hooks that alter Drupal's behavior, define routes, controllers, and other functionality. Here is an example of what your mymodule.module file might look like:

    <?php


    /**

     * @file

     * Contains mymodule.module.

     */


    use Drupal\Core\Routing\RouteMatchInterface;


    /**

     * Implements hook_menu().

     */

    function mymodule_menu() {

      $items = [];


      $items['mymodule/custom-page'] = [

        'title' => 'Custom Page',

        'description' => 'A custom page for my module.',

        'page callback' => 'mymodule_custom_page',

        'access callback' => TRUE,

      ];


      return $items;

    }


    /**

     * Page callback for custom page.

     */

    function mymodule_custom_page() {

      return [

        '#markup' => 'Hello, world!',

      ];

    }


  4. Enable your module. You can do this through the Drupal UI by going to Extend (admin/modules), or by running the following command from the command line in the root of your Drupal installation: drush en mymodule.

  5. Clear the Drupal cache. You can do this through the Drupal UI by going to Configuration > Development > Performance (admin/config/development/performance), or by running the following command from the command line in the root of your Drupal installation: drush cr.

  6. That's it! Your custom module is now installed and ready to use. You can further customize and enhance it as per your requirement.