Friday, March 4, 2011

Create a Simple Module(Hello World) in Drupal 7 Example


Create a Simple Module(Hello World) in Drupal 7 Example

There are 2 file .info and .module



















As you can see the folder structure is as above image.

The contents of .info file are 
;$Id$
name = Fourth
description = fourth.
package = Drupal 7 Development
core = 7.x
files[] = fourth.module
;dependencies[] = autoload
php = 5.2

The contents of .module file are 

// $Id$
/**
  * @file
  * A module exemplifying Drupal coding practices and APIs.
  *
  * This module provides a block that lists all of the
  * installed modules. It illustrates coding standards,
  * practices, and API use for Drupal 7.
  */




function fourth_theme() {
    return array(
    // as in 'theme('verbose_method',...)
    'verbose_method' => array(
      // routes to file exampleModule/spanks-box-thing.tpl.php or [YOUR ACTIVE THEME DIRECTORY]/spanky-box-thing.tpl.php
      'template' => 'fourth',
      // these variables must be called in the theme function, and will appear in the template as $title, $body, $link
      'variables' => array(
        'title' => null,
        'body' => null,
        'link' => null,
      ),
    ),
  );
}    

function fourth_help($section) {
    $abc='hi';
  switch ($section) {
    case 'admin/help#fourth':
    //    return t('hi');
      return theme('verbose_method', array('text' => 'How are you?'));
  }
}


The contents of fourth.tpl.php

<div id="whatever">
<br />
jooo hello </div>
<br /></div>

Now place the folder in /sites/all/modules folder of your drupal installation.

Then go to Modules on the webpage and Enable the Fourth Module and Save the Configuration and go to Help page on webpage and click on the Fourth the template page will pop up.


There are 2 was to implement this using templates and themes function.

We will now see how we can implement the same using theme functions.

Now you saw above how we used templates for modules. Now replace fourth.module with below file.

<?php
// $Id$
/**
  * @file
  * A module exemplifying Drupal coding practices and APIs.
  *
  * This module provides a block that lists all of the
  * installed modules. It illustrates coding standards,
  * practices, and API use for Drupal 7.
  */
/**
  * Implements hook_help().
  */
 
function fourth_help($section) {
  switch ($section) {
    case 'admin/help#fourth':
      return t('This module implements an hello harshal asds thats awesome form.');
  }
}




function fourth_block_info() {
   $blocks = array();
   $blocks['list_modules'] = array(
      'info' => t('BlockList.'),
      'cache' => DRUPAL_NO_CACHE,
   );
   return $blocks;
}


/**
* Implements hook_block_view().
*/

    /*
    * Display output
    */
    function fourth_block_view($block_name = '') {
        if ($block_name == 'list_modules') {
         $list = module_list();
    $theme_args = array('items' => $list, 'type' => 'ol');
    $content = theme('item_list', $theme_args);
    $block = array(
      'subject' => t('Enabled Modules'),
          'content' => $content,
    );
      return $block;
        }
    }


Now again place the fourth folder in /sites/all/modules folder and go to webpage ->Modules and Enable the module. Then go to Structure ->Blocks and you will see the module as BlocksList. Now place the module under content. In configuration specify the theme and webpage where you want to display the modulet.

Do check out other articles on drupal..Yo

Can't Log In to Admin Page - Drupal

Can't Log In to Admin Page - Drupal


Unable to find the login page try below.

 site url followed by /user (sans the quotes)
That will bring you to the Drupal login page.