컴퓨터 활용(한글, 오피스 등)/기타

Creating a custom module in Drupal 11

3604 2025. 1. 25. 17:40
728x90

출처: AI

Creating a custom module in Drupal 11 involves several steps. Here's a basic guide to get you started:

  1. Set Up Your Development Environment: Make sure you have Drupal installed and a development environment set up. You can use tools like XAMPP or Acquia Dev Desktop.
  2. Create a Module Directory: Create a directory for your module in the modules/custom directory of your Drupal installation. For example, modules/custom/helloworld.
  3. Create the .info.yml File: Inside your module directory, create a file named helloworld.info.yml and add the following content:
  4.  
    name: Hello World
    type: module
    description: 'A simple Hello World module.'
    core_version_requirement: ^11
    package: Custom
    
  5. yaml
  6. Create a Routing File: Inside your module directory, create a file named helloworld.routing.yml and add the following content:
  7. yaml
    helloworld.page:
      path: '/hello-world'
      defaults:
        _controller: '\Drupal\helloworld\Controller\HelloController::content'
        _title: 'Hello World'
      requirements:
        _permission: 'access content'
    
  8. Create a Controller: Inside your module directory, create a directory named src/Controller and inside that directory, create a file named HelloController.php with the following content:
  9. php
    <?php
    
    namespace Drupal\helloworld\Controller;
    
    use Drupal\Core\Controller\ControllerBase;
    
    class HelloController extends ControllerBase {
      public function content() {
        return [
          '#type' => 'markup',
          '#markup' => $this->t('Hello, World!'),
        ];
      }
    }
    
  10. Enable the Module:
    * drupal login > http://your-drupal-site/admin/modules > Extend > List > Custom > Hello World 체크 > install 버튼 체크
     
    Go to the Extend page in your Drupal admin interface and enable the "Hello World" module.
  11. Visit the Page: Navigate to http://your-drupal-site/hello-world to see your custom module in action!

This is a basic example to get you started. You can expand on this by adding more functionality, such as custom blocks, forms, and more.

For more detailed information, you can refer to the Drupal documentation on creating custom modules.

Does this help you get started with your custom module?

how to Enable the Module ?

Enabling a module in Drupal is fairly straightforward. Here’s how you can do it:

  1. Navigate to the Extend Page: In your Drupal admin interface, go to Extend. You can find this in the top navigation bar.
  2. Locate Your Module: Find the module you created (Hello World) in the list. Modules are usually listed in alphabetical order, so it should be under the "Custom" category.
  3. Enable the Module: Check the box next to your module name and click on the Install button at the bottom of the page.
  4. Confirm Installation: Once the module is enabled, you'll see a confirmation message.

That's it! Your custom module should now be enabled and ready to use. You can then navigate to http://your-drupal-site/hello-world to see your module in action.

If you encounter any issues or need further assistance, feel free to let me know! 😊

728x90
반응형