{"id":3255,"date":"2019-06-13T17:38:36","date_gmt":"2019-06-13T12:08:36","guid":{"rendered":"https:\/\/www.idslogic.com\/blog\/?p=3255"},"modified":"2025-02-25T12:07:17","modified_gmt":"2025-02-25T06:37:17","slug":"easy-steps-to-creating-a-drupal-8-module","status":"publish","type":"post","link":"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module","title":{"rendered":"Easy Steps to Creating a Drupal 8 Module"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_72 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#How_to_Make_Your_Own_Customized_Module_Using_the_Latest_Version_of_Drupal\" title=\"How to Make Your Own Customized Module Using the Latest Version of Drupal\">How to Make Your Own Customized Module Using the Latest Version of Drupal<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Give_a_Name_to_Your_Module\" title=\"Give a Name to Your Module:\">Give a Name to Your Module:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Now_Create_the_Drupal_Module_yaml_File\" title=\"Now Create the Drupal Module yaml File:\">Now Create the Drupal Module yaml File:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Create_the_Routing_File\" title=\"Create the Routing File:\">Create the Routing File:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Create_the_Directory_for_controller\" title=\"Create the Directory for controller:\">Create the Directory for controller:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Now_Create_the_Controller_File\" title=\"Now Create the Controller File:\">Now Create the Controller File:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.idslogic.com\/blog\/easy-steps-to-creating-a-drupal-8-module\/#Check_if_the_Module_Functions\" title=\"Check if the Module Functions:\u00a0\">Check if the Module Functions:\u00a0<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p><span style=\"font-size: 14pt;\">Drupal 8 has come up with various new features and updates and it is very clear that the developers at Drupal are working to make it all the more awesome and simpler.\u00a0 With the release of the version 8, the coming days would be exciting for the developers as well as the users.<\/span><\/p>\n<p><span style=\"font-size: 14pt;\">The biggest advantages of CMSs like Drupal are that they help the users to create websites easily without having to depend too much on coding. But the real power lies in creating customized modules that can specifically serve your business needs.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Make_Your_Own_Customized_Module_Using_the_Latest_Version_of_Drupal\"><\/span><span style=\"font-size: 18pt; color: #008000;\">How to Make Your Own Customized Module Using the Latest Version of Drupal<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Give_a_Name_to_Your_Module\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Give a Name to Your Module:<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">The first step towards creating your<span style=\"color: #ff0000;\"> Drupal module development<\/span> is to decide the name of the module. <\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Usually in Drupal, we give the module a machine name and Drupal will identify it by the name given. <\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Remember that this name is quite different from the name that is usually seen by the user. Before giving a name, you should remember some of the following things:<\/span><\/p>\n<ol>\n<li><span style=\"font-size: 14pt;\">You should not use any spaces between the words<\/span><\/li>\n<li><span style=\"font-size: 14pt;\">The name should not start with any uppercase letter<\/span><\/li>\n<\/ol>\n<p><span style=\"font-size: 14pt;\">Drupal 8 has a clean folder structure and all the modules will be placed in the MODULE folder. So, create a <span style=\"color: #000080;\"><strong>hello-world<\/strong> <\/span>folder here and this will serve as the machine name for the module that you are creating and it will also contain all the essential files for the module to function.<\/span><\/p>\n<hr \/>\n<h3><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Now_Create_the_Drupal_Module_yaml_File\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Now Create the Drupal Module yaml File:<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">The parameters for your module should be set. This includes the title and the description of the module. The keys will be defined in the file <span style=\"color: #000080;\"><strong>hello-world.info.yml<\/strong>. <\/span><\/span><\/p>\n<p><span style=\"font-size: 14pt;\">Drupal 8 uses the YAML. The <em>name<\/em> will show the title of the module, the <em>type<\/em> will tell Drupal that we are making a module, the <em>description <\/em>will describe what the module is about, the <em>package<\/em> will show the category under which the module will be listed and lastly the <em>core<\/em> will tell Drupal that it is compatible with Drupal 8.x core.<\/span><\/p>\n<hr \/>\n<h3><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Create_the_Routing_File\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Create the Routing File: <\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">The next step is about creating a routing file for the module. This file is used by the controller to know the URL path that the module\u2019s function will execute. So, the name of your file will be <span style=\"color: #000080;\"><strong>hello-world.routing.yml. <\/strong><\/span><\/span><\/p>\n<p><span style=\"font-size: 14pt;\">The path will be used to access the module. The controller is the method that Drupal will call in order to process a request to our path.\u00a0 And the permission will ensure that only users who can access the content can see the Hello World page.<\/span><\/p>\n<div style=\"margin: 40px auto; border: 2px solid #065beb; padding: 20px 25px; border-radius: 6px; background: 0;\">\n<div style=\"display: inline-block; vertical-align: middle; width: 62%;\"><span style=\"font-size: 14pt;\"><strong>Are You Looking for Dedicated Drupal Developer?<\/strong><\/span><\/div>\n<div style=\"display: inline-block; vertical-align: middle; width: 36%;\"><span style=\"font-size: 14pt;\"><a style=\"padding: 10px 22px; margin-bottom: 0; line-height: 1.3em; text-align: center; text-decoration: none; vertical-align: middle; cursor: pointer; color: #ffffff; background-color: #065beb; -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px; display: block;\" href=\"https:\/\/www.idslogic.com\/dedicated-drupal-developer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Call us today!<\/a><\/span><\/div>\n<\/div>\n<hr \/>\n<h3><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Create_the_Directory_for_controller\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Create the Directory for controller: <\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">The next step is to create a new subdirectory under the module folder that will hold the controller file.\u00a0 All the external plugins, controllers and etc. are placed under this directory. <\/span><\/p>\n<p><span style=\"font-size: 14pt;\">All the controllers and external plugins will be placed under this director and the name is<span style=\"color: #000080;\"> <strong>src<\/strong><\/span> which is the short for source.\u00a0 <\/span><\/p>\n<p><span style=\"font-size: 14pt;\">So create a folder with the name src under your module\u2019s root directory and then create a new folder under it and name it Controller.<\/span><\/p>\n<hr \/>\n<h3><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Now_Create_the_Controller_File\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Now Create the Controller File: <\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">We will then create the controller file that will control the functionality of the module. But remember that the name is relatable so that it can easily be identified. In this case, we have the name <span style=\"color: #000080;\"><strong>HelloWorldController.php.<\/strong><\/span><\/span><\/p>\n<hr \/>\n<h3><\/h3>\n<h3><span class=\"ez-toc-section\" id=\"Check_if_the_Module_Functions\"><\/span><span style=\"font-size: 16pt; color: #008080;\"><strong>Check if the Module Functions:\u00a0 <\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-size: 14pt;\">This is the last step. You can now login to your Drupal site and then enable the module in case it is not done. you can test, if it functions properly and also visit the path that you specified in the routing file. In case you get the \u201cpage not found\u201d error, then clear the cache. Recheck to find if it is functioning properly.<\/span><\/p>\n<blockquote>\n<p style=\"box-shadow: 0 0 16px #cccccc; padding: 10px;\"><span style=\"font-size: 14pt;\"><strong>Also Read:\u00a0<\/strong><a href=\"\/blog\/drupal-8-vs-umbraco-a-comparison-based-on-the-cms-features\"><span style=\"color: #ff0000;\">Drupal 8 vs Umbraco: A Comparison Based On The CMS Features<\/span><\/a><\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Drupal 8 has come up with various new features and updates and it is very clear that the developers at Drupal are working to make it all the more awesome and simpler.\u00a0 With the release of the version 8, the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":3258,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256],"tags":[870,871,872,534],"class_list":["post-3255","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cms","tag-drupal-8-development","tag-drupal-8-module","tag-drupal-8-module-development","tag-drupal-development"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/3255","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/comments?post=3255"}],"version-history":[{"count":3,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/3255\/revisions"}],"predecessor-version":[{"id":10743,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/posts\/3255\/revisions\/10743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media\/3258"}],"wp:attachment":[{"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/media?parent=3255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/categories?post=3255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idslogic.com\/blog\/wp-json\/wp\/v2\/tags?post=3255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}