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
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@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 Jefferson
thanks for jumping in :)
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.
perfect - that's what I need. I have a question about what if there are multiple modules that are getting called back - but I'll look through the doco and see what is what. I'll come back if I have any questions
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.
yeah - i started here - but I like the hook idea much better.
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.
that's what I thought (that .module) are always included - thanks for confirming.
http://api.drupal.org/ - Your new best friend. :-)
oh yeah - I'm all over it - just wasnt' sure the best place to begin - thanks!
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
On 5-May-06, at 11:44 AM, Dan Robinson wrote:
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.
yeah - i started here - but I like the hook idea much better.
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.
that's what I thought (that .module) are always included - thanks for confirming.
Just to perhaps help make this distinction clear: * implement a new hook_ call that other modules can implement Pro: uses built-in "plugin" method of Drupal Con: all modules always get included * use custom .inc files for your module Pro: potentially less load for large numbers of .inc files Con: custom way of including functionality The hook_ method likely outweighs any near term benefits that you MIGHT get from provisional inclusion, because EVERYONE benefits from making module inclusion/caching/etc. faster. -- Boris Mann Vancouver 778-896-2747 San Francisco 415-367-3595 SKYPE borismann http://www.bryght.com
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"?
Sorry - I don't have a huge amount of time to talk about it, but check out my game.module. It does what you're asking, using a hook based system, but with .inc files. It's probably more complicated than what you need, but look for the following in the CVS [1]: The following routines duplicate the internal nodeapi and module loading of Drupal core, but with a focus on .inc files and as-needed loading. And specifically these functions: game_system_list game_include_list game_include_invoke game_include_invoke_all game_include_hook And for example: // load in all race includes $includes = game_system_list('races'); // for each one loaded... foreach ($includes as $include) { // call the 'info' hook and store results in $shelf_item. $shelf_item = game_include_invoke($include->name, 'info'); } I'll try to answer any questions a bit later. [1] http://cvs.drupal.org/viewcvs/drupal/contributions/modules/game/ -- Morbus Iff ( IGNORANCE IS BLISS, AND WE ARE BLISSED OFF ) Technical: http://www.oreillynet.com/pub/au/779 Culture: http://www.disobey.com/ and http://www.gamegrene.com/ icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus
On May 4, 2006, at 10:09 PM, Dan Robinson wrote:
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.
The ecommerce package might be a good source of inspiration for this. It doesn't use plugins but keeps in-line with the Drupal hook system, meaning other modules can tap into the ecommerce API. There's an api for products, payments, the checkout process, etc. I also opted to use *.module files instead of includes with the intentions being that any component of the ecommerce package could also be used by itself. For example, I just want a shopping cart because I already have another product creation system. Hope that helps! Matt Westgate | www.lullabot.com
participants (5)
-
Boris Mann -
Dan Robinson -
Larry Garfield -
Matt Westgate -
Morbus Iff