Drupal Developer newsletter

Drupal hook: hook_mail_alter

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

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

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

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

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

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.

Advertise

Drupal Developer is offering services with no cost. Its free and will be free. But to maintain and distribute the salary to supporting staff, we need to offer the advertisements feature. By publishing your ads on Drupal Developer, you give a little back to the community. This will encourage us to provide the best resources available for Drupal Development. If you really like our idea and would like to publish your add on Drupal Developer, please use the site contact form and let us know what's on your mind.

Drupal module

Drupal is one of the most fascinating Open Source CMSes available on the web. With Drupal, one can publish a site in few minutes. The learning cure of Drupal is some what steep when it comes to write codes or developing a theme for Drupal. Drupal is fully dynamic, controllable CMS. So, all the aspects of the website is in full control. You can change the shortcut icon with few clicks!!! That's the power of Drupal.
Drupal offers most of the features out of the box. With installing Drupal, you will have registration, permissions, user management and content management system. What you can add to this feature is either you enhance them in your own code or tweak the behavior a bit.
The code we develop to do such things are called 'Modules' in Drupal terminology.

Programmatically create vocabulary

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(

Syndicate content

Recent comments

toolbar powered by www.mit3xxx.de