How to create a custom page programmatically in Drupal 8/9

lakshmi , Credit to  volkotech-solutions May 27

Learn how to programmatically create custom pages in Drupal 8/9 using code and various APIs. Easy to follow steps to make pages with the desired functionality.

In Drupal 8/9, there are three steps to creating a custom page.

  1. Create a custom module in drupal.
  2. The routing must be declared in the module_name.routing.yml file.
  3. Finally, add a controller that returns the page's body.

Create a custom module:

Modules in Drupal are located in the /modules folder. We'll create two custom and contrib folders inside the /modules folder. Additional modules from drupal.org will be stored in the contrib folder, and our custom modules will be stored in the custom folder.

Create a .info.yml file:

Let's name our custom module welcome_page. Create the folder /modules/custom/welcome_page. In this folder, we'll need to create the welcome_page.info.yml file and add the following code.

name: Welcome page
description: Custom module for volkotech solutions welcome page.
type: module
core_version_requirement: ^8 || ^9

Enable a module:

For all possible ways to enable a module click here.

Using the Manage administrative menu, navigate to the Extend page in the list of modules, search the welcome page module and then select its checkbox. Scroll down to the bottom of the webpage, and then click Install.

welcomepage

Module Welcome page has been enabled successfully.

Declaring a route:

The routing information is saved in welcome_page/welcome_page.routing.yml.

welcome_Page.home_page:
 path: '/home/page'
 defaults:
   _controller: '\Drupal\welcome_page\Controller\WelcomePageController::homePage'
   _title: 'WELCOME PAGE'
 requirements:
   _permission: 'access content'

Let us take a closer look at what is contained in this YML file.

welcome_Page.my_page: This is the route's machine name. Route machine names should be formatted as module_name.sub_name.

path: This is the URL of the page on your website.

defaults: This section describes the page and title callbacks.

requirements: This defines the conditions under which the page will be displayed.

Adding a controller (page callback):

The page body is returned by the controller. It must be a registered service or a class method. Create a new subfolder "modules/custom/welcome_page/src/Controller." Create a file called "WelcomePageController.php" in this folder with the following content:

<?php
namespace Drupal\welcome_page\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Provides route responses for the Welcome page module.
*/
class WelcomePageController extends ControllerBase {
 /**
  * Returns a simple message page.
  *
  * @return array
  *   A simple renderable array.
  */
 public function homePage() {
   return [
     '#markup' => 'Welcome to volkotech solutions',
   ];
 }
}

here,

namespace: This declares the prefix that is required to fully qualify the name of the class that we are defining.

use: This allows us to use ControllerBase instead of the fully qualified name. This makes our class line much easier to read.

homePage(): The method specified in the YML file must be accessible to the public. It should return an array that can be rendered.

The structure of the custom page module:

structure

After clearing the cache, you should be able to see your customized page.

output

Conclusion:

That's all! You have successfully created your first Drupal 8/9 custom page.

Comments