Drupal 8 – Create a Hello World custom module in Symfony framework

It’s time to get start with Drupal 8! The biggest change in this major update is that Drupal 8 is now with the Symfony PHP framework. So Drupal developer could work with those components such as ClassLoader, Routing and DependencyInjection which are provided by the Symfony framework.

Ok! Stop talking and start coding~ =D

The following example is a simplified version from the screencast recorded by Joe Shindelar @ Drupalize.Me.

1. Drupal 8 now has a new folders and files hierarchy. Now all core files are inside the core folder while the contributed/custom modules and themes folders are now located at the Drupal webroot.
drupal-8-symfony-custom-hello-world-module-1
 

2. Inside the modules folder, create the custom folder where we store all the custom modules. Also a contrib folder for storing other contributed modules which are available online.

3. Create our first Drupal 8 module folder and name it hello_world inside modules/custom.

4. Starting from Drupal 8, we could use YAML for the .info file instead of the traditional .ini format. So inside the modules/custom/hello_world folder, create hello_world.info.yml as follow.
drupal-8-symfony-custom-hello-world-module-2

hello_world.info.yml

name: Hello World
description: A simple hello world module.
core: 8.x
package: Eureka
type: module
dependencies:
  - node
  - block

 

5. Create the modules/custom/hello_world/hello_world.module as well to implement the hook_menu(). You could compare the deprecated and the new implementations. Some of the parameters are now handled in the module controller and the routing.yml.
drupal-8-symfony-custom-hello-world-module-3

hello_world.module

<?php
/**
 * @file
 * A simple hello world module.
 */

/**
 * Deprecated hook_menu() implementation.
 * It is still usable in Drupal 8.
 */
/*
function hello_world_menu() {
  return array(
    'hello' => array( 
      'title' => 'Hello World',
      'page callback' => 'hello_world_page',
      'access callback' => 'user_access', 
      'access arguments' => array('view content'), 
      'file' => 'hello.pages.inc', 
    ), 
  ); 
}
*/

/**
 * Implementation of hook_menu().
 */
function hello_world_menu() {
  return array(
    'hello' => array( 
      'title' => 'Hello World', 
      'route_name' => 'hello_world_page', 
    ), 
  ); 
} 

 

6. In the past, we may have different .inc files for different functions. Now, with Symfony, we adopt the Object Oriented Programming practice and we should recognize those .inc files into the module Controller Class. In addition, in order to auto load it, we have to follow the PSR-0 name spacing for the class path. So we create modules/custom/hello_world/lib/Drupal/hello_world/Controller/HelloWorldController.php.
drupal-8-symfony-custom-hello-world-module-4

HelloWorldController.php

<?php

namespace Drupal\hello_world\Controller;

use Drupal\Core\Controller\ControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Class HelloWorldController implements ControllerInterface {
  
  // Implementation of ControllerInterface::create method.
  public static function create(ContainerInterface $container) {
    return new static($container->get('module_handler'));
  }

  // Hello World page callback.
  public function helloWorldPage() {
    $build = array(
      '#type' => 'markup',
      '#markup' => t('Hello World!'),
    );
    return $build;
  }
}

 

6. Finally, we need to create the modules/custom/hello_world/hello_world.routing.yml for mapping the route_name to the page callback.
drupal-8-symfony-custom-hello-world-module-5

hello_world.routing.yml

hello_world_page:
  pattern: 'hello-world'
  defaults:
    _content: 'Drupal\hello_world\Controller\HelloWorldController::helloWorldPage'
  requirements:
    _permission: 'view content'

 

7. Enable the module.
drupal-8-symfony-custom-hello-world-module-6
 

8. Check if your Hello World page work by visiting /hello-world.
drupal-8-symfony-custom-hello-world-module-7
 

Done =)

Reference:

Advertisement

4 thoughts on “Drupal 8 – Create a Hello World custom module in Symfony framework”

      1. i think it just need some times to move the standard from .ini to .yml in this transition period. in the future, i guess only yml is allowed.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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