Skip directly to content

First Approach to Drupal 8

rteijeiro's picture
on Tue, 09/17/2013 - 01:08

This is my first post about module development for Drupal 8. I will try to demystify that the transition from Drupal 7 to Drupal 8 is hard and painful. Hope you find this posts helpful and you enjoy reading them as I enjoy writing them. Feel free to ask me in the comments but be patience because I receive tons of spam comments ;)

The examples below are based on the Bingo module I have developed for this posts series. You can clone the repository and take a look at the code while you are reading the post.

First of all we should take a look at .info file changes. The .info file now has YAML syntax so it's called .info.yml file:

bingo.info.yml

name: Bingo                                                                                                                                 
description: Simple Bingo game for easy draw winners for your contests.
core: 8.x
type: module
version: 8.x-dev

As you can see, it's pretty similar to the old .info files but using YAML syntax.

One big change introduced in Drupal 8 is the new Symfony-based routing system. To make an URL accessible in your module you only need to create the .routing.yml file and add the URL path with the corresponding callback, as you can see:

bingo.routing.yml

bingo:                                                                                                                                      
  path: 'bingo'
  defaults:
    _content: '\Drupal\bingo\BingoController::content'
  requirements:
    _permission: 'access content'

bingo_add:
  path: 'bingo/add'
  defaults:
    _form: '\Drupal\bingo\AddForm'
  requirements:
    _permission: 'access content'

The sample code above declare two URL paths: 'bingo' that points to a page content and 'bingo/add' that points to a form. Both paths are available if you type them in a browser but if you want to show them as menu entries, you will need to add a hook_menu to your .module file as usual:

bingo.module

/**
 * Implements hook_menu().
 */
function bingo_menu() {
  $items['bingo'] = array(
    'title' => t('Bingo'),
    'route_name' => 'bingo',
  );

  $items['bingo/add'] = array(
    'title' => t('Add participant'),
    'route_name' => 'bingo_add',
  );

As you can see you only need to declare the text for the menu entry and the path where it points to show the menu links. The routing is now managed by routing.yml file.

But, where are these menu entries pointing and where I print my pages and forms?

Well, it's a new improvement in Drupal 8 that introduces OOP paradigms and MVC patterns. Now your code should be implemented through classes and located in different files to improve readability and maintainability. For that, a new concept is introduced in Drupal 8, PSR-0 (Propose Standards Recommendation). In this group are discussed best PHP programming practices for all well known PHP frameworks. One of these best practices is namespace declaration and class autoloading. You should create a specific directory structure where you can place the files containing your classes definitions, and declare it as your namespace. You classes will be automatically loaded and will never conflict with third party classes because you are using your own namespace. You can see an example in the bingo controller class definition:

lib/Drupal/bingo/BingoController.php

<?php                                                                                                                                       

namespace Drupal\bingo;

class BingoController {

  <class definition here>

I have placed the BingoController.php file inside the lib/Drupal/bingo folder structure. You should replace bingo with the name of your module to create your own structure. Then I have declared the Drupal\bingo namespace where this class will be loaded. Also you should replace bingo with your module name in order to create your own namespace.

You can make use of classes of other namespaces only adding the route to the namespace, as you can see in the following example:

lib/Drupal/bingo/AddForm.php

<?php                                                                                                                                       

namespace Drupal\bingo;

use Drupal\Core\Form\FormInterface;

class AddForm implements FormInterface {

In the example code above, I make use of Drupal\Core\Form\FormInterface namespace to get access to it's classes and interfaced. Then I can implement the FormInterface interface and declare my own methods.

In the following post we will see how we can print our pages and forms and access database.

 

Resources

This posts are inspired in the Drupal 8 blog post series by Alex Bronstein (Thanks!!) that you can read at: http://effulgentsia.drupalgardens.com

Comments

Charles's picture

Thank you for your tutorials it's really helpful :)

Arijit Chowdhuri's picture

Great work , this is realy verry helpful post.

Post new comment