Custom Module in Drupal 8 in just 8 easy steps

Jan 29th, 2016

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:

File Structure

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.

├── SaUserInfo.php
[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.

Comments are closed.

Let's Discuss Your Project

Get free consultation and let us know your project idea to turn
it into an amazing digital product.

Let’s talk

NEWS & BLOG

Related Blogs

Why should you hire React Native developers for your project?

Hire Developer Jun 19th, 2023

Why should you hire React Native developers for your pr...

Read more
Seven Wearable App Technology Trends to Watch Out for in 2022

Hire Developer Dec 28th, 2020

Seven Wearable App Technology Trends to Watch Out for i...

Read more
How On-demand Grocery Delivery App Benefits Your Store and Expands Business

Hire Developer Jun 9th, 2020

How On-demand Grocery Delivery App Benefits Your Store ...

Read more

INQUIRY

Let's get in touch

UNITED STATES

31236 Meadowview Square,
Delmar, DE 19940, USA

Sales: +1 667 771 6758

UNITED KINGDOM

13 Layton Road, Hounslow,
London, TW3 1YJ

Sales: +44 7404 607567

INDIA

2nd Floor, Sun Avenue One, Bhudarpura, Ayojan Nagar, Nr. Shyamal Cross Road, Ahmedabad, Gujarat-380006

Sales: +91 635-261-6164

For Project Inquiries

biolah

hariantulis

depo 25 bonus 25

depo 25 bonus 25

mndrmndr.com

bonusdeposit.net

https://www.greentourstanzania.com/wp-includes/customize/

https://temp1.novotest.biz/id/

depo 25 bonus 25

https://sumberjo-blitar.desa.id/images

https://sumberjo-blitar.desa.id/data

depo 25 bonus 25 to 5x

depo 25 bonus 25

https://www.greentourstanzania.com/wp-includes/js/product/

https://smpabbs.sch.id/gacor/100/

https://smpabbs.sch.id/gacor/bonus/

deposit 25 bonus 25

depo 25 bonus 25

bonus new member 100

https://ppdb.smk-kosgoro.sch.id/data/depo 25 bonus 25https://jesus.nouvellevie.com/wp-includes/images/Getoko.iddepo 25 bonus 25https://bonus-baru.s3.ap-southeast-1.amazonaws.com/link-daftar-slot-gacor.htmlhttps://bonus-baru.s3.ap-southeast-1.amazonaws.com/scatter-pink-paling-gacor.htmlhttps://worldlisteningproject.org/wp-includes/depo25bonus25/bonus new member 100