wesley tanaka

Drupal: Partway Object Oriented

Drupal doesn't use PHP classes.  In spite of that, one can look at Drupal with object oriented glasses.  It helps make some sense of the system. But if none of that made any sense to you, don't worry about reading this section. I just think that I would have understood Drupal a lot quicker if something like this had been the first thing I read about it.

As a convention in this section, concepts are followed with their drupal equivalents formatted as code.  For example, "someMethodCall (drupal_do_something)" means that the conceptual method someMethodCall is implemented in drupal with the drupal_do_something function.

One can think of the Drupal core as defining the following base classes.  Instances of these classes are sometimes (but not always) stored in PHP as typeless objects.

abstract class AbstractNode

This class isn't ever really referred to by name.  Some people refer to it as "node", but for the most part "node" refers to an object which is of the type AbstractNode.

Conceptual examples of subclasses include "BlogPost" (blog), "Photograph", "SingleStaticWebPage" (page), "ForumPost" (forum).  You can find the subclasses of node available on your site in http://example.com/admin/content/types.  Instance objects would be particular individual blog posts, or specific individual forum posts.  If you are reading this online, the webpage you're looking at is an instance of Drupal's Book Class (the book node type).

Drupal core defines the story type, which is exactly like page in that they're both trivial concrete subclasses of AbstractNode (neither defines any extra functionality).  Why do that?  You can choose to treat them differently (maybe show timestamps on story nodes, but not page nodes).  If you choose to treat them exactly the same, then yes, there is no point.

A few of AbstractNode's concrete methods:
Signature
Drupal equivalent
Description
void putIntoDB()
node_save($node) Saves the node into the database
int getNodeID()
$node->nid
Gets the integer node id, which uniquely identifies the node.  If you go to a drupal site and visit node/1234, you're looking at the node with node ID 1234
static AbstractNode fetchFromDB(nodeID)
node_load($nid)
Gets a node object from the database by node ID
String formatAsHTMLForDisplay()
node_view($node, ...)
Gets HTML used for displaying the node to the browser
String formatEditForm()
node_form($node, ...)
Gets an HTML form used for editing the node
void deleteFromDB()
node_delete($nid)
Deletes the node from the database
int getCreationTime()/setCreationTime()
$node->created
Creation time
int getAuthorUserID()/setAuthorUserID()
$node->uid
Gets a user ID for the owner of the node (often the author)
etc...
   

abstract class Block

These are the rectangular things that show up in the sidebars on many sites.  Examples of things you might implement with a block subclass: sidebar ad units, login boxes, "what's the weather in my city".  Subclasses are often singletons, since you usually don't want two of the same block on the same page.  Well, some people do.  3 Google AdSense ads in blocks on the same page would be an example of multiple instances of a class GoogleAdsenseBlock

Methods include:
Signature
Drupal Equivalent
Description
String getBlockTitle()
mymodule_block('view', ...)
The title for the block, usually displayed at the top
String getBlockBody()
mymodule_block('view', ...) The HTML for the block body

Drupal also conceptually has a class DefaultBlock extends Block which has a UI (http://example.com/admin/build/block/add) allowing you to instantiate a block which is stored as a row in the database.

abstract class Filter

One method:
Signature
Drupal Equivalent
Description
String go(String in)
mymodule_filter('filter', ... , $in)
Takes a string as input, and returns a string
Examples of subclasses: "remove dangerous html tags", "guess what this broken html means and output valid html", "change newline character into <br />", "replace emoticons with <img> tags", "swedish chef filter".

Filters are used on anything that has a format associated with it (mainly node bodies, comments, block bodies).  What's a format?

class Format

An ordered list of Filter.  Singleton.  Instances of Format have an integer formatID (usually $format) which uniquely identify that Format.
One method:
Signature
Drupal Equivalent
Description
String go(String in)
check_markup($in, $format, ...)
Runs each Filter contained in this Format on the input in order, passing the output from one filter to the input of the next and returning the final result.

final class Comment

One of the weird exceptions to the "everything is a Node" rule, which is especially strange because comments and nodes have a lot of similar functionality that has to be duplicated for both.  No subclasses allowed.  Any single comment on a Drupal site is an instance of Comment.

Signature
Drupal Equivalent
Description
int getParentNodeID()
$comment->nid
Runs each Filter contained in this Format on the input in order, passing the output from one filter to the input of the next and returning the final result.
int getFormattedBody()
check_markup($comment->comment, $comment->format)
Formats the body of the comment using its associated Format object
etc...
   

class User

For the most part, cannot be overridden.  Examples of instances: one normal user on your site.  the administrator.  the anonymous user instance that represents any anonymous user.

Signature
Drupal Equivalent
Description
authenticateMe()
mymodule_auth(...)
The one user-related function that you can override -- by default, does a query on the local drupal user table and checks the password.  This can be used to accept OpenID, or if you have a different database table that already contains your users and you want to integrate those users into Drupal.
String formatMyProfileForHTMLDisplay()
user_view($uid)
Shows the user's profile page.
void logMeOut()
user_logout()
Logs the user out
isBlocked()
user_is_blocked($username)
Checks if a user is blocked from using the site
setBlocked(isBlocked)
$function = 'user_user_operations_' . ($isBlocked?'':'un') . 'block';
$function(array($user));

Sets the user's status to either prevent them from using the site or allow them back in.
etc...
   

Other

Drupal also contains a bunch of "classes" which would probably correspond in C to structs.  These are mostly stored and passed around as arrays of arrays of arrays of ...

class FormElement

A logical element in a form.  Examples: "text field", "set of checkboxes", "date", "attachments".  Represented as an associative array().

class Form

An array() of FormElement.
Syndicate content