You are here

What is a Drupal hook?

Ah, Drupal hooks.  One of my favorite topics.

Did you know that hooks really are one of the main reasons I love Drupal so much?  No?  Well now you know.

Here is a very casual, bird's-eye view (and I hope not-too-developer-oriented) explanation of what a Drupal hook is:

A hook is a way for you to place a piece of your own custom code to be run by Drupal at a very specific time.  Using hooks, you can ask Drupal to run a piece of your code precisely when a comment is viewed, or a node is edited or a search result is displayed.

Hooks are precisely what make Drupal so incredibly flexible and customizable.  Not only does the hook framework
allow you to essentially get pieces of code to run during predictable times within Drupal core's workflow, such as
looking at a page or deleting a page or editing a comment, but contrib modules themselves can implement hook interfaces,
so that you can in turn modify the behavior of your contrib modules too.

WOW!

No wonder Drupal so frequently wins on CMS comparisons in terms of powerfulness and flexibility.

In any case, here's a real world example:

In the node.module file (which obviously comes with core, as I can't imagine what a Drupal site would be without nodes), you
can find lots and lots of calls to the module_invoke_all() function, which simply calls any hooks that are registered
to run at these specific points in Drupal's execution.  Here are just two examples from node.module:

...

// Let modules modify the node before it is saved to the database.
    module_invoke_all('node_presave', $node);

...

// Allow modules to make their own additions to the node.
  module_invoke_all('node_view', $node, $view_mode, $langcode);

 

The sheer number of available places to hook into is incredible.  You can run your own code when a node
is viewed, updated, created, deleted or commented on.  You can add fields to a node as it is indexed or viewed.
You can modify all sorts of things about comments, blocks, forms and the search index on your site.

You can choose to add fields or simply text to user profiles through hooks, you can even do things like add
custom fields to certain content types if you're using apache-solr as a search back-end, which is a contrib module!
Do you see how awesome that is?  Apache solr isn't even Drupal core, it was a contributed module.  But they made sure,
as they implemented it, to have it call any available hooks at certain points, such as right before it writes
a node to the search index.  So I have written custom modules in the past to add extra custom stuff to the search index
for certain content types of mine.  With hooks, we can customize custom modules!  :-)
 
The way you would register your own hook is, in a custom module, implement a function that
has a very specific naming convention in order to be recognized by Drupal as a hook and a hook that has to be run
at a specific time, at that!  To be called during the viewing of a node (as in the second example above), you'd have to
name your hook

YOURMODULENAME_node_view($node, $view_mode, $langcode) {
   ...
} 

 

You can find hook's naming conventions, argument details and even sample implementations in Drupal's extensive API
documentation
.   

If you search for hook node view in Google, you're taken to the api of the hook_node_view specific function pretty quickly: https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_view/7

Once you find the Drupal API documentation on a particular hook you're trying to implement, one really useful place to look out for is the collapsed section showing core and contrib functions that already implement the particular hook:

If you expand that section, you can click on those functions to see sample implementations of that hook that are already used in Drupal.  It can help you figure out how the parameters are used or get you started with some sample code to copy and modify to your needs...

Hopefully this makes sense, and leaves you with a sense of wonder at the endless possibilities hooks let us achieve in customizing our Drupal sites.

Share

Comments

In response to a module-specific issue I posted on drupal.org, the developer suggested "looking at hook_election_candidate_post_has_enough_alter()." (The module is "Election.") Not being a developer and still a bit new with Drupal, I don't know how to pick this up and run with it, so to speak. Can you point me in the right direction? Does this involved modifying one or more files in the module's directory, or writing a very simple custom module file that goes inside that module's directory?

Hey Charles,

You typically don't modify anything in Drupal core or in a contrib module (unless you're submitting a patch - functionality that you want to contribute back to the module for everyone to use).  Otherwise, if you make changes to those files, they'll get messed up when you do Drupal/module updates later on.

You generally want your Drupal instance and modules to match exactly what the rest of the community is using on Drupal.org. :-)

So - though I haven't read through your question and the response, I'd assume you need to create a small custom module that modifies how Election is acting.

You can point me to the thread and I can see if I can help further...  Good luck!

Boriana

Here is the issue: https://www.drupal.org/node/2335025
I haven't yet asked this person (who is the module developer) about how to use this info, because I thought I should first understand more about how to use this structure.

Looks like that's a hook you can implement in your custom module.  Cool that they have this API and good documentation - look at the election_candidate.api.php file in /election/election_candidate.  It's a hook you can implement in the smallest of custom modules (I have a post about creating a custom module here: http://www.drupalwoo.com/content/blog/create-your-first-simple-drupal-custom-module).  Yours could be very minimal with just this one hook as a function.  You can keep the check they have, but also return TRUE if $num_candidates >= 1....

Again, good luck...

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.