Hi

Is there any links to support on how to create hooks and a brief description of what hooks are and why they are used??

The reason i ask this as, having played around with drupal for several weeks now, i now have a site that looks good and and has all the functionality i require. However, still being relatively new to it all, I don't understand where or how i implement a hook.

For example, I would like to alter the form actions when a new user is added, and i believe i have to look into the users module. However, i dont know whether i just add code into the module or create a new module for the code and i also don't know how i am meant to reference the correct elements.

I have looked through the forum topics related to hooks and i, like many other newbies as it appears, haven't found anything that helps get a handle on the hooks!?

Any help or directions would be appreciated

AMRIT

Comments

rogerpfaff’s picture

amrit09’s picture

Thanks for the links

danaktivix’s picture

I'd like to second this. There's a great guide on creating a block module and a module that produces a page, which I've used successfully. But now I want to add some custom functionality to the node edit pages. I know its possible: I'm using the Organic Groups and Gmap module, and these add 1) a list of organic groups and 2) an editable location map to node forms.

So - how is it done? Well, this post is my detective attempt to find out. Maybe you'll find it useful too. The Drupal API page explains the hook system here:

http://api.drupal.org/api/5/group/hooks

A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type. To extend Drupal, a module need simply implement a hook. When Drupal wishes to allow intervention from modules, it determines which modules implement a hook and call that hook in all enabled modules that implement it.

OK... that makes sense, kinda. And the links to the hooks explain more, with a little snippet of code. More code examples below these would be brilliant - though e.g. hook_block -

http://api.drupal.org/api/5/function/hook_block

- does have a good example. We can see that there are a set of operations - $op - that will be implemented in different places in Drupal.

(Although I don't understand this: in the hook_block example above, $op can be 'configure' - so you can have some configuration code. But in the module development example - http://drupal.org/node/82967 - configuring the block is done with the hook_menu and a separate admin function (which isn't a hook.) Is there a right way? Would the 'configure' operation automatically appear on the module's configuration page? How does it do so in the module developing example - is it because the admin path is specified? So many questions!)

hook_form looks like a promising route to doing what Organic Groups and location map do -

http://api.drupal.org/api/5/function/hook_form

And there's even an example -

http://api.drupal.org/api/5/file/developer/examples/node_example.module

... er, or at least they would be if the source code was there. It ain't. Booo.

Anyway, to insert new elements into a node edit form, I think we need:

http://api.drupal.org/api/5/function/hook_form_alter

As the link above notes, 'One popular use of this hook is to add form elements to the node form.' Sounds promising.

Also, the forms API quickstart quide looks good too:

http://api.drupal.org/api/5/file/developer/topics/forms_api.html

The Drupal forms API is a powerful leap forward. It also allows for almost unlimited possibilities for custom theming, validation, and execution of forms. Even better, ANY form (even those in core) can be altered in almost any way imaginable--elements can be removed, added, and rearranged. The following pages are certainly not a comprehensive guide to this functionality, but should provide a good working foundation with which to do the most basic form creation, theming, validation, and execution.

And the form API - http://api.drupal.org/api/5/file/developer/topics/forms_api_reference.html

og.module inserts a list of groups on the site, so that the user can tick which ones she wants the node to appear in - the audience for the content. (There's more to it than this, but lets leave it at that for now.)

og.module uses og_form_alter from line 1369 to 1393. This hook passes the form into the og_form_add_og_audience at line 1454.

So, I think, you add a new form using the form API into the og_form_alter hook - using 'node_form' if you want to alter the main node editting form. I think. Anyone else like to confirm this / embellish?

So now we've worked out where it is, we need to figure out how it works. This may take a little time... what we could definitely do with is a 'hello world' example of adding to a node form.

Anyway, I'll post this for now, and get back to you when I've figured out quite what's going on.

p.s. This is also a useful page I found:

http://api.drupal.org/api/5/file/includes/common.inc

Explains things like the use of t('string') to load strings - I've seen no explanation of that anywhere else. Definitely useful for a newbie like me.

H3rnand3z’s picture

another newbie trying to undersand hooks too.

amrit09’s picture

Is there any possibility that anyone can post up an example of how to write and implement a simple hook (doesnt matter what is does) so i can see what goes on?

Thanks
AMRIT

rogerpfaff’s picture

a very simple example for hook_help()

guess you want to show up a description for your module on the module install page:

function mymodule_help($section) {
  $output = '';
  switch ($section) {
    case 'admin/modules#description':
      $output = t('This module is used to do something for you.');
      break;
  }
  return $output;
}

now for every other hook you write mymodule_hookname($params) and enter your code in the hook that might alter objects or get something from the database or will output something. somehow easy. :)

greetings
roger