Introduction

Drupal 8 philosophy and architecture is different than Drupal 7. From a backend and frontend standpoints there are new concepts. One of the new things is the adoption of certain components of Symfony framework and some external libraries such as Doctrine, Assetic, Backbone, Guzzle, Twig, etc. The MVC (Model View Controller) architecture is used and it's one of the important improvements of the drupal platform.

Drupal 8 Routing system

A route is a path defined for Drupal to return a page (content). For example, the page , '/blog' from the Appnovation website is a route. When Drupal receives a request, it tries to match the requested path to a defined and known route. If the route is not defined of course we'll see a "beautiful" 404 error page. On Drupal 7 routing parts of hook_menu() were used for creating menu entries, tabs, contextual links...

Below is a good description of how Drupal 8 processes a request:

Drupal 8 process request

Let's create our first module

Inside the "/sites/all" directory create a "modules" directory. Inside this last directory, create a directory named "appno_custom", this is the name of our module. We need to tell Drupal that the module exists, on D8 it's done on a .yml file. Here is a link to this markup languguage (yml).

Create "appno_custom.info.yml" file inside the "appno_custom" directory.

This is the structure of the file.

name: Appno custom D8 module

description: A custom module to show how D8 works

package: Custom

type: module

version: 1.0

core: 8.x

The .module file

Put appno_custom.module in the root of the appno_custom directory.

The src directory

We have to create a directory that will contain controllers, forms, plugins, tests...It should be called src, with this the controller class will be "autoloaded".

Inside "appno_custom" folder create the src subdirectory. 

The controller

Here we'll create a simple controller. In MVC applications, the controller do most of the work. Create a "Controller" directory inside the src one. Now let's create the controller file inside the "Controller" folder an name it CustomController.php. We will just return a text.

/**
* @file
* Contains \Drupal\appno_custom\Controller\CustomController.php
*/
namespace Drupal\appno_custom\Controller;

use Drupal\Core\Controller\ControllerBase; 

class CustomController extends ControllerBase {

   public function content() {

      return array(
        '#type' => 'markup',
        '#markup' => t('Hi Appnovators'),
      );
   }
}

// Comments on this code

You may see some unusual keywords on a drupal module development ^^.

Yeah as I said the D8 philosophy is different, it's an Orieted Object approach.
Let's take a look at the three words in bold.

Namespace are commonly structured as hierarchies to allow reuse of names in different contexts. For example in ths file all functions(methods) that we will declare will be used on the context "Drupal\appno_custom\Controller".

 The second keyword allow to "include" an other class. That means that we wi'll be able to call methods from this class.

The last one is extends. Inheritance is one of the most important concept of the OOP. Here this instruction means that our Controller have "ControllerBase" class as parent. Extends involve a hierarchy relationship between classes.

 A simple schema to understand relationship between two classes and more details about inheritance in OOP.

The route

The controller that we created does nothing at this point. We need to associate a route to the controller so that it can be executed.

Create a file called appno_custom.routing.yml at the root of appno_custom folder and add the following code.

appno_custom.content:

  path: 'custom'

  defaults:

    _controller: 'Drupal\appno_custom\Controller\CustomController

  _title: 'Hi Appnovators'

  requirements:

  _permission: 'acces content'

The word in bold on the code is thename of the controller without the "controller" suffix and in lowercase.

In order to view the content go to the '/custom' page, the 'Hi appnovators" message is returned.

Now that the route works and we have some markup returned, we can create a link to go directly to this page when clicking it.

Create an appno_custom.links.menu.yml file in the module root and add the following.

appno_custom.admin:

  title: 'Appno custom module settings'

  description: 'A sample drupal 8 module'

  parent: system.admin_config_development

  route_name: first_module.content

  weight: 100

We define here the admin settings for our module. Clear the cache and then go to configuration > development. You should see the menu item and by clicking on it you will go to the '/custom' page. We have our first Drupal8 module.

We can add custom blocks and other plugins to the module, will explain how to create blocks for Drupal 8 on an other post!

Read Next
MuleSoft

Mule SFTP and PGP Encryption

04 April, 2016|4 min