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
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:
Key Summary
- Module Location: Place sa_module in sites/all/modules/custom/sa_module within the Drupal 8 root directory.
- File Structure:
- sa_module.install: Implements hook_schema() to define database tables, including keys and indexes for schema creation. sa_module.info.yml: Declares the module’s existence; enable via http://YOUR_HOST/admin/modules or Extend menu. sa_module.routing.yml: Defines routes (e.g., sa_module.list): Path: Sets URL (e.g., /admin/list/sa_module) with a leading slash. Defaults: Specifies _controller (references AdminController) or _form (e.g., AddForm, EditForm, DeleteForm). Requirements: Enforces permissions like “Add, Edit, Delete, access content.” sa_module.module: Uses hook_menu() to define menu items; paths (e.g., admin/content/sa_module) must match sa_module.routing.yml. sa_module.links.menu.yml: Configures admin menu links, placing the module in top-level or sub-menu (e.g., Admin > Content).
- sa_module.install: Implements hook_schema() to define database tables, including keys and indexes for schema creation. sa_module.info.yml: Declares the module’s existence; enable via http://YOUR_HOST/admin/modules or Extend menu. sa_module.routing.yml: Defines routes (e.g., sa_module.list): Path: Sets URL (e.g., /admin/list/sa_module) with a leading slash. Defaults: Specifies _controller (references AdminController) or _form (e.g., AddForm, EditForm, DeleteForm). Requirements: Enforces permissions like “Add, Edit, Delete, access content.” sa_module.module: Uses hook_menu() to define menu items; paths (e.g., admin/content/sa_module) must match sa_module.routing.yml. sa_module.links.menu.yml: Configures admin menu links, placing the module in top-level or sub-menu (e.g., Admin > Content).
- sa_module.install: Implements hook_schema() to define database tables, including keys and indexes for schema creation.
- sa_module.info.yml: Declares the module’s existence; enable via http://YOUR_HOST/admin/modules or Extend menu.
- sa_module.routing.yml: Defines routes (e.g., sa_module.list): Path: Sets URL (e.g., /admin/list/sa_module) with a leading slash. Defaults: Specifies _controller (references AdminController) or _form (e.g., AddForm, EditForm, DeleteForm). Requirements: Enforces permissions like “Add, Edit, Delete, access content.”
- Path: Sets URL (e.g., /admin/list/sa_module) with a leading slash.
- Defaults: Specifies _controller (references AdminController) or _form (e.g., AddForm, EditForm, DeleteForm).
- Requirements: Enforces permissions like “Add, Edit, Delete, access content.”
- sa_module.module: Uses hook_menu() to define menu items; paths (e.g., admin/content/sa_module) must match sa_module.routing.yml.
- sa_module.links.menu.yml: Configures admin menu links, placing the module in top-level or sub-menu (e.g., Admin > Content).
- Classes:
- Controller: AdminController.php in modules/custom/sa_module/src/Controller (PSR-4 standard) manages route logic. Model: SaUserInfo.php handles database operations with methods like getAll(), add(), update(), delete(). Forms (in src/Form): AddForm.php: For adding new entries. EditForm.php: For editing existing entries. DeleteForm.php: For deleting entries.
- Controller: AdminController.php in modules/custom/sa_module/src/Controller (PSR-4 standard) manages route logic. Model: SaUserInfo.php handles database operations with methods like getAll(), add(), update(), delete(). Forms (in src/Form): AddForm.php: For adding new entries. EditForm.php: For editing existing entries. DeleteForm.php: For deleting entries.
- Controller: AdminController.php in modules/custom/sa_module/src/Controller (PSR-4 standard) manages route logic.
- Model: SaUserInfo.php handles database operations with methods like getAll(), add(), update(), delete().
- Forms (in src/Form): AddForm.php: For adding new entries. EditForm.php: For editing existing entries. DeleteForm.php: For deleting entries.
- AddForm.php: For adding new entries.
- EditForm.php: For editing existing entries.
- DeleteForm.php: For deleting entries.
- Admin Menu Integration: sa_module.links.menu.yml adds the module as a menu item/tab for accessibility.
- Installation Steps:
- Clear cache before enabling. Go to Extend > List > CUSTOM, enable sa_module. For reinstallation, uninstall, clear cache, then re-enable.
- Clear cache before enabling. Go to Extend > List > CUSTOM, enable sa_module. For reinstallation, uninstall, clear cache, then re-enable.
- Clear cache before enabling.
- Go to Extend > List > CUSTOM, enable sa_module.
- For reinstallation, uninstall, clear cache, then re-enable.
- Key Notes:
- Ensure path consistency between .module and .routing.yml. Use Pastebin code for SaUserInfo.php, AddForm.php, EditForm.php, and DeleteForm.php.
- Ensure path consistency between .module and .routing.yml. Use Pastebin code for SaUserInfo.php, AddForm.php, EditForm.php, and DeleteForm.php.
- Ensure path consistency between .module and .routing.yml.
- Use Pastebin code for SaUserInfo.php, AddForm.php, EditForm.php, and DeleteForm.php.
- Use Case: Develops a Drupal 8 module for managing user data with database integration, forms, and an admin interface.
Read More:
Update Top Taxonomical Aspects for Fintech Industry to Watch in 2022
File Structure
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.
Read More:
Update 7 ways small businesses can benefit from mobile apps
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
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.
Read More:
Update Mobile App Development Trends You will continue in 2017
Hope you find this Drupal 8 Custom Module helpful. To get latest tech updates don’t forget to subscribe our blog.
Contact us: www.inapps.net
Rate this post
Related Articles

Best Countries to Outsource Software Development (2026 Guide)
Software development outsourcing means hiring an external engineering team - usually based in another country- to design, build, or maintain software on your behalf, instead of hiring in-house. Most US, UK, and Australian companies do this to access skilled engineers faster and at a lower fully-loaded cost than domestic hiring allows.

How to Choose an Offshore Software Development Company in 2026
Search for "offshore software development company" and you'll get the same result every time: a list. Ten names, fifteen names, twenty-five names, each with a logo and a two-line pitch, and no real way to tell which one is actually right for your project

Are Developers Becoming Too Dependent on AI Tools?
AI coding tools went from novelty to daily habit in under two years, and the tools themselves keep getting better. But using a tool every day is not the same as trusting it, and a wave of 2026 research is starting to show a real gap between feeling faster with AI and actually being better at the job. Here is what the data says, and what it means for how you build and evaluate an engineering team.