"; var wpa_add_test = "no";
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.
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:
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.
In this step we are going to create routing file which help us to navigate into Drupal module by specifying different controller action.
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.
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”.
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]
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.
@2022 Solution Analysts Pvt Ltd. All Right Reserved