[development] implementing "plugins"

Larry Garfield larry at garfieldtech.com
Fri May 5 04:39:04 UTC 2006

There's a bunch of ways to do plugins in PHP, some OO some not.  In the CMS I 
use at work, I think I have 2 or 3 different methods in use in various 
places. :-)

If this is for Drupal specifically, then there's really just two ways to 
reasonably consider:

1) Do you want other modules to be able to define these plugins?  If so, then 
the Drupal Way(tm) is to define a hook.  You would call something like:

module_invoke_all('mymoduleapi', 'anoperation', $someparameter);

And other modules can simply declare a function:

function othermodule_mymoduleapi($op, $stuff) {

And do whatever.  Hooking those together is handled magically by Drupal.  See 
hook_block(), hook_nodeapi(), etc. for examples.

2) Do you want people to just drop .inc files into a plugins directory inside 
your module?  For that, look at the flexinode module, which does exactly 
that.  (There are lots of others, but that's the first one that comes to 
mind.)  I've not looked at its guts in detail, but that's the direction you'd 
want to go.

As for what gets included, If you're module is enabled then Drupal will 
automatically include your mymodule.module file on every page load, but 
that's it.  You can include/require whatever else you need/want.  Note that 
it is NOT included into the global namespace, so if you put stuff outside of 
a function (which you shouldn't do), it won't be happening globally.

http://api.drupal.org/ - Your new best friend. :-)

On Thursday 04 May 2006 22:09, Dan Robinson wrote:
> Hi,
> I want to introduce a concept of "plugins" to my module.  In this case
> the plugin merely needs to supply a single function that returns data.
> Is the best way to do this to have the plugin call a register function
> with the name of a function (and then call "eval"?).  This is probably
> more of a PHP question than a Drupal question.  Oh yes and is there
> documentation somewhere on what code Drupal automatically "includes"?
> Thanks,
> Dan

Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 

More information about the development mailing list