Skip to Content

You are here

Developing First Drupal 8 module

As Drupal 8 is coming in few days, its time to start developing modules in Drupal 8 for better support in future. I have ported my module to Drupal 8 recently. There is bit learning curve present in every drupal version. Things become better and better in next version, same is the case with Drupal 8 about improvement but for sure that its not alike D5, D6, D7. All previous versions were not using object oriented approach but more procedure oriented. Hooks were key concepts for any custom module development, but in D8 its not the case.

In Drupal 8 YAML is most important component of any module development, every module has a .yml file in it for different purpose.
Lets take a look at higher level differences between Drupal 7 and Drupal 8.

1. .info file is replaced by .info.yml

2. Menu System:

hook_menu is no more
It has been replaced by a yourmodule.routing.yml file where you need to specify the controller and path for menu. Controllers determine the flow of the module, we will learn about controllers in further sections.

3. Permission system:

hook_permissions is no more
It has been replaced by a yourmodule.permissions.yml, permissions needs to be specified under that file.

4. Real/True MVC framework in Drupal Core:

Now Drupal uses MVC- Model View Controllers for real... Wow, Thanks to Symfony Framework in core we have Drupal with MVC. We have menu system which makes use of controllers and views as a separate system so now themers don't have to bother much to coders. Controllers need to get namespace and use few classes for proper use as follows:

namespace Drupal\yourmodulename\Controller
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;

5. Drupal 8 Forms :

As things have became object oriented we use now Class Formbase to extend to our custom form class. If your module uses form system it basically will need to extend Formbase and you are good to go with your custom forms.
For using this new form system you need to give namespace to your form and use following classes:

namespace Drupal\yourmodulename\Controller
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

hook_form converted to buildForm() function,
hook_submit converted to submitForm() function,
if your form uses validation then its same case with hook_validate.

Hope this takes your fear out for D8 a little bit in terms of development.
You can take a look at my module under

Enjoy #D8,