• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Write For US
  • News
  • About Us
  • Tools
You are here: Home / Programming / Drupal 8: Create a Simple Module

Drupal 8: Create a Simple Module

Apr 23, 2020 by Editorial Staff Leave a Comment

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 type
  • description: Say Hello World
    This is the description.
  • package: Custom
    It’s the group where the custom module appears
  • core: 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:

 

 


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Filed Under: Programming

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar




Quick Links

  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Word Set 01
  • Convert PNG to JPG Online

Subscribe

* indicates required

Share

   

Hot topics

  • A Simple YouTube Video Downloader Script in PHP 13.3k views
  • Open a URL in a new tab using JavaScript 10.7k views
  • Using Third-Party Libraries in Codeigniter 8.4k views
  • Add FTP/SFTP in Visual Studio Code 6.1k views
  • Replace “\n” with new line characters, using Notepad++ 4.8k views
  • Manually Install APK Files In Android Studio Emulator 4.7k views
  • Upload Multiple Images and Store in Database using PHP and MySQL. 4.5k views
  • How To Install And Use CKEditor In Laravel? 4.2k views
  • Simple PHP Shopping Cart 4k views
  • Spout, an awesome library for reading and writing in Excel. 3.3k views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2021 · TutsPlanet Gene Theme on Genesis Framework · Powered By BunnyCDN