• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Drupal hook

Drupal hook: hook_user

3
0

Drupal offers user management out of the box. This includes new user registration, log in, password mailer and user session management. Drupal stores password as md5 hash value. When a user provides her log in credentials, Drupal generates md5 has values for the password provided and compares it with the one stored in the database against the provided username/email address. This is a well known technique implemented by Unix.

Drupal hook: hook_mail_alter

3
0

When sending e-mails or newsletters from your website, you might want to inject some information like website name, disclaimer, copy right information, unsubscribe link and such. Just like hook_form_alter, you can use hook_mail_alter to virtually alter anything in the mail just before Drupal sends the e-mail. You can even change the mime type of the email, for example, from plain text to HTML and vice versa.
Here is the syntax/signature of the hook

  • Drupal 5: hook_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers)
  • Drupal 6, 7: hook_mail_alter(&$message)

$message has the following key-value pairs:

  • 'id': Unique id of the mail being sent.
  • 'to': The destination e-mail address(es)

Drupal hook: hook_view

3
0

The full view of a node including all the information associated with the nodes like author, node posted date, file attached to the node and any other data that are associated by other custom modules is managed by hook_view. The main goal and use of the hook_view is to allow the node modules to add extra information and manage those extra information to display when viewing the node. Nodes can have tease view as well as page view. On teaser view, only some of the information associated with the node is displayed; we can say its an overview of the node. On page view, all the information associated with the node is displayed.

<?php
/**
* Implementation of hook_view
* @param $node
* @param $teaser
* @param $page
* @return unknown_type
* @author Drupal Developer
*/

Drupal hook: hook_block

2
0

Blocks are the one of the basic and most fascinating features of Drupal. With block you can display information in the form of block by saving and utilizing the screen area. Here is an example of implementation of hook_block

<?php
/**
* Implementation of hook_block
*
* @param $op
* @param $delta
* @param $edit
* @return $block
* @author Drupal Developer
*/
function browse_info_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[1] = array(
'info' => t("Browse information by category"),
'status' => 1,
'region' => 'content',
'weight' => 0,
);

return $blocks;
case 'configure':
$am__vocabulary = _get_vocabulary();
$am__imagecache = _get_imagecache();

Drupal hook: hook_cron

2
0

You've just published a Drupal website and people find it very useful. So, they started signing up with your website and its really very good traffic now. You have planned to implement some features and you want to let know about to all the members of the site. There are two ways: you send e-mails to individuals by using their contact form or implement a system with which you create a node and the site itself sends e-mails. How about sending the e-mails at specific period of time? ohh.. yeah. that's great. Drupal's hook_run makes it happen. Since, there are more features to come, you want to do this every time you have something new to let others know about, you would use hook_cron, which performs specific tasks assigned to it periodically.

Drupal hook: hook_install

2
0

hook_install() creates tables in the database for the module to which its associated. hook_install not just restricted creating tables but it also creates vocabularies, terms in the vocabularies and sets variables to be used by modules. The hook_install set ups the basic requirement for a module to function properly. Not all the modules require to implement hook_install but which saves data to own tables and for providing extra features for which there are no tables or fields exists from where information can be fetched.

  • Drupal 5: Table definition and all other required set up for the module
  • Drupal 6, 7: drupal_install_schema('[hook]')

Here is an example of hook_install()

<?php
/**
* Implementation of hook_install
*

Drupal module hooks: install file

2
0

When Developing a custom module for Drupal, we need to use Drupal APIs and we need to make use of Drupal hooks. There are many Drupal hooks available and different hooks go in different files.

In this section we will learn about all those required hooks which should be written when developing a custom Drupal module.

First we will look at the hooks which are written in .install file.

I'd like to make a note here that don't mistaken the .module as the Drupal module. A Drupal module is consist of one or more .module file, one or more .info file and optionally, one or more .install file.

So, first we look at the hooks in .install file.

hook_install()

  • Drupal 5.x - Table structure and other items like vocabulary and its terms to be used by the module.

Programmatically create vocabulary

2
0

Imagine a situation in which you are developing a custom module and would like to create a vocabulary when ever your module is installed on a Drupal site. You can instruct the user to create a vocabulary with desired name and settings but the best approach to this situation is to create a vocabulary from the code and populate it with terms right within the module you have developed. This will reduce dependency and the user need not have to configure it manually. This will result in more use of module.

To create a vocabulary using the code you need the function taxonomy_save_vocabulary($vocabullary)
where $vocabullary is an associative array containing information about the vocabulary being created.
The $vocabulary would be:
<?php
$vocabulary = array(

Drupal hook: hook_menu

0
0

With Drupal, creating links which points to some content within the site or to some external content is quite easy using the in built interface for adding links. In Drupal, links are referred as menus, like in any desktop application. With Drupal interface for adding menus, we are just limited to add menus that points to some already existing content, like a node, or any external site. But when it comes to custom module, we have to add the menus by writing code. To add menus from the custom module, we need hook_menu function.

The hook_menu declares paths created and used by the module.
<?php
/**
* Implementation of hook_menu
*/
function hook_menu() {
$items = array();
$items['mypath'] = array(
'type' => MENU_NORMAL_MENU, // default is set to 'MENU_NORMAL_ITEM'

Syndicate content

Sitestats

Recent comments

rss-top-right.png
toolbar powered by www.iconcy.com