Professional Documents
Culture Documents
Modules in Drupal 8
With the most recent release of Drupal—Drupal 8, it has become pretty clear that the folks at
Drupal are on a mission; one which aims to make Drupal all the more awesome and simpler.
If the release of Drupal 8 is any indication, the coming days are going to be exciting for
everyone using the platform.
One of the many things that changed with the release is how differently Drupal 8 modules
work compared to Drupal 7. It has become significantly effortless, now.
Here are the 7 steps that will help you create custom Drupal 8 modules without having to
break a sweat. However, since Drupal 8 is still in the development phase, there may be
changes brought about in the future as the code attains maturity.
When it comes to Drupal 8, the modules folder hosted in the root directory is used to keep the
contributed or custom modules.
Since we have selected the name of the module to be “demo_module”, the module created
will also have the same name. As the case is, begin by creating a folder under the Drupal
installation on the path: “sites/all/modules/custom/demo_module.”
The schema definition in Drupal is an array used to represent one or multiple tables and
indexes and keys that relate to the table/tables. A schema is described by ‘hook_schema()’
that should exist in the ‘.install file’ of the module.
In order to let Drupal know about the existence of your module, an ‘.info.yml file’ needs to be
created. The process is similar as creating an ‘.info file’ in Drupal 7.
type: module
package: Custom
version: 1.0
core: 8.x
You are required, now, to enable the module by either clicking on ‘Extend’ from menu or
following the path: ‘http://YOUR_HOST/admin/modules’.
demo_module.list:
path: ‘/demo’
defaults:
_controller: ‘\Drupal\demo_module\Controller\DemoController::demo’
_title: ‘Demo’
requirements:
In Drupal 7, the .module is required even if it doesn’t contain any code. In Drupal 8, it is
optional.
In this step, we need to create the ‘DemoController.php’ as per the naming standards as
postulated by the PSR-4. To do so, create a;
Folder ‘modules/custom/demo_module/src/Controller’.
<?php
namespace Drupal\demo_module\Controller;
class DemoController {
return array(
);
}
Step# 6: Creating the ‘Model Class and Forms’
In this step, we will introduce and make of the ‘utility/model class’ in order to acquire needed
data from relevant database and can use the necessary data in the ‘DemoController’, we have
created in the previous step for this particular module.
This set of execution steps is the one used in displaying the module we have created as a
menu. As we please or choose, the menu link can be placed parallel to ‘Top Menu’ or as
‘Admin -> Content’ like a Tab as any ‘Descended Menus’.
demo_module.list:
parent: system.admin_config_development
route_name: demo_module.list
weight: 100
If you are able to carefully follow these aforementioned steps, you can easily launch your own
module from the Admin Panel of Drupal 8, we are certain.
You can sign in to the Admin Panel of the latest Drupal 8 platform and the path can be
followed. In case the module is being installed for the first time, you are required to clean the
‘Cache’ and follow ‘Extend > List > CUSTOM (Accordion) so as to get the module enabled.
If you have not done it before installing, then you need to uninstall the module, clean up the
‘Cache’ and reinstall the module from the above path.
Hope you have found the above tips useful. Please contact us to know more about our Drupal
expertise and services.