Glue code: Component module

Submitted by Larry on 13 July 2007 - 2:54am

Lullabot's Jeff Eaton has been talking about "Glue Code" lately; that's the extra little bit of code you write to make module A talk to module B to add that last little bit to that View you setup with that CCK-based node to make it just right. He's started spinning some of it off into modules, like Top Node, and asked for other people's favorite glue. Here's mine. :-)

The most common "glue" I need is along the lines of "so I have this block, but I don't want it in a block region. I want it right there!" Mostly it's a block or a view. Panels is way overkill for that, and doesn't let you dynamically add a block to, say, the content area of a node page. Sure you could setup custom block regions and add some PHP code to control when it does or does not show up, but dear god why? :-)

Enter component.module. It's basically some stock code ripped out of Panels 1.0 and wrapped into simple convenience functions. Want a block? call component_get_block($module, $delta), and you get back a string that is the fully rendered and themed block. Then do something with it. Print it in a template file, add it to a $node->content array, whatever. There's another for pulling a view, another for a view filter form, and even one for a panel (that I've never actually needed yet, but it was easy to add).

Sure, pulling a block like that is only maybe 4 lines of code, and a view is maybe 10. But who wants to write 10 lines of code when they don't have to?

I briefly toyed with the idea of calling the module "mvc", since it enables pull-based templating rather than push-based (PAC), but I decided that was too pedantic even for me. Documentation is, sadly, rather sparse at the moment. One of these days I'll write something better up for it, but for now the function names are, hopefully, self-explanatory.

No horses were harmed in the making of this glue code.

Anonymous (not verified)

4 January 2008 - 12:10pm

New to drupal. This sounds like exactly what I need, but from what file do I call these functions like component_get_block()? I want a certain content type to pull in a field from another content type.

From wherever makes sense. :-) I've personally called it from within a template file, within a phptemplate_ function, and from inside hook_nodeapi(). The first is probably a bad idea, but the latter two should be fine.