{"id":1719,"date":"2014-01-24T11:34:13","date_gmt":"2014-01-24T09:34:13","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1719"},"modified":"2014-01-27T15:47:47","modified_gmt":"2014-01-27T13:47:47","slug":"creating-a-drupal-7-module-from-scratch","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=1719","title":{"rendered":"Creating a Drupal 7 module from scratch"},"content":{"rendered":"<p>Drupal&#8217;s API (Application Programmer Interface) provides the golden combination of simplicity and power. Almost every phase of Drupal&#8217;s page building process can be intercepted and the data modified. But powerful modules can still be written in just a few dozen lines of code.<\/p>\n<p>Now, we are going to create a simple tutorial drupal 7 module from scratch &#8230;<\/p>\n<h2>Building your module&#8217;s basic structure<\/h2>\n<p>Let&#8217;s start by creating the basic folder structure and files for our module. A Drupal module needs to be placed in a specific directory in your Drupal installation and it needs to have some basic files so that it can be identified as a module by the Drupal system. The first thing we need to do is create a folder for our module called <code>myblock<\/code>. That folder should be kept in the <code>sites\\all\\modules\\<\/code> folder of your Drupal installation. In this folder create two blank files called\u00a0<code><code>myblock<\/code>.info<\/code> and\u00a0<code><code>myblock<\/code>.module<br \/>\n<\/code><\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1723\" alt=\"drupal-module\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module.jpg\" width=\"669\" height=\"186\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module.jpg 669w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module-300x83.jpg 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/a><\/p>\n<p>This basically describes your module&#8217;s basic properties like its name, description and on which Drupal core level it can be deployed. Once you have added these details to the <code><code><code>myblock<\/code><\/code>.info<\/code> file you should be able to see your module in the module list as shown below. You can enable your module now.<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module11.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1724\" alt=\"drupal-module11\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module11.jpg\" width=\"953\" height=\"131\" \/><\/a><\/p>\n<h2>Understanding Drupal Hooks<\/h2>\n<p>The system to extend Drupal is built completely on the concept of hooks. Hooks are based on naming. For a module to implement a hook in Drupal, it has to create a function called <code>modulename_hookname<\/code> depending on which hook it needs to implement in the <code>.module<\/code> file. Once there is an event to call the hooks, the Drupal system will call the hook implementation of all the enabled modules.<\/p>\n<p>The first hook we will implement in our basic module is the hook_block_info and hook_block_view which lets you view a block in any region you wish on your theme.<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module1.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1722\" alt=\"drupal-module1\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module1.jpg\" width=\"673\" height=\"721\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module1.jpg 673w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module1-280x300.jpg 280w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>The hook myblock_block_info will display in structure my block and we can affect it to display in the footer for example :<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module2.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1728\" alt=\"drupal-module2\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module2.jpg\" width=\"911\" height=\"406\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module2.jpg 1076w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module2-300x133.jpg 300w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module2-1024x456.jpg 1024w\" sizes=\"(max-width: 911px) 100vw, 911px\" \/><\/a><\/p>\n<p>This will display via myblock_view the following :<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module3.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1729\" alt=\"drupal-module3\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module3.jpg\" width=\"645\" height=\"528\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module3.jpg 692w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2014\/01\/drupal-module3-300x245.jpg 300w\" sizes=\"(max-width: 645px) 100vw, 645px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article we created a complete Drupal module from scratch. We used different Drupal hooks and Drupal functions to create a configuration page and to create a block which we can place on a region. Drupal provides a variety of hooks which can be used to customize and provide additional functionality over the core. Through this short and fun tutorial, we&#8217;ve proven the extensible nature of Drupal makes expanding on it an easy and pleasant task. <strong><a title=\"Create Module Drupal 7 \u2013 Part 2\" href=\"http:\/\/www.extradrm.com\/?p=1762\">this tutorial will be continued here <\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Drupal&#8217;s API (Application Programmer Interface) provides the golden combination of simplicity and power. Almost every phase of Drupal&#8217;s page building process can be intercepted and the data modified. But powerful modules can still be&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2842,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[139,6],"tags":[259,262,263],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1719"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1719"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1719\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2842"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}