The core of the CMS Drupal is not based in object-orientation in version 7. For writing pluggable modules, it has a "hook" system, whereby you can alter the behavior (by altering passed data) for defined system functions.

For instance, Drupal ha and API through which you create arrays to define forms that are translated into HTML. There is a defined hook called hook_form_alter whereby you can alter any form array after its created, and before its rendered, allowing you to change the form entirely. Similarly, you can do the same for queries, emails, etc generated through the API.

I am not well-versed in OO or design patterns, so background and expoundings are helpful. Also, I'm coming out of only PHP (and MySQL), so I'm not familiar with examples of how other languages do things.

Since these are all based on functions and callbacks, I wonder what object-oriented design patterns give you this functionality.

2 Answers 2


What you may be looking for is Aspect Oriented Programming, which is an extension of OO that adds a facility for performing hooks (called "pointcuts") at method invocations selected by a description of the calls to intercept.

Alternatively, the Decorator pattern is a pure OO way of achieving some of the same results, but does require more work to set up.


Isn't this the delegation pattern?

E.g. define an AlterFormProcessor interface for your hook delegate, add .setAlterFormProcessor(AlterFormProcessor hook) to your service. If the hook has been set, call it and collect the result.

For even more complex forms of processing, implement .addWhateverProcessor() instead of .setWhateverProcessor and run whole pipelines of delegated processors. Remember how event processing is done in AWT or in browser JavaScript.

  • Well, I can't say I'm well-versed in OO, so I can't answer your question. The wikipedia page says "instead of performing one of its stated tasks, delegates that task to an associated helper object". By this definition, Drupal hooks are not delegates; the original function still does its job, but a hook_alter is written later to change its basic functionality, even up to disabling it. So the hook system is designed to fundamentally change the original behavior, not perform a delegated responsibility.
    – user1936
    Commented Oct 2, 2015 at 18:28
  • 1
    @user1936: Imagine the following code: if (myHook != null) return myHook.execute(arguments) else {...}. With this you can allow a hook to overtake the default processing, if set, and run default processing if not set. You can see it as having a default delegate which is set if no other is set; that default may not be completely dummy. (Disclaimer: I'm also not well-versed in the GoF terminology, and stay away from too much OOP.)
    – 9000
    Commented Oct 2, 2015 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.