The Hello World module is a simple example module used to demonstrate the basic steps of creating a custom module in Drupal.
How can I try out the Hello World module?
To try out the Hello World module, you can clone the git repository containing the example code using the provided Git clone command. After cloning, you can follow the steps outlined in the tutorial to build and test the module.
Example: Cloning the Hello World module repository
git clone https://github.com/aziz-712/D8HWexample.git modules/custom/hello_world
Tutorial: Hello World
What is the Hello World tutorial about?
The Hello World tutorial is a step-by-step guide to create a basic Drupal module called Hello World. It covers creating the module skeleton, adding a routing file, creating a controller, adding a menu link, and configuring default settings.
How do I prepare a module skeleton for the Hello World module?
To prepare a module skeleton for the Hello World module, you start by creating a module folder and a .info.yml file. This establishes the foundation for your module in Drupal.
Example: Module .info.yml file
name: 'Hello World'
type: module
description: 'A simple module to display "Hello, World!"'
core_version_requirement: ^8 || ^9
package: Custom
How do I add a routing file for the Hello World module?
To add a routing file for the Hello World module, you create a hello_world.routing.yml file and define the paths and controllers associated with your module.
Example: Routing file (hello_world.routing.yml)
hello_world.content:
path: '/hello'
defaults:
_controller: '\Drupal\hello_world\Controller\HelloController::content'
_title: 'Hello World'
requirements:
_permission: 'access content'
How do I create a basic controller for the Hello World module?
To create a basic controller for the Hello World module, you create a PHP file named HelloController.php within the /src/Controller directory of your module. This file contains the necessary code to display the “Hello, World!” message.
Example: Controller code (HelloController.php)
<?php
namespace Drupal\hello_world\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Defines HelloController class.
*/
class HelloController extends ControllerBase {
/**
* Display the markup.
*
* @return array
* Return markup array.
*/
public function content() {
return [
'#type' => 'markup',
'#markup' => $this->t('Hello, World!'),
];
}
}
How do I add a menu link for the Hello World module?
To add a menu link for the Hello World module, you create a hello_world.links.menu.yml file and define the menu link properties such as title, description, parent, and route name.
Example: Menu link definition (hello_world.links.menu.yml)
hello_world.admin:
title: 'Hello module settings'
description: 'Example of how to make an admin settings page link'
parent: system.admin_config_development
route_name: hello_world.content
weight: 100
How do I configure default settings for the Hello World module?
To configure default settings for the Hello World module, you create a hello_world.settings.yml file within the ‘config/install’ directory. This file defines the default configuration values for your module.
Example: Default settings configuration (hello_world.settings.yml)
hello:
name: 'Hank Williams'
Additional Tips
How can I link to an external resource in the menu link?
You can link to an external resource in the menu link by specifying the URL using the url
property in the hello_world.links.menu.yml file.
Example: Linking to an external resource
hello_world.external_link:
title: 'External Link'
description: 'Example of linking to an external resource'
parent: system.admin_config_development
url: http://example.com
weight: 200
How can I create an editable menu link?
To create an editable menu link, you need to use Drupal’s entity system to create a menu link. The provided PHP code snippet demonstrates how to achieve this.
Example: Creating an editable menu link via PHP
// Create an editable menu link.
// This is just an illustrative example, actual implementation may vary.
use Drupal\menu_link_content\Entity\MenuLinkContent;
$menu_link = MenuLinkContent::create([
'title' => 'My Editable Link',
'link' => ['uri' => 'internal:/my/path'],
'menu_name' => 'main-menu',
'parent' => 'menu-link:system.admin_config_development',
'expanded' => TRUE,
'weight' => 10,
]);
$menu_link->save();