This draft patch introduces a common set of methods for saving, loading, and deleting Drupal objects (nodes, users, taxonomy terms, etc.) and also a new hook,
hook_drupal(), to be used with all object types, supplementing (but not replacing) the existing set of
On the dev list recently we've discussed a longstanding desire: to bring consistency to the various Drupal object types (node, term, user, etc.) and at the same time make it possible to attach behaviours to all object types at once (rather than having to do each as a separate hook).
Several creative approaches have been suggested. They all have at their core the creation of a common set of methods that all object types can extend. From there several different proposal emerge. Some want to introduce a new global object table, so all objects have a single unique identifier. Some ideas involve relatively minor changes, while others propose radical restructuring of our existing code.
We could and should continue discussing possible approaches. But it seems most practical to start with implementing the minimum we've agreed on so far: a common, consistent set of methods for all object types. With that implemented, we'll be in a better position to see what further restructuring we need. It will also put some powerful immediate benefits of a common API into fairly easy reach, e.g., converting node_access to be a general object access system.
- Introduce a set of object handling functions in common.inc:
drupal_load(), calls the appropriate $type's method, e.g.,
drupal_invoke(), used to invoke a type-specific method, e.g.,
node_load(). This is called only by the
drupal_delete()methods. In other words, we retain our existing _load, _save, and _delete functions (for nodes, users, etc.), but they're now called not directly but via the general drupal_ calls.
drupal_invoke_drupal(). This calls the general hook_drupal(), which can be implemented for all object types. Our old friends
hook_user()still exist, and can still be implemented separately. In fact, doing so is more efficient if the desired action applies to only one object type, e.g., node. They are not, however, invoked directly. Rather, calling
hook_drupal()and the type-specific hook, e.g.,
hook_nodeapi()is renamed to
- Order of arguments is reordered for consistency and for passing by reference. Specifically, the $op argument comes before the $object being passed in
hook_drupal()and therefore in
- Existing object functions are renamed for consistency, e.g.,
- The existing _save and _delete return values
SAVE_DELETEDare returned for all object types (e.g., they were used previously for terms but not for nodes).
- Taxonomy vocabulary and term object handling is pulled into two include files, vocabulary.inc and term.inc, which have function names without the 'taxonomy', e.g.,
hook_term()implementations are possible, but in most cases it will be simpler to change
- The existing function
drupal_load()is renamed to
drupal_load_file(), since we introduce a new
Many, no doubt!
This is a roughed-in and untested patch. I haven't yet upgraded my test environent to MySQL 4.1 so I haven't been able to test at all yet. I'll do so soon and post revisions. It's certainly broken in places.
One I'm aware of is passing by reference. In this draft I've used only a single argument passed by reference in
hook_drupal() and the other object hooks (
hook_user(), etc.) are invoked. However, at least one existing hook,
hook_user(), has two arguments passed by reference in at least some hook implementations. I'll need to look more closely at these to see if it's possible to get this down to a single argument passed by reference. If not, it'd be messy, since arguments passed by reference can't have defaults. We'd need to pass in arguments for all calls to
drupal_invoke_drupal(), even though in most cases they're not needed.
Also, a number of inconsistencies are not addressed here. For example, though
I'd appreciate feedback on the general approach and the specific implementation. Is this where we want to be going as a first step to the aims outlined above?