How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to Thank you!


Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Drupal 8 - How to use hook_theme

In this tutorial I will show you a quick implementation of hook_theme, that will list the title/body of a FAQ nodes.

I've created a new routing and a new controller class (FaqController) for this.

  path: 'faq'
    _controller: '\Drupal\mymodule\Controller\FaqController::faq'
    _title: 'FAQ'
    _permission: 'access content'

Because I want to get all FAQ nodes, I'm injecting the entity_type.manager service into our controller class.

Drupal 8 theme hook_theme

To create a configuration form, we need to extend our form from ConfigFormBase and not from FormBase like we do in case of a normal form.

Extending the ConfigFormBase, we need also to implement the getEditableConfigNames() method, that will return an array with condiguration names that are editable.

In the buildForm() method, we need to create all form elements that will be used to input the settings. For this, we can use the form API of Drupal 8.

Of course, we can have access to our configuration object and get all our configurations from it.

Drupal 8 form config form

Similar to Drupal 7, we are able to pass custom data between form validate/submit callbacks.

In Drupal 8, there are two way of setting a key => value to $form_state: set a single key => value or multiple keys => values.

To set a single key, we can use set() method that is defined in FormStateInterface interface.

Drupal 8 form form_state

In order to attach custom validate/submit callbacks to an existing form in Drupal 8, you need to alter it.

To alter an existing form, we need to use the hook_form_alter() function.

Let's suppose for example that we have an existing form called example_form defined in a module example.

An we have a module called mymodule, where we want to alter the example_form and add our custom validate/submit callbacks.

The first step is to implement the hook_form_alter() in your mymodule.module file.

Drupal 8 form form_validate form_submit

In this tutorial I will show you how to create a simple form using Drupal 8.

In order to create a new form, we need to extend the FormBase class. The FormBase class is implementing the FormInterface interface, so our class needs to implement the methods from FormInterface.

The place where we create forms in Drupal 8 is: /mymodule/src/Form

The class name should end in 'Form', for example: ExampleForm.

The methods from FormInterface are:

Drupal 8 form drupal_get_form

In this 'How to' I will show you how to subscribe to an exisitng event.

You can read my previous article on How to dispatch an event.

1 Create a new file in root if your module

In this file, we need to create a new service and tag it as an 'event_subscriber' so Drupal will know that this service is an event subscriber.

Drupal 8 Event Dispatcher

Drupal 8 - How to dispatch an event

In this 'How to' I will show you how to dispatch an event from a module called 'othermodule'.

First, we need a class that is extending the \Symfony\Component\EventDispatcher\Event class.
This calss will contains all the information we want to send when we dispatch the event.

Place this class in \othermodule\src\Event\OtherModuleEvent.php

Drupal 8 Event Dispatcher

In Drupal 7 we used the function drupal_alter() to allow other modules to alter our data.

This function is not available any more in Drupal 8.
For Drupal 8 we have to use the following code, supposing that $data is our data that can be altered by other modules.

Drupal 8 drupal_alter alter

Sign in your account to have access to different features

Forgot your details?