- Ecommerce Development
- Update Custom Module in Drupal 8 in just 8 easy steps
Custom Module in Drupal 8 in just 8 easy steps is an article sent to you by the InApps editorial team. Hope readers will have more useful knowledge at www.inapps.net
You are viewing the article: Custom Module in Drupal 8 in just 8 easy steps
Drupal 8 module works differently compared to Drupal 7 as Drupal guys have made it awesomely simple now, Drupal 8 requires just 8 steps to create cool modules, these are:
In Drupal 8, custom or contributed modules are kept under modules folder in the root directory.
Given that our choice of machine name (module name) is “sa_module”, start the module by creating a folder within your Drupal installation at the path: sites/all/modules/custom/sa_module.
Create .install file
A Drupal schema definition is an array structure representing one or more tables and their related keys and indexes. A schema is defined by hook_schema(), which must live in the modulename.install file. hook_schema() should return an array mapping ‘tablename’ => array(table definition) for each table that the module defines. The following keys in the table definition are processed during table creation:
Create .info.yml file
You need to create an info yml file to tell Drupal that your module exists. This is similar to creating a .info file in Drupal 7. This will be sa_module.info.yml which code is mentioned below. Enable that module by following the path http://YOUR_HOST/admin/modules OR Click on Extend from Menu.
Create .routing.yml file
In this step we are going to create routing file which help us to navigate into Drupal module by specifying different controller action.
- sa_module.list: In Drupal8 we should define route as a module_name.route_name ‘module_name => sa_module’ ‘route_name => list’
- path: We can specify the path of module where user will be redirected once he will go to that module. Ideally this would be the URL to the route which has to have leading forward slash “”.
- defaults: In default we can specify multiple things. In our case we have introduced _controller and _form.
- _controller: The _controller references a method on the AdminController class.
- _form: A _form define classes which need to be introduced in order to define forms in our module. i.e AddForm, EditForm, DeleteForm
- requirements: Under requirements, we specify the permission which will check user needs to have to be able to view the page. In our case we have captured “Add, Edit, Delete and access” content permission which is common for other content section.
Create .module file
In Drupal 8, hook_menu() is used to define menu items only. If we have hook_menu(), we need to make sure that the route and path in sa_module. Module should match exactly with the route and path which in written in sa_module.routing.yml.
In module creation we are using ‘admin/content/sa_module’ in sa_module.module file. This should be the same path: ’ /admin/list/sa_module’ in sa_module.routing.yml.
Create Controller Class
We have to create our AdminController.php according to the PSR-4 naming standard. Create a folder “modules/custom/sa_module/src/Controller”. In this folder, create a file named “AdminController.php”.
Create Model Class and Forms
We are going to introduce utility / model class in order to fetch data from Database and communicate it with AdminController which we built for this module.
In our case we have created SaUserInfo.php as a Model class and that helps to define methods like getAll(), add(), update(), delete() etc.
[Code for http://pastebin.com/cjeTnTTJ]
To create add/edit/delete feature, below are the forms with its code,
Form │ ├── AddForm.php
[Code for http://pastebin.com/3pRQqphk]
Form │ ├── EditForm.php
[Code for http://pastebin.com/jpCVNsYW]
Form │ ├── DeleteForm.php
[Code for http://pastebin.com/jRhcVvGB]
Create Menu on Admin
These are the execution steps to display our module as a menu. We can put menu link parallel to Top Menu or any of the descended menu like Admin -> Content as a Tab.
We have to create a file sa_module.links.menu.yml which will define Menu at the Top and set routing from there to load Module listing page.
By performing all above steps carefully we are in position to launch our own module from Drupal8 Admin panel.
Please log into Drupal Admin panel and follow the path. If you are installing the module first time clear your cache first and then follow Extend > List > CUSTOM (Accordion) to enable your module. If you have already installed it, then first uninstall the module, clear your cache and reinstall it by following above path.
Hope you find this Drupal 8 Custom Module helpful. To get latest tech updates don’t forget to subscribe our blog.
Follow this to make sure you’ve got Custom Module in Drupal 8 in just 8 easy steps. Save and share with those around you these extras.
To learn more about ECOMMERCE DEVELOPMENT
Let’s create the next big thing together!
Coming together is a beginning. Keeping together is progress. Working together is success.