Most of you are familiar with creating modules in Drupal 7, but things got changed in Drupal 8. Drupal 8 has massive change in code architecture and adhering latest industry standards.
Already Drupal 8 become the talk of town, so today’s tutorial we are working with a simple module in Drupal 8.
Module Directory
Drupal 8 has a different approach of creating modules than in the 7. We name our module as hello world. So create a directory called hello_world
under modules
directory.
Module Information
In Drupal 8 config files are type of yaml files, Symphony users must know this format already. So we create a hello_world.info.yaml
in our folder.
So the content in this file will be as follows
name: Hello World description: First Custom Drupal 8 Module package: Custom type: module version: 1.0 core: 8.x
name: Hello World
This is the title of our module shown on the extend page.type: module
This is the type of the extension, that we are creating a module typedescription: Say Hello World
This is the description.package: Custom
It’s the group where the custom module appearscore: 8.x
This informs Drupal our module is compatible with Drupal 8.x core.
Now you can see your module in the admin extend
section.
Enable the module, and now you are done.
Routing in Module
Now we have to see some actions on this module. So we have just added a route to get something while hitting URL.
Create the file hello_world.routing.yml
and enter the following parameters:
hello_world: path: '/hello/world' defaults: _controller: '\Drupal\hello_world\Controller\ExampleController::hello' _title: 'My first page in Drupal8' requirements: _permission: 'access content'
Controller for the URL Action
After we have added the necessary configurations to the router, we need to add a PHP file which will be responsible for our the display and other actions. Since Drupal 8 follows PSR-4 standards, there is a specific directory structure to follow. So create a folder called src
and folder called Controller
inside.
Now create controller called ExampleController.php
and a method called hello
in it.
<?php /** * @file * @author Rakesh James * Contains \Drupal\example\Controller\ExampleController. * Please place this file under your example(module_root_folder)/src/Controller/ */ namespace Drupal\hello_world\Controller; /** * Provides route responses for the Example module. */ class ExampleController { /** * Returns a simple page. * * @return array * A simple renderable array. */ public function hello() { $element = array( '#markup' => 'Hello world, text!', ); return $element; } } ?>
Any Drupal 8 class will also need to be namespaced. This is just to make sure there is no conflict in the class names. Remember in the routing file instructed to call a function called hello()
, so there the method in the controller.For our simple page, we can return an array with keys #markup. But you can add many more options there including the theming instructions.
Now we are done. Before we finishing our custom module clear the cache in Configuration->Development->Performance
Now, if we visit http://lcoalhost/drupal8/hello/world, we can see our custom page:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply