{"id":20279,"date":"2016-01-29T11:42:40","date_gmt":"2016-01-29T17:12:40","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/custom-module-in-drupal-8-in-just-8-easy-steps\/"},"modified":"2023-08-16T23:00:50","modified_gmt":"2023-08-17T04:30:50","slug":"custom-module-in-drupal-8-in-just-8-easy-steps","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/custom-module-in-drupal-8-in-just-8-easy-steps\/","title":{"rendered":"Custom Module in Drupal 8 in just 8 easy steps"},"content":{"rendered":"<p>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:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"File_Structure\"><\/span><strong>File Structure<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In Drupal 8, custom or contributed modules are kept under modules folder in the root directory.<br \/>\nGiven that our choice of machine name (module name) is \u201csa_module\u201d, start the module by creating a folder within your Drupal installation at the path: sites\/all\/modules\/custom\/sa_module.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_install_file\"><\/span><strong>Create .install file<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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 \u2018tablename\u2019 =&gt; array(table definition) for each table that the module defines. The following keys in the table definition are processed during table creation:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_infoyml_file\"><\/span><strong>Create .info.yml file<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_routingyml_file\"><\/span><strong>Create .routing.yml file<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this step we are going to create routing file which help us to navigate into Drupal module by specifying different controller action.<\/p>\n<ul>\n<li><strong>sa_module.list<\/strong>: In Drupal8 we should define route as a module_name.route_name \u2018module_name =&gt; sa_module\u2019 \u2018route_name =&gt; list\u2019<\/li>\n<li><strong>path<\/strong>: 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 \u201c\u201d.<\/li>\n<li><strong>defaults:<\/strong> In default we can specify multiple things. In our case we have introduced _controller and _form.<\/li>\n<li><strong>_controller:<\/strong> The _controller references a method on the AdminController class.<\/li>\n<li><strong>_form<\/strong>: A _form define classes which need to be introduced in order to define forms in our module. i.e AddForm, EditForm, DeleteForm<\/li>\n<li><strong>requirements:<\/strong> 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 \u201cAdd, Edit, Delete and access\u201d content permission which is common for other content section.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Create_module_file\"><\/span><strong>Create .module file<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<\/p>\n<p>In module creation we are using \u2018admin\/content\/sa_module\u2019 in sa_module.module file. This should be the same path: \u2019 \/admin\/list\/sa_module\u2019 in sa_module.routing.yml.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_Controller_Class\"><\/span><strong>Create Controller Class<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We have to create our AdminController.php according to the PSR-4 naming standard. Create a folder \u201cmodules\/custom\/sa_module\/src\/Controller\u201d. In this folder, create a file named \u201cAdminController.php\u201d.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_Model_Class_and_Forms\"><\/span><strong>Create Model Class and Forms<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<br \/>\nIn our case we have created SaUserInfo.php as a Model class and that helps to define methods like getAll(), add(), update(), delete() etc.<\/p>\n<p>\u251c\u2500\u2500 SaUserInfo.php<br \/>\n[Code for http:\/\/pastebin.com\/cjeTnTTJ]<br \/>\nTo create add\/edit\/delete feature, below are the forms with its code,<br \/>\nForm \u2502 \u251c\u2500\u2500 AddForm.php<br \/>\n[Code for http:\/\/pastebin.com\/3pRQqphk]<br \/>\nForm \u2502 \u251c\u2500\u2500 EditForm.php<br \/>\n[Code for http:\/\/pastebin.com\/jpCVNsYW]<br \/>\nForm \u2502 \u251c\u2500\u2500 DeleteForm.php<br \/>\n[Code for http:\/\/pastebin.com\/jRhcVvGB]<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Create_Menu_on_Admin\"><\/span><strong>Create Menu on Admin<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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 -&gt; Content as a Tab.<\/p>\n<p>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.<\/p>\n<p>By performing all above steps carefully we are in position to launch our own module from Drupal8 Admin panel.<\/p>\n<p>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 &gt; List &gt; 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.<\/p>\n<p>Hope you find this Drupal 8 Custom Module helpful. To get latest tech updates don\u2019t forget to subscribe our blog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Drupal 8 module works differently compared to Drupal 7 as Drupal guys have made it awesomely simple now,<\/p>\n","protected":false},"author":1,"featured_media":20280,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-20279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hire-developer"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20279"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20279\/revisions"}],"predecessor-version":[{"id":32812,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20279\/revisions\/32812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20280"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}