[support] can i hook a hook?

Larry Garfield larry at garfieldtech.com
Sat Dec 11 10:01:47 UTC 2010


On Friday, December 10, 2010 3:12:32 am sebastian chedal wrote:
> Thanks for your input, a few final questions about this before i give up on
> trying to be fancy.
> 
> 1. Is it not possible to "extend" a module with a custom module? Kinda like
> if I was object programing? Or is hook_alter also required for this?

No.  Modules are not classes (for good reason) and cannot be extended in that 
fashion.

> 2. Is there any documentation I can read about making a module able to be
> altered? What changes need to be made to allow this?

There's about 419 ways to make a module extensible, or extend another module. 
:-)  Unfortunately there's also over a million lines of code in contrib, and 
not all of them are accessible via those mechanisms.  At some point code just 
needs to do what it needs to do.

In general, make sure you build good API code even if you're not using it in 
an API-ish way, and leverage existing Drupal patterns whenever possible.  At 
least the good ones.  hook_block() is not a good pattern, sadly. :-(

> 3. The nodeasblock.module does have several nodeasblock_*_form_alter
> functions in it, is it somehow possible to preprocess some of these in a
> theme or in a custom module, to add the new checkbox to toggle the link
> being an href yes/no, and to then call a different function [from my custom
> module] to accomplish this?

Without looking at the code I couldn't say.  It really depends on what the 
display code looks like.  If it's a theme function, then you can override that 
in your theme.  If it's hard coded into the hook_block() implementation 
directly then you're pretty much stuck.

> Even if this last option is not the best solution [since I should probably
> submit a patch to the module with the new checkbox as part of the module's
> code], I'm curious to just know IF this is possible; so that when I
> encounter more complicated problems, where what I need to do isn't
> necessarily something that anyone else is ever going to want, and I still
> want to make sure that if the module is updated that my extension to it
> still (probably) works.

Whether or not it's possible depends on which subsystem you're using and 
whether or not the module developer planned ahead properly.  Some do very well 
there, others not so much.  It's a challenging educational problem.

> While I appreciate your comment that I could just "leave it" I'm also
> trying to learn here at the same time the more advanced aspects of drupal
> at this stage since I am feeling ready for it! At the moment the best
> solution I have is using PHP at the block-nodeasblock.tpl.php level to do
> a string replace and strip the a href; but that feels like "wearing a
> bullet vest" instead of just "getting rid of the gun".

Well if it's a template file then you have a preprocess step you can leverage 
to overwrite the variable.  Again, assuming the original module was coded in a 
way that lets you.  It really does sound like the maintainer just didn't 
foresee this situation so you can't hook into it at the level you need.  

> Until now I've been depending on a lot of other Drupal people to handle
> bugs/features when they get hardcore but I really want to get a better
> understanding for how to modify / extend / hook into and alter modules.
> It's hard finding a lot of good information on this, so I'm always open to
> some good links too!

The module developers guide and theming guide in the handbooks are a good 
place to start.  I'd also recommend stopping by IRC to see if someone can help 
there, as it's much more efficient than the mailing list.

Cheers!

--Larry Garfield


More information about the support mailing list