Skip to main content

How to develop a custom page on Drupal 8

calendar_todayApril 13, 2017 Anitha

Drupal developers can create custom pages using custom templates and routing in Drupal 8. Follow Drupal coding standards. Keep modules in root directory as given below.

modules/custom
modules/contrib

In custom folder contains our own custom modules. In contrib folder contains Drupal contributed modules.

Custom Module

 

Create a custom module

Module name: example

Info.yml

Drupal 7 - example.info (module_name.info) to store metadata about the module.
Drupal 8 - example.info.yml (module_name.info.yml) to store metadata about the module.

Path: modules/custom/example/example.info.yml

Example:

name: Example Custom Module
description: 'Provides a custom functionality' 
type: module 
core: 8.x 
package: Custom

routing.yml

Drupal 7 - Have hook_menu() to declare the path.
Drupal 8 - Create the routing.yml file.

The path of the content will be defined in example.routing.yml

Path: modules/custom/example/example.routing.yml

Example:

example.icecream_list_page:
  path: '/icecream/list'
  defaults:
    _controller: '\Drupal\example\Controller\IcecreamController::icecreamPage'
    _title: 'My Custom Icecream Page'
  requirements:
    _permission: 'access content'

Explanation about Routing code

example.icecream_page - machine name of the route
path - It gives the path to the page on the site. Note the leading slash (/)
defaults - It describes the page and title callbacks
  IcecreamController: Controller class name and it should be the file name of controller
    icecreamPage: Method Name
requirements - It is a condition to be displayed under the particular page. Can specify modules, permissions that must be enabled.

Page implementation

IcecreamController.php

Drupal 7 - Have .inc or .module file to define the method.
Drupal 8 - Create the Icecreamcontroller.php file.

The path of the content will be defined in Icecreamcontroller.php

Path: modules/custom/example/src/Controller/IcecreamController.php

<?php

/**
 * @file
 * @author Anitha
 * Contains \Drupal\example\Controller\IcecreamController.
 */


namespace Drupal\example\Controller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Provides route responses for the example module.
 */
class IcecreamController extends ControllerBase {

  /**
   * Returns a simple page.
   *
   * @return array
   *   A simple renderable array.
   */
  public function icecreamPage() {
    $element = array(
      '#markup' => 'Frame all your needed contents',
    );
    return $element;
  }

}

After clearing the cache and see your custom page.

Features, Functions & Benefits

  • Can easily attach a theming function i.e  and/or tpl.php to a URL path/pattern.
  • Integration with the Context module Note : use custompage_region_tile($region_name) to include regions in custom pages, but not template variables!
  • Better for SEO and other presentations
  • To do package both data collection functions and an initial theme in a module
  • User-interface enhancement that will allow inline editing of the aggregated content items such as nodes and views on a custom page itself
  • Can do Custom Page to include a node with nid in site's default language and will know to swap it with the translation node when language is switched.

Subscribe to our blogs

The subscriber's email address.

Related Posts

Drupal 8 development
Top 10 benefits of redesigning your website with Drupal 8
When was the last time your website had a facelift or created a strategy to improve your brand visibility in versatile…
Drupal web services
Building web services with Drupal 7
A web service is a software system designed to support interoperable machine-to-machine interaction over a network. Web…
Drupal 7 PHP 7
PHP 7 can help improve Drupal site performance
Drupal is PHP 7-ready and sites that run many contrib modules, or particularly memory-intensive ones, will need more…

start with anubavam today

You have an idea we have engineers to convert your ideas into reality

Request Quote