1

Whenever I try to read about what are Hooks in Drupal, and how to manipulate forms or content with them, the text seem not to deal with the UI at all, but mostly with coding... Maybe I did not encounter the right reading material about UI-Hook relationship?

Can someone please detail in simple words and a simple example?

  1. What are Hooks?
  2. Do you use them via UI or only through Coding?
  3. Do I need to know PHP to use a module such as Hook form-alter?
0

3 Answers 3

2
  1. Hooks are a system to allow you to extend and alter Drupal's functionality. Specifically, a hook is a PHP function that follows a specific naming convention, so that Drupal can find them and call them appropriately.

  2. You use them only through code.

  3. You need to know PHP to write hooks. They are PHP functions that use Drupal's API and conventions. For hook_form_alter(), you at least need to understand PHP arrays and HTML form elements.

To answer your titular question, "Can I learn the Drupal Hooks issue if I've yet to learn PHP?", I suppose you can learn both at the same time, if you have past programming experience, or if you are especially dedicated.

5

Hooks can be thought of as internal Drupal events. They are also called callbacks, but because they are constructed by function-naming conventions and not by registering with a listener, they are not truly being called back. Hooks allow modules to “hook into” what is happening in the rest of Drupal.

Suppose a user logs into your Drupal web site. At the time the user logs in, Drupal fires hook_user_login.

That means that any function named according to the convention module name plus hook name will be called. For example, comment_user_login() in the comment module, locale_user_login() in the locale module, node_user_login() in the node module, and any other similarly named functions will be called.

If you were to write a custom module called spammy.module and include a function called spammy_user_login() that sent an e-mail to the user, your function would be called too, and the hapless user would receive an unsolicited e-mail at every login.

The most common way to tap into Drupal’s core functionality is through the implementation of hooks in modules.

You have to use coding to implement hooks and you need to know PHP.

For more details about the hooks Drupal supports, see the online documentation at http://api.drupal.org/api/7, and look under Components of Drupal, then “Module system (Drupal hooks).”

5
  • You have my upvote, but please move summary to the front
    – Mołot
    Jul 9, 2015 at 14:45
  • I removed the examples from the yellow (main) paragraph.
    – M a m a D
    Jul 9, 2015 at 14:51
  • If I understand right, Hook is a part in a module that calls a certain function?
    – user16289
    Jul 9, 2015 at 22:19
  • A module may have hook. Hook is any event that happens in Drupal. Inserting a node, user login, viewing a node, ... are hooks. Modules may use any hook or they may use no hook. It deponds on what the goal of module is.
    – M a m a D
    Jul 9, 2015 at 23:04
  • One more tip; The event it self is hook but in coding when we say hook_form_alter or hook_user_login what we mean by hook in the function name is the name of a module or your theme (if it is a theme name then you should include this function in the template.php)
    – M a m a D
    Jul 10, 2015 at 10:47
2

Hooks allow modules to interact with the Drupal core. A hook is a PHP function which is used to extend Drupal functionality.

Basically your modules understands hooks. Read Here about hooks.

You can use hooks through coding part only and you should at least be able to read PHP code. Soon you'll be writing it too. You don't need a lot of PHP knowledge to implement hooks. After a little practice, you will find it is much easier to work with hooks than it looks.

You can find API on the API page on drupal.org.

Watch videos and ask here for any kind of assistance you need.