Bundleclasses
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
BundleClasses adds actual classes to Drupal. Out-of-the-box Drupal doesn't have very good Entity support. Entity API improves on that, but still Node and User etc aren't classes. And besides: all node types are completely different, so should have different classes.
Doesn't play well with:
1. stdClass Type hinting (don't do that):
File entity: #2180639: stdClass type hinting breaks real entity objects
Media: #2180641: stdClass type hinting breaks real entity objects
Pathauto: #2117209: Relax function arguments policy on pathauto_*_update_alias() functions.
Realname: #2182881: stdClass type hinting breaks real entity objects
2. Something in Entity when saving Field Collections programmatically..? Something with "indirect modification of overloaded property xxxx" I think. A fatal error if I remember correctly, so that might be a deal breaker if you're very picky.
3. Drupal core. Strangely enough core modules don't have good entity support. Which is the reason this module exists. It's also the reason it doesn't always work well. The biggest flaw: hook_node_presave et al don't get passed in a Node object, but ALWAYS an stdClass, even with this module on. Can't change that. No fix. Hideous requirement:
function hook_node_presave($node) {
$real_node = $node;
_bundleclasses_ensure_entity_class('node', $node);
// Now $node is the stdClass object you must alter, and $real_node is
// the BCNode object with the logic and methods and magic. =(
}
It does this:
Create an (optional) Node class in your custom module:
// BCNode is BundleClasses' standard Node class. All your Node classes
// MUST extend that.
class MyNode extends BCNode {
public function alert() {
// My app loves to send people mail about nodes. This helper does
// that with $node->alert().
}
}
And tell BC to use it:
// In an update hook for instance. It's saved into a variable, so do it
// just once!
_bundleclasses_set('node', 'MyNode', 'type');
And since all node types are completely different, they should have different classes:
// %bundle% will be replaced by the camel cased bundle name: my_node_type
// becomes MyNodeType.
_bundleclasses_set('node', 'My%bundle%Node', 'bundle');
And you can do the same for any other entity type:
// Users don't have bundles.
_bundleclasses_set('user', 'User', 'type');
// All my comments are the same.
_bundleclasses_set('comment', 'GenericComment', 'type');
// Taxonomy terms are important and special.
_bundleclasses_set('taxonomy_term', 'Term', 'type');
_bundleclasses_set('taxonomy_term', '%bundle%Term', 'bundle');
// And all other entity types too (don't forget to extend the
// original 'entity class').
_bundleclasses_set('registration', 'Registration', 'type'); // This doesn't change but it's mandatory.
_bundleclasses_set('registration', '%bundle%Registration', 'bundle');
Don't forget to create all those classes and add them to files[] in your module's info file.
