Drupal Module Creating First Simple Module Tutorial.
Now we will dive in and create our first module. Our first module will
show simple text, format it, and display it as a block in the site's
layout. We will cover the following steps:
- Creating the .info and .module files
- Creating a new module
- Using basic hooks
- Installing and configuring the module
There
are two files that every module must have (though many modules have
more). The first, the .info file, we examined above. The second file is
the .module (dot-module) file, which is a PHP script file.
A Place for the Module
In Drupal, every module is contained in its own directory. To keep
naming consistent throughout the module (a standard in Drupal), we will
name our directory with the module name.
In this practice, we will create a module named "helloword". So we
have to create a directory named "helloword". Place it within
\sites\all\modules.
Creating a .info File
Drupal components use the information in this file for module
management. The .info file is written as a PHP INI file, which is a
simple configuration file format.
The .info file must follow the standard naming conventions for modules. It must be named
.info. is the same as the directory name. Example helloword.info.
3 | description = "Our first module in drupal module development" |
A Basic .module File
A Block Hook
The hook_block() function isn't just for displaying block contents,
though. In fact, this function is responsible for displaying the block
and providing all the administration and auxiliary functions related to
this block
Write these code at helloword.module file:
12 | function helloword_block($op='list' , $delta=0, $edit=array()) { |
15 | $blocks[0]['info'] = t('Hello Word!'); |
18 | $blocks['subject'] = t('Hello Word!'); |
19 | $blocks['content'] = t('This is our first module!'); |
Our module will display information inside a Drupal block. To do
this, we need to implement the hook_block() function. When Drupal calls a
hook_block() function, Drupal passes it as many as three parameters:
$op, $delta, $edit
$op can have the following four possible values:
- list: This is passed when the module should provide information about itself.
- view: to provide content for displaying to the user.
- configure: an administration form used to configure the block.
- save: configuration information from the form data generated by configure needs to be saved.
The $delta parameter is set during a particular operation. The $edit
parameter is used during configuration (when the save operation is
called). We dont need in this example.
Installing a Module
Step #1: Enabling The Module
A module is enabled through the Drupal Administration interface. Once
logged into Drupal, navigate to Administer | Site Building | Modules in
the left-hand navigation
To activate the module, simply check the box under the Enabled heading, and then click the Save configuration button.
Displaying the Module's Content
Just as with enabling the module, this is done through the
administration interface. Go to Administer | Site Building | Blocks to
configure block placement. We will configure our module to appear in the
right sidebar.
Make sure to press the Save Blocks button at the bottom.
No comments:
Post a Comment