Home
  • English
  • עברית
  • Home
  • Portfolio
  • Blog
  • Services
  • About us
  • Contact

Message module and CTools $plugin

Submitted by Amitaibu on Thu, 06/10/2010 - 14:39
— Tagged as
  • CTools
  • Drupal-planet
  • Message

If I may quote myself:

I'm thinking, which is easier than actually sitting down and writing a patch, that it would be neat if CTools exportables plugin had a uniform UI that modules such as Context, Message and others to come, could use.

We're in a scratch your own itch world aren't we, huh? So I've created a CTools repository in github, created a branch called export-ui and it resulted with this getting-close-to-commit committed patch.

When I started writing Message I had some understanding of how CTools exportables work but having merlinofchaos review my work, explain some of his thoughts and providing his golden code, really changed the way I look at writing generic and reusable code. In fact, I've decided to overhaul much of the Message module for it to be more inline with CTools' standards.

Message was a inspired by Context module. Context uses some object oriented, so I've copied that too. That's where I got it wrong - because my use case was a bit different. OOP is powerful when it's needed but sometimes it might be an overkiller. Ok, so no OOP, but I still needed a pluggable system. CTools has the $plugin concept. Its almost the same as having an info hook, only it's without a hook - you place it directly on an inc file that CTools will include for you when its needed. But the "trick" that I've learned - and there shouldn't be any drum rolls cause it might be obvious - is to use a "process" function that populates the $plugin with defaults. The defaults can be for example the callbacks that will be used, the Views handlers that will be automatically declared, the text strings, the allowed operations, etc'. Basically, when you use the $plugin, you should be thinking how an extending module can alter the module's behavior by simple changes.

For example here's all the code needed to define an Organic groups realm, which means you can make messages accessible only to users that are subscribed to a group:

<?php
// Message plugin deceleration.
$plugin = array(
 
'realm' => 'og',
 
'title' => t('Organic groups realm'),
 
'description' => t("Determine access by a user's membership in a group."),
);

/**
* Get all groups a user belongs to.
*/
function message_plugin_og_accessible_ids($account) {
  return
array_keys($account->og_groups);
}
?>

Tada! Message is taking care of all the rest.

To sum it - Message is trying to follow CTools' idea - do the heavy lifting for you by assuming sensible defaults, but still allow you to change the behavior by as little code as possible.

If you can completely disregard the news and politics then dang - these are happy times!

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><blockquote>
  • Lines and paragraphs break automatically.
  • Pairs of<blockquote> tags will be styled as a block that indicates a quotation.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Gizra | Brener 5, Tel-Aviv 63826 Israel | +972-527-587-697 | info@gizra.com