0

First time I am creating a custom module in Drupal 8. I have some pages having only HTML markup and JS code.

Assume I have 3 pages flow and user goes to 1st page (next button) -> 2nd page (next button) -> 3rd page.

To achieve it I have created a custom module as below. My custom module requires multiple routes with its own separate twig/view template.

The name of my module is custom_demo and it is created at /web/modules/custom/custom_demo with necessary artefacts as below.

The issue is first 2 pages work, the moment I click on next button to go on 3rd page, it does not work and shows The requested page could not be found. message.

Working

http://mysite.dd:8083/mypage/page1

http://mysite.dd:8083/mypage/page2

Not Working

http://mysite.dd:8083/mypage/page3

custom_demo.info.yml

name: Demo
type: module
description: 'Demo Description'
package: Custom
core: 8.x

custom_demo.routing.yml

custom_demo.page1:
   path: '/mypage/page1'
   defaults:
      _controller: 
        '\Drupal\custom_demo\Controller\DemoController::page1'
     _title: 'test title1'
   requirements:
   _permission: 'access content'

custom_demo.page2:
   path: '/mypage/page2'
   defaults:
     _controller: '\Drupal\custom_demo\Controller\DemoController::page2'
     _title: 'test title2'
  requirements:
    _permission: 'access content'

custom_demo.page3:
  path: '/mypage/page3'
  defaults:
    _controller: '\Drupal\custom_demo\Controller\DemoController::page3'
    _title: 'test title3'
  requirements:
    _permission: 'access content'

custom_demo.module

<?php

function custom_demo_theme($existing, $type, $theme, $path) {
return array(
  'demo_page1' => array(
    'variables' => array('title' => NULL),
    'template' => 'demo-page1',
  ),
  'demo_page2' => array(
    'variables' => array('title' => NULL),
    'template' => 'demo-page2',
  ),
  'demo_page3' => array(
    'variables' => array('title' => NULL),
    'template' => 'demo-page3',
  ),
);
}

/src/Controller/DemoController.php

<?php

 namespace Drupal\custom_demo\Controller;

 use Drupal\Core\Controller\ControllerBase;


 class DemoController extends ControllerBase {

public function page1() {
    return array(
        '#theme' => 'demo_page1',
        '#title' => 'test title1',
    );
}

public function page2() {
    return array(
        '#theme' => 'demo_page2',
        '#title' => 'test title2',
    );
}

public function page3() {
    return array(
        '#theme' => 'demo_page3',
        '#title' => 'test title3',
    );
 }
}

Twig templates

Under custom module folder I created the folder /templates and created following 3 files with some test content.

  1. demo-page1.html.twig
  2. demo-page2.html.twig
  3. demo-page3.html.twig

Can someone please help, why the http://mysite.dd:8083/mypage/page3 is not woking?

Thanks - Hitesh

  • Please edit your question so that the YML accurately reflects what your code is. Currently you've missed out the indentation, so if you were using that exact code then none of the routes would work. It makes it impossible to know whether or not you've simply got the indentation wrong – Clive Jun 7 at 11:35
  • Hi Clive, bravo brother!!! you are right...there was an issue with indentation for the 3rd route in the yml file. It's working now! – Hitesh Jun 7 at 12:15
  • What's in the templates? – Jaypan Jun 7 at 14:17
  • Hi Jaypan, twig templates are having HTML markup and JS code. Do you know how to define JS and CSS code separately and use for all 3 templates? There is a common JS code as well as specific to a page so how to do it? – Hitesh Jun 7 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.